I’ve a requirement to change remote server Execution policy during my Script execution, In order to do this I’ve apt below work flow.
- Collect Current Execution policy and assign it to Parameter
- Change Execution policy to Remotesigned
- Change Policy to old state.
I’ve tried with below code but its not allowing me to do so, Can anyone help me with your thoughts.
Step 1:
$global:ExecutionActual = Invoke-command -computername $Windows_Name -scriptblock {Get-ExecutionPolicy -Scope LocalMachine} | Select Value -expandproperty Value
Step 2:
Invoke-command -computername $Windows_Name -scriptblock {Set-ExecutionPolicy RemoteSigned -force}
Step 3:
Invoke-command -computername $Windows_Name -scriptblock {Set-ExecutionPolicy $global:ExecutionActual -force}
I’ve issue with step 3, Below one is the sample error.
Cannot bind argument to parameter ‘ExecutionPolicy’ because it is null.
+ CategoryInfo : InvalidData: (
[Set-ExecutionPolicy], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetExecution
PolicyCommand