Prof. Powershell

PowerShell 2.0 Remoting

WMI is no longer necessary for managing remote computers with PowerShell. Here's how to extend your management grasp.

In PowerShell 1.0, working with remote computers meant using Windows Management Instrumentation (WMI), fussing with firewall ports and troubleshooting RPC connectivity. PowerShell 2.0 will allow you to reach out and manage remote computers in a variety of ways from the relative comfort of your desk. But of course there are requirements.

First off, you'll need PowerShell 2.0 installed not only on your desktop, but also any remote computer you want to manage. Initially, you'll be limited to Windows 7 and Window Server 2008 R2, but eventually everything after Windows XP should be supported. You'll also need to install Windows Remote Management (WinRM) 2.0. These bits should ship with Windows 7 and Server 2008 R2. Most likely they will be integrated in later PowerShell 2 installation packages for other operating systems. While technically not required, you'll find WinRM easier to set up and use in a domain environment. As you might expect Vista and newer systems will require administrative privileges in an elevated session.


Configuring a computer for remoting is as simple as opening an elevated PowerShell prompt and running a simple command:

PS C:\> enable-psremoting

That's it! The cmdlet handles all the WinRM service configuration, including firewall exceptions. Look at the cmdlet's help to see what actions will be performed on your system. If you later decide to stop using remoting, you can disable WinRM like this:

PS C:\> disable-psremoting

To verify that the remote protocols are working properly, use the Test-WSMan cmdlet:

PS C:\> test-wsman

You'll also be able to use Group Policy on a Windows Server 2008 domain controller to configure WinRM. Once remoting has been enabled, you;ll be able to run sessions interactively on remote computers. You'll be able to run commands as background jobs on remote computers and retrieve the results on your computer. We'll cover these techniques and more in future columns and you'll wonder how you ever managed without PowerShell before.

In the mean time, you can learn more about WinRM and other Windows infrastructure technologies at http://blogs.msdn.com/wmi/.


About the Author

Jeffery Hicks is a Microsoft MVP and an IT veteran with almost 20 years of experience, much of it spent as an IT consultant specializing in Windows server technologies. He works today as an independent author, trainer and consultant. His latest book is Managing Active Directory with Windows PowerShell 2.0: TFM (SAPIEN Press 2011). Follow Jeff on Twitter and on his blog.

Reader Comments:

Thu, Jun 10, 2010 Ismail U.S.A

Hi ,
        I am attaching a sample script  of what we are trying to do.
1.we are using powershell 2.0 new remoting  feature  with fan-out model.
2.The issue we are facing is how to check if the remote command (invoke-command) has successfully executed the function on the remote computers. we want to check each of the computer independently and then log the error(including the machine name it failed on and error message) ,stop the script etc....
3.The function we have written for this purpose(checkforerrors) in the attached file is randomly working sometimes and not sometimes.

I would like to know if their is anyway we can achieve this.(it can be even without using that function). If you can get me a suggestion on this, that would be great.

Thanks in advance.function CreateAppPool {
param (
[string]$domain,
[string]$poolName
)
Add-PSSnapIn webAdministration;

if (-not (test-path IIS:\AppPools\$poolName)) {
New-WebAppPool -name $poolName
Set-ItemProperty IIS:\AppPools\$poolName -name managedPipelineMode -value 0 ### 0=Integrated, 1=Classic
Set-ItemProperty IIS:\AppPools\$poolName -name processModel -value @{userName="$domain\xxxxx";password="yyyyyy";identitytype=3}
}

}


function checkforErrors {
param (
[string]$allsession,
[string]$errorMsg
)

foreach ($session in $allsession)
{
if($errorMsg -ne $null)
{
$Machineinfo = Get-PSSession ($machine)
#$errorlist = $Machineinfo += $errorMsg
tee-object -file "e:\temp\debug.ps1" -InputObject $errorlist
Remove-PSSession *
write-host("exited out because of error")
exit 1
}
else{
Write-Host("Success")
}
}
}
################Start place for execution of script##################

##############
### Main#####
##############


#These will be the machines on which "CreateAppPool" function will be executed remotely
$allmachines = "computer1,computer2"

#----Here we are adding the sessions created for each machine in allsession variable
foreach ($machine in $allmachines) {

# creating the persistent sessions for all the machines
#$allsession = New-PSSession -ComputerName $target -Name $target
$allsession = New-PSSession -Name $target -ComputerName $target
}

#####IMPORTANT#######
#Powershell remoting using fan-out model
Invoke-Command -Session $allsession -ScriptBlock $function:CreateAppPool -ArgumentList $domain,$poolName -ErrorVariable errorMsg
##This is the function we are plannig to use if the execution of the above function errored out while execution on remote machines
checkforErrors $allsession $errorMsg

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above