Change Execution Policy

Welcome Forums General PowerShell Q&A Change Execution Policy

Viewing 3 reply threads
  • Author
    Posts
    • #223377
      Participant
      Topics: 4
      Replies: 2
      Points: 40
      Rank: Member

      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.

      1. Collect Current Execution policy and assign it to Parameter
      2. Change Execution policy to Remotesigned
      3. 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

    • #223389
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Hello Ganesh,

      The error is telling you the argument for parameter ‘executionpolicy’ is null. The reason it’s null is because it is defined on the machine you are running the script on, not the remote machine. You may resolve your issue with either of the following.

      Invoke-command -computername $Windows_Name -scriptblock {Set-ExecutionPolicy $args[0] -force} -ArgumentList $global:ExecutionActual

      or

      Invoke-command -computername $Windows_Name -scriptblock {Set-ExecutionPolicy $using:global:ExecutionActual -force}

    • #223752
      Participant
      Topics: 4
      Replies: 2
      Points: 40
      Rank: Member

      Thanks a lot, It’s worked for me.

    • #223788
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      Fantastic. You are welcome.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.