Using PowerShell Remoting and Jobs
- 2/15/2013
- Understanding Windows PowerShell remoting
- Using Windows PowerShell jobs
- Using Windows PowerShell remoting: step-by-step exercises
- Chapter 4 quick reference
Using Windows PowerShell remoting: step-by-step exercises
In this exercise, you will practice using Windows PowerShell remoting to run remote commands. For the purpose of this exercise, you can use your local computer. First, you will open the Windows PowerShell console, supply alternate credentials, create a Windows PowerShell remote session, and run various commands. Next, you will create and receive Windows PowerShell jobs.
Supplying alternate credentials for remote Windows PowerShell sessions
Log on to your computer with a user account that does not have administrator rights.
Open the Windows PowerShell console.
Notice the Windows PowerShell console prompt. An example of such a prompt appears here:
PS C:\Users\ed.IAMMRED>
Use a variable named $cred to store the results of using the Get-Credential cmdlet. Specify administrator credentials to store in the $cred variable. An example of such a command appears here:
$cred = Get-Credential iammred\administrator
Use the Enter-PSSession cmdlet to open a remote Windows PowerShell console session. Use the credentials stored in the $cred variable, and use localhost as the name of the remote computer. Such a command appears here:
Enter-PSSession -ComputerName localhost -Credential $cred
Notice how the Windows PowerShell console prompt changes to include the name of the remote computer, and also changes the working directory. Such a changed prompt appears here:
[localhost]: PS C:\Users\administrator\Documents>
Use the whoami command to verify the current context. The results of the command appear here:
[localhost]: PS C:\Users\administrator\Documents> whoami
iammred\administrator
Use the exit command to exit the remote session. Use the whoami command to verify that the user context has changed.
Use WMI to retrieve the BIOS information on the local computer. Use the alternate credentials stored in the $cred variable. This command appears here:
gwmi -Class win32_bios -cn localhost -Credential $cred
The previous command fails and produces the following error. This error comes from WMI and states that you are not permitted to use alternate credentials for a local WMI connection.
gwmi : User credentials cannot be used for local connections At line:1 char:1 + gwmi -Class win32_bios -cn localhost -Credential $cred + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands. GetWmiObjectCommand
Put the WMI command into the -scriptblock parameter for Invoke-Command. Specify the local computer as the value for computername and use the credentials stored in the $cred variable. The command appears here (using -script as a shortened version of -scriptblock):
Invoke-Command -cn localhost -script {gwmi -Class win32_bios} -cred $cred
Press the up arrow key to retrieve the previous command and erase the credential parameter. The revised command appears here:
Invoke-Command -cn localhost -script {gwmi -Class win32_bios}
When you run the command, it generates the error appearing here because a normal user does not have remote access by default (if you have admin rights, then the command works):
[localhost] Connecting to remote server localhost failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic. + CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransport Exception + FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
Create an array of computer names. Store the computer names in a variable named $cn. Use the array appearing here:
$cn = $env:COMPUTERNAME,"localhost","127.0.0.1"
Use Invoke-Command to run the WMI command against all three computers at once. The command appears here:
Invoke-Command -cn $cn -script {gwmi -Class win32_bios}
This concludes this step-by-step exercise.
In the following exercise, you will create and receive Windows PowerShell jobs.
Creating and receiving jobs
Open the Windows PowerShell console as a non-elevated user.
Start a job named Get-Process that uses a -scriptblock parameter that calls the Get-Process cmdlet (gps is an alias for Get-Process). The command appears here:
Start-Job -Name gps -ScriptBlock {gps}
Examine the output from starting the job. It lists the name, state, and other information about the job. Sample output appears here:
Id Name PSJobTypeName State HasMoreData Location -- ---- ------------- ----- ----------- -------- 9 gps BackgroundJob Running True localhost
Use the Get-Process cmdlet to determine if the job has completed. The command appears here:
Get-Job gps
Examine the output from the previous command. The state reports completed when the job has completed. If data is available, the hasmoredata property reports true. Sample output appears here:
Id Name PSJobTypeName State HasMoreData Location -- ---- ------------- ----- ----------- -------- 9 gps BackgroundJob Completed True localhost
Receive the results from the job. To do this, use the Receive-Job cmdlet as shown here:
Receive-Job gps
Press the up arrow key to retrieve the Get-Job command. Run it. Note that the hasmoredata property now reports false, as shown here:
Id Name PSJobTypeName State HasMoreData Location -- ---- ------------- ----- ----------- -------- 9 gps BackgroundJob Completed False localhost
Create a new job with the same name as the previous job: gps. This time, change the -scriptblock parameter value to gsv (the alias for Get-Service). The command appears here:
Start-Job -Name gps -ScriptBlock {gsv}
Now use the Get-Job cmdlet to retrieve the job with the name gps. Note that the command retrieves both jobs, as shown here:
Get-Job -name gps
Id Name PSJobTypeName State HasMoreData Location -- ---- ------------- ----- ----------- -------- 9 gps BackgroundJob Completed False localhost 11 gps BackgroundJob Completed True localhost
Use the Receive-Job cmdlet to retrieve the job ID associated with your new job. This time, use the -keep switch, as shown here:
Receive-Job -Id 11 -keep
Use the Get-Job cmdlet to retrieve your job. Note that the hasmoredata property still reports true because you’re using the -keep switch.
This concludes this exercise.