• No results found

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:

1. Type the full path to the script. 2. Include the name of the script. 3. Ensure you include the ps1 extension.

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:

1. Type the full path to the script. 2. Include the name of the script. 3. Ensure you include the ps1 extension. 4. Feed this to the PowerShell.exe program.

The RetrieveAndSortServiceState.ps1 script uses the Get-WMIObject cmdlet to make a connec- tion 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, run- ning, 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 pro- duces 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 Retrieve- AndSortServiceState.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.

Running scripts inside Windows PowerShell 1. Open Windows PowerShell.

2. 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

3. 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

4. This concludes this procedure. Please close Windows PowerShell. Running scripts outside Windows PowerShell

1. Open the Run dialog box (Start | Run, or the Windows Flag key + R, or Ctrl + Esc then R). 2. Type PowerShell and use the -noexit switch. Type the full path to the script. The com-

mand for this is shown here:

Powershell -noexit C:\mytest\RetrieveAndSortServiceState.ps1

3. 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 CreateShortCutToPower- Shell.vbs script from Chapter 1, “Overview of Windows PowerShell.”

Quick Check

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.