The DirectoryListWithArguments.ps1 script will not run in Windows PowerShell by default. This is because scripting support is disabled by default in Windows PowerShell. If you attempt to run a Windows PowerShell script when the support has not been enabled, an error will occur, and the script will not run. This error message is seen in Figure 4-1.
Figure 4-1: Error generated when attempting to run a Windows PowerShell script when execution policy not set
This is referred to as the restricted execution policy. There are four levels of execution policy that can be configured in Windows PowerShell by using the Set-ExecutionPolicy cmdlet. These four levels are listed in Table 4-1. The restricted execution policy can be configured by group policy using the "Turn on Script Execution" Group Policy setting in Active Directory. To configure the group policy setting in Active Directory, you can download the required ADM file from http://www.microsoft.com/downloads. It can be applied to either the computer object or the user object. The computer object setting takes precedence over other settings.
User preferences for the restricted execution policy can be configured by using the Set-Execution-Policy cmdlet, but they will not override settings configured by Group Policy. The resultant set of restricted execution policy settings can be obtained by using the Get-ExecutionPolicy cmdlet.
Level | Meaning |
---|---|
Restricted | Will not run scripts or configuration files |
AllSigned | All scripts and configuration files must be signed by a trusted publisher |
RemoteSigned | All scripts and configuration files downloaded from the Internet must be signed by a trusted publisher |
Unrestricted | All scripts and configuration files will run. Scripts downloaded from the Internet will prompt for permission before running |
Just the Steps | To retrieve the script execution policy, use the Get-ExecutionPolicy cmdlet. |
Open Windows PowerShell.
Use the Get-ExecutionPolicy cmdlet to retrieve the effective script execution policy. This is shown here:
Get-ExecutionPolicy
This concludes this procedure. Leave Windows PowerShell open for the next procedure.
Q. Do Windows PowerShell scripts work by default?
A. No. Windows PowerShell scripts must be explicitly enabled.
Q. What cmdlet can be used to retrieve the resultant execution policy?
A. The Get-ExecutionPolicy cmdlet can retrieve the resultant execution policy.
Q. What cmdlet can be used to set the script execution policy?
A. The Set-ExecutionPolicy cmdlet can be used to set the script execution policy.
Use the Set-ExecutionPolicy cmdlet to change the script execution policy to unrestricted. This command is shown here:
Set-ExecutionPolicy unrestricted
Use the Get-ExecutionPolicy cmdlet to retrieve the current effective script execution policy. This command is shown here:
Get-ExecutionPolicy
The result prints out to Windows PowerShell console as shown here:
Unrestricted
This concludes this procedure.
Tip | If the execution policy on Windows PowerShell is set to restricted, how can you use a script to determine the execution policy? One method is to use VBScript to read from the following registry key: \SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\ executionPolicy from both the HKLM and the HKCU hives. However, keep in mind these keys only appear after the execution policy has been changed. A script that checks the registry for the execution policy is GetPowerShellExecutionPolicy.vbs, found in the scripts folder for this chapter on the CD-ROMg. |
You cannot simply double-click on a Windows PowerShell script and have it run. You cannot type the name in the Start | Run dialog box either. If you are inside Windows PowerShell, you can run scripts if you have enabled the execution policy, but you need to type the entire path to the script you wish to run and make sure you include the ps1 extension.
Just the Steps | To run a Windows PowerShell script from inside PowerShell, follow these steps:
|
If you need to run a script from outside Windows PowerShell, you need to type the full path to the script, but you must feed it as an argument to the PowerShell.exe program. In addition, you probably want to specify the -noexit argument so that you can read the output from the script. This is shown in Figure 4-2.
Figure 4-2: Use the -noexit argument for the PowerShell.exe program to keep the console open after a script run
Just the Steps | To run a Windows PowerShell script from outside PowerShell, follow these steps:
|
The RetrieveAndSortServiceState.ps1 script uses the Get-WMIObject cmdlet to make a connection into the WMI service. We will examine WMI as it relates to Windows PowerShell in Chapter 5, Using WMI, and Chapter 6, Querying WMI, but because of the way Windows PowerShell uses cmdlets, you do not need to know everything about a technology to use it in your script. The RetrieveAndSortServiceState.ps1 script will create a list of all the services that are defined on a machine. It then checks to see if they are running, stopped, or disabled and reports the status of the service. The script also collects the service account that the service is running under.
A Sort-Object cmdlet is used to perform three sorts on the data: It sorts first by the start mode of the service (that is, automatic, manual, disabled); it sorts next by the state of the service (that is, running, stopped, and so forth); and it then alphabetizes the list by the name of each service in each of the two previous categories. After the sorting process, the script uses a Format-Table cmdlet and produces a table output in the console window. The RetrieveAndSortServiceState.ps1 script is shown here, and the Running Scripts Inside Windows PowerShell procedure examines running this script.
The script is designed to run against multiple remote machines, and it holds the names of the destination machines in the system variable $args. As written, it uses two computer names that always refer to the local machine: localhost and loopback. By using these two names, we can simulate the behavior of connecting to networked computers.
RetrieveAndSortServiceState.ps1
$args = "localhost","loopback" foreach ($i in $args) {Write-Host "Testing" $i "..." Get-WmiObject -computer $args -class win32_service | Select-Object -property name, state, startmode, startname | Sort-Object -property startmode, state, name | Format-Table *}
Note | For the Running Scripts Inside Windows PowerShell procedure, I copied the RetrieveAndSortServiceState.ps1 script to the C:\Mytest directory we created in Chapter 3. This makes it much easier to type the path and has the additional benefit of making the examples clearer. To follow the procedures, you will need to either modify the path to the script or copy the RetrieveAndSortServiceState.ps1 script to the C:\Mytest directory. |
Open Windows PowerShell.
Type the full path to the script you wish to run. For example C:\Mytest. You can use Tab completion. On my system, I only had to type C:\My and then press Tab. Add a backslash (\), and type the script name. You can use Tab completion for this as well. If you copied the RetrieveAndSortServiceState.ps1 into the C:\Mytest directory, then simply typing r and pressing Tab should retrieve the script name. The completed command is shown here:
C:\mytest\RetrieveAndSortServiceState.ps1
A partial output from the script is shown here:
Testing loopback ... name state startmode startname ---- ----- --------- --------- Alerter Running Auto NT AUTHORITY\Loc... Alerter Running Auto NT AUTHORITY\Loc... AudioSrv Running Auto LocalSystem AudioSrv Running Auto LocalSystem
This concludes this procedure. Please close Windows PowerShell.
Open the Run dialog box (Start | Run, or the Windows Flag key + R, or Ctrl + Esc then R).
Type PowerShell and use the -noexit switch. Type the full path to the script. The command for this is shown here:
Powershell -noexit C:\mytest\RetrieveAndSortServiceState.ps1
This concludes this procedure.
Tip | Add a shortcut to Windows PowerShell in your SendTo folder. This folder is located in the Documents and Settings\%username% folder. When you create the shortcut, make sure you specify the -noexit switch for PowerShell.exe, or the output will scroll by so fast you will not be able to read it. You can do this by hand, or modify the CreateShortCutToPowerShell.vbs script from Chapter 1, “Overview of Windows PowerShell.” |
Q. Which command can you use to sort a list?
A. The Sort-Object cmdlet can be used to sort a list.
Q. How do you use the Sort-Object cmdlet to sort a list?
A. To use the Sort-Object cmdlet to sort a list, specify the property to sort on in the property argument.