Friday, September 13, 2013

Resetting IIS remotely using Powershell script

While working on a project I come across a scenario where I wanted to reset the IIS remotely. I searched on internet looking for the best and easiest solution. After spending in some time I found out that powershell script would be best and easiest way to achieve my task.Steps explained below will explain how can we achieve this.

In order to remotely connect we need to make sure firewall is not blocking the port on which we are trying to connect so first thing to do is run the following command on server as well as your machine to get the port open for communication.


 To enable that follow below steps;

1- Open Windows PowerShell command window (Start -> Accessories -> Windows PowerShell)
2- Execute following command


winrm quickconfig
Press y when it asks for confirmation.


$server="IRVDIGWEB22"

invoke-command -computername $server -scriptblock {iisreset} -Credential (Get-Credential)

The code above simply connect to server first and then reset the IIS on it. But for connection we need to have to authenticate that we have rights to the server so for that we need to provide credentials of server.

invoke-command -computername $server -scriptblock {iisreset} -Credential (Get-Credential)

Credential (Get-Credential) here prompts for credentials 
$server is the name of server that has IIS you want to reset.
iisreset is the command you want to perform on remote machine.


I hope that would help you. Sample Script File can be downloaded here