30 Nov 2011 @ 2:57 PM 

Sometimes I’m at a customer where an end-user calls while testing the newly build environment and reports that some settings are not applied. Often these settings are applied through Group Policy or PowerShell scripts.
For the PowerShell part, the first thing I check is the execution policy.

Although I could ask the user to provide me the output of the Get-ExecutionPolicy command, they will probably not appreciate me for asking this.
Next to that, most of the times I get a incident ticket with the message and no contact with the end-user myself.
Because of this, I’ve written this PowerShell function to get the execution policy for a remote computer:

#Usage: Get-RemoteExecutionPolicy Laptop1
function Get-RemoteExecutionPolicy ($TargetName)
{
$RegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $TargetName)
$PSRegKey= $RegKey.OpenSubKey(“SOFTWARE\\Microsoft\\Powershell\\1\\ShellIds\\Microsoft.PowerShell”)
$Policy = ($PSRegKey.getvalue(“ExecutionPolicy”)).tostring()
Return $Policy
}

It is also possible to invoke a command on the remote machine… like this:

Invoke-Command –Computer Laptop1 –ScriptBlock {Get-ExecutionPolicy}

Another way of doing this, is by using a remote PowerShell session… like this:

New-PSSession –Computer Laptop1 | Get-ExecutionPolicy

For more information about the commands to get the execution policy for specific scopes you can read one of my previous posts: PowerShell and the execution policies explained

Post to Twitter

Posted By: Jeff Wouters
Last Edit: 30 Nov 2011 @ 02:58 PM

EmailPermalink
Tags


  • http://twitter.com/andyjmorgan Andrew Morgan

    Correct me if I’m wrong, but I think this is dependent on the remote registry service. For a WMI based query, try this:

    function get-RemoteExecutionPolicy{
        param(
       [string]$computer=”.”)

        $executionpolicy=([WMIClass]“\$computerROOTDEFAULT:StdRegProv”).GetStringValue(“2147483650″, “SOFTWARE\Microsoft\Powershell\1\ShellIds\Microsoft.PowerShell”, “Executionpolicy”).svalue
        return $executionpolicy
     }

  • http://www.jeffwouters.nl Jeff Wouters

    Hi Andrew,
    Correct, my first option depends on the remote registry service… The second and third do not.
    Thanks for your WMI version, good one! :-)
    Jeff.


Get Adobe Flash player
 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 164
  • Comments » 150
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.