none
powershell powercfg -setactive script RRS feed

  • Question

  • I'm trying to get my script working which should simply change the active power scheme to "CUSTOM", which is a scheme that is added via group policy. I can run all of the exact same commands straight from the powershell command line, but when I run it as a script, it fails with the following error:

    Invalid Parameters -- try "/?" for help

        + CategoryInfo          : NotSpecified: (Invalid Parameters -- try "/?" for help:String) [], RemoteException

        + FullyQualifiedErrorId : NativeCommandError

        + PSComputerName        : computer1

    Can someone tell me what I'm doing wrong with this script?

    param (
    	[string]$ComputerName
    )
    
    
    try {
    	$CUSTOM = Invoke-Command -ComputerName $ComputerName -ScriptBlock { powercfg -l | %{if($_.contains("CUSTOM")){$_.split()[3]}} }
    	Write-Host "CUSTOM ="$CUSTOM
    	$CurrPlan = Invoke-Command -ComputerName $ComputerName -ScriptBlock {$(powercfg -getactivescheme).split()[3]}
    	Write-Host "CurrPlan ="$CurrPlan
    	if($CurrPlan -ne $CUSTOM) {
    		Write-Host "Changing Active Power Plan"
    		Invoke-Command -ComputerName $ComputerName -ScriptBlock { powercfg -s $CUSTOM }
    		Write-Host "SetActive Triggered"
    	}
    } catch { Write-Warning -Message "Unable to set power plan to CUSTOM" }
    
    $newPlan = Invoke-Command -ComputerName $ComputerName -ScriptBlock {$(powercfg -getactivescheme).split()[3]}
    if ($newPlan -eq $CUSTOM) { Write-Host "Plan is set to CUSTOM" }
    else { Write-Host "Setactive Failed" }
    
    Write-Host $ComputerName ": CUSTOM=" $CUSTOM "; Current Plan=" $currPlan
    

    • Moved by Bill_Stewart Tuesday, June 16, 2015 7:52 PM Abandoned
    Thursday, April 23, 2015 3:33 PM

All replies

  • Post all of the error message.


    \_(ツ)_/

    Thursday, April 23, 2015 5:22 PM
  • I already did.

    Invalid Parameters -- try "/?" for help

        + CategoryInfo          : NotSpecified: (Invalid Parameters -- try "/?" for help:String) [], RemoteException

        + FullyQualifiedErrorId : NativeCommandError

        + PSComputerName        : computer1

    Thursday, April 23, 2015 5:25 PM
  • You have  system problem.  Perhaps you have a corrupt script file. 

    It is also possible that Group Policy has you locked out.


    \_(ツ)_/

    Thursday, April 23, 2015 6:20 PM
  • Could you explain what brought you to those possible conclusions?

    I can run commands manually from the powershell command line. My script file is just a plain text file. Everything looks normal when I open it in notepad (not corrupted). If it were group policy, the manually typed commands wouldn't work, either. I can run other powershell scripts just fine.

    Thursday, April 23, 2015 7:58 PM
  • You had a number of logic errors and odd constructs:

    Function Set-PowerCfg{
    	Param (
    		[Parameter(Mandatory)]
    		[string]$ComputerName
    	)
    	
    	Process{
    		try{
    			$session = New-PSSession $ComputerName
    			$sb = { if ($line = (powercfg -l | Select-String 'CUSTOM')) { $line[0].Line.Split(' ')[3] } }
    			if ($CUSTOM = Invoke-Command -ScriptBlock $sb) {
    				Write-Host "CUSTOM=$CUSTOM" -fore green
    				$CurrPlan = Invoke-Command -ComputerName $ComputerName -ScriptBlock { $(powercfg -getactivescheme).split()[3] }
    				Write-Host "CurrPlan=$CurrPlan" -fore green
    				if ($CurrPlan -ne $CUSTOM) {
    					Write-Host "Changing Active Power Plan"
    					#Invoke-Command -Session $session -ScriptBlock { Param ($plan); powercfg -s $plan } -Args $CUSTOM
    					Write-Host "SetActive Triggered" -fore green
    				} else {
    					Write-Host 'CUSTOM already set' -fore green		
    				}
    			} else {
    				Write-Host 'CUSTOM not found' -fore red
    			}
    			
    		}
    		catch {
    			Write-Host "Unable to set power plan to CUSTOM" -fore red
    		}
    	}
    }
    
    Set-PowerCfg
    


    \_(ツ)_/

    Thursday, April 23, 2015 9:13 PM
  • Thanks. What was I doing wrong, though? Could you explain why certain parts in my script were wrong?

    thanks for your time :)

    Friday, April 24, 2015 4:08 PM
  • This is one of a number of better approaches to what you are trying to do.  It may also help you see where your errors and logic failed.

    Function Set-PowerCfg{
    	Param (
    		[Parameter(
                Mandatory,
                ValueFromPipeline
            )]
    		[string]$ComputerName,
            $scheme='CUSTOM'
    	)
    	Begin{
            #create scritpblock once inpipeline
    		$sb ={
                param($scheme)
                Write-Host "Attempting to set power scheme on $ComputerName to $scheme" -fore green
                if($line=powercfg -l | Select-String $scheme){
                    $custom=$line[0].Line.Split(' ')[3]
                    Write-Host "SELECTED=$custom" -fore green
                    $CurrPlan=(powercfg -getactivescheme).split()[3]
                    Write-Host "CurrPlan=$CurrPlan" -fore green
                    if ($CurrPlan -ne $CUSTOM) {
                        Write-Host "Changing Active Power Plan"
                        #powercfg -s $custom
                        Write-Host 'SetActive Triggered' -fore green
                    }else{
                        Write-Host 'CUSTOM:$scheme already set' -fore green		
                    }
                } else {
                    Write-Host 'CUSTOM:$scheme not found' -fore red
                }
    		}
        }
    	Process{
            Invoke-Command -ScriptBlock $sb -ComputerName $ComputerName -ArgumentList $scheme
    	}
    }
    
    Set-PowerCfg -ComputerName $env:COMPUTERNAME


    \_(ツ)_/

    Sunday, April 26, 2015 1:26 PM
  • Here is a more flexible and faster way to set the power plane:

    Function Set-PowerCfg{
    	Param (
    		[Parameter(
                Mandatory,
                ValueFromPipeline
            )]
    		[string]$ComputerName,
            $scheme='CUSTOM'
    	)
        
        Process{
            $ErrorActionPreference='Stop' # set for whole function
            Write-Host "Attempting to set power scheme on $ComputerName to $scheme" -fore green
            Try{
                $current=(Get-WmiObject win32_powerplan -Namespace root\cimv2\power -Filter 'isActive=true' -ComputerName $computername).ElementName 
                Write-Host "Current Power Plan on $ComputerName is $current" -fore green
                if ($current -eq $scheme) {
                    Write-Host 'CUSTOM:$scheme already set' -fore green		
                }else{  
                    $target=Get-WmiObject win32_powerplan -Namespace root\cimv2\power -Filter "ElementName='$scheme'" -ComputerName $computername -EnableAllPrivileges
                    if($target){
                        Write-Host "Changing Active Power Plan to $scheme"
                        $return=$target.Activate()
                    } else {
                        Write-Host 'CUSTOM:$scheme not found' -fore red
                    }
                }
            }
            Catch{
                #rethrow exception and exit
                Throw $_
            }
        }
    }
    Set-PowerCfg -ComputerName w8test -scheme balanced
    
    


    \_(ツ)_/

    Sunday, April 26, 2015 2:04 PM