Controlling IIS7 remotely with PowerShell
Our deployment script needed to do some basic IIS administrative tasks remotely on a Windows 2008 (non-R2) server, which runs IIS7, recently. Finding the information and fiddling around with it took me a good day and a half, so I thought I’d post the steps here to help someone else (more than likely myself) in the future:
- Download the Windows Management Framework Core package for your setup
- If your machine is something older than Windows 7 or Server 2008 R2, you’ll need to get the PowerShell IIS7 Snap-In
-
If your workstation/build server and target web servers happen to be on different Windows domains, you’ll need to run this one time on each client machine:
Set-Item WSMan:\localhost\Client\TrustedHosts *
-
Run this command once on each server:
winrm quickconfig
-
You’ll need to load the PowerShell Snap-In once on each client, which differs depending on which version of Windows you’re running. Anything older than Windows 7 or Server 2008 R2:
Add-PSSnapin WebAdministration
Windows 7 and Server 2008 R2 run:
Load-Module WebAdministration
-
Check if it’s working properly by running this command on any version of Windows (you should see the IIS7 Snap-In listed):
Get-Module -ListAvailable
- Get credentials for accessing the remote server
-
Start running some remote commands on your web servers:
Invoke-Command -ComputerName “webserver_computerName” -Credential $credentials_from_last_step -ScriptBlock { Add-PSSnapin WebAdministration; Stop-Website IIS_Site_Name }
It’s not really that hard once you get the proper packages installed and the permissions worked out, and since it’s so powerful and useful for scripting purposes it’s well worth the trouble. The available commands are awesome for use in automated deployment scripts.
You can learn more about the PowerShell Snap-In provider here, and at its download site here.