Tuesday 17 March 2015

PowerShell - Running & Exec Policy

Running PowerShell scripts 

Once you've written your script, save your script in a file with a .ps1 extension. From the PowerShell console, you can run the script by specifying the full path to the script: 

PS C:\> C:\Scripts\Get-RunningServices.ps1  

Note that your scripts can also be parameterized so that it can take an incoming value when invoked. If this is the case, you can specify the parameter the same way you specify it in a regular cmdlet: 

PS C:\> C:\Scripts\Get-RunningServices.ps1 -ComputerName ROGUE 

If you are at the script directory, you don't have to specify the path. You can also use a dot-sourcing operator to run the script. Dot sourcing a script means that any of the variables and functions in the script are loaded into the current scope and available for use in the same console session:
PS C:\Scripts> .\Get-RunningServices.ps1
PS C:\Scripts> .\Get-RunningServices.ps1 -ComputerName ROGUE 
 
Note that depending on your execution policy settings, the script may run or get access denied errors. If this is the case, you may either need to adjust your execution policy or sign your script.

Execution policy

The available execution policies are provided in the following table:


Policy
Runs a command?
Runs a local script?
Runs a remote script?
Restricted
Yes
No
No
AllSigned
Yes
Must be signed
Must be signed
RemoteSigned
Yes
Yes
Must be signed
Unrestricted
Yes
Yes
Yes—prompts before running downloaded scripts
Bypass
Yes
Yes
Yes—no warnings or prompts

The default execution policy depends on the operating system you are using. For Windows 8, Windows Server 2012, and Windows 8.1, the default policy is Restricted. For Windows Server 2012 R2, it is RemoteSigned.

If you want to check which execution policy you are running on, you can use the following command:

Get-ExecutionPolicy 
 
If you want to change it, use the following command:

Set-ExecutionPolicy 
 
The following is a screenshot of what you can expect when you run these two cmdlets:


No comments:

Post a Comment