Share via


Restart IIS remotely

Question

Monday, February 3, 2014 4:18 PM

Hi,

I want to restart IIS service on a remote machine. What's the Powershell command for this, where I will be able to mention the remote server name, username, password?

All replies (4)

Monday, February 3, 2014 4:31 PM âś…Answered | 2 votes

I'm not sure if there is a specific PowerShell cmdlet (my guess is there is). The method I use is Invoke-Command. Here is a basic example:

$Server = "EXAMPLE.example.com"
$Scriptblock = {IISRESET}
$Credetial = Get-Credential

Invoke-Command -ComputerName $Server -Scriptblock $Scriptblock -Credential $Credential

Update: There are in fact IIS cmdlets for restarting IIS components. Generally restarting IIS as a whole is not the best method, you should be restarting individual application pools or websites. Check out the cmdlets Restart-WebAppPool for restarting an application pool and Restart-WebItem which you can use to restart app pools or web sites.

Update 2: Neither of these cmdlets appear to have a remote server parameter. To this I would say you would then need to use Invoke-Command or set up a PSSession if you wish to run these remotely.

Jason Warren

@jaspnwarren

jasonwarren.ca

habaneroconsulting.com/Insights


Monday, February 3, 2014 4:26 PM | 2 votes

Easiest way is to do

IISRESET Server1 /RESTART

But must be ran from a machine that has IIS installed. There are IIS cmdlets, but you would need Powershell Remoting enabled, in order to run the Invoke-Command cmdlet.

Another way is to use WMI, and connect to the root\WebAdministration namespace, and use the appropriate class. I use the WMi way, but mainly for managing Application Pools, not IIS itself.

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Don't Retire Technet


Monday, February 3, 2014 4:34 PM | 2 votes

Here's what I use; In fact, I used it this weekend. It's helpful for doing an IIS reset on several servers at once where the servers are pulled from an AD group.

$Servers = (Get-ADGroupMember -Identity 'AppServers').Name | sort Name
Invoke-Command -ComputerName "$Servers.mydomain.com" -ScriptBlock {
    iisreset.exe
}

You could add to it by sleeping the script for a minute, then iterating through the servers returning the 10 newest entries in each of the servers' system logs. This would help you verify that IIS had in fact been restarted.


Monday, February 3, 2014 4:45 PM | 2 votes

Both Tommy and Jason's methods assume the PSRemoting is enabled and configured in your environment, but Jason states a good fact, in that you should be working with individual application pools or web sites. I have the below which works with Applicaiton Pools and uses WMI, which doesnt require PSRemoting

# Stops and restarts the specified application pool on the specified machine
Function RecycleAppPool
{
    param
    (
        [string]$serverName,
        [string]$applicationPool
    )
    
    # Return an app pool object
    $aPool = Get-WmiObject -Authentication PacketPrivacy -Impersonation Impersonate -ComputerName $serverName `
            -Namespace "root\WebAdministration" -Class "ApplicationPool" | Where-Object { $_.Name -eq "$applicationPool" }
    # Check to make sure we actually have an object
    If ($aPool -ne $null)
    {   
        If ((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Started")
        {
            Write-Host -ForegroundColor Green "Attempting to Stop Application Pool: $applicationPool on Server: $server"
            $aPool.Stop()
            
            $stopper = 0
            While (((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Stopping"))
            {
                Write-Host -ForegroundColor Yellow "Stopping...."
                $stopper++
                Start-Sleep 5
                
                If ($stopper -eq 10)
                {
                    Write-Host -ForegroundColor Red "There was an issue with stopping the Application Pool $applicationPool."
                    exit
                }
            }
        
            If ((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Stopped")
            {   
                Write-Host -ForegroundColor Green "Application Pool: $applicationPool on Server: $server has been stopped"
            }
        }
    
        If ((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Stopped")
        {
            Write-Host -ForegroundColor Green "Attempting to Start Application Pool: $applicationPool on Server: $server"
            $aPool.Start()
        
            If ((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Started")
            {   
                Write-Host -ForegroundColor Green "Application Pool: $applicationPool on Server: $server has been started"
            }
        }
    }
    Else
    {
        Write-Host -ForegroundColor Red "There was an error trying to recycle app pool: $applicationPool on server: $serverName, " `
            + "please make sure you typed everything correctly"
    }
}

# App Pool state is returned as an integer, so this function converts the integer into the respected string value
Function ConvertAppPoolState
{
    param
    (
        [int]$value
    )
    
    switch ($value)
    {
        0 { "Starting"; break }
        1 { "Started"; break }
        2 { "Stopping"; break }
        3 { "Stopped"; break }
        default { "Unknown"; break }
    }
}

You then call the main function of RecycleAppPool by:

RecycleAppPool ServerName AppPoolName

And that should stop and then start the application pool specified on the server that was specified.

EDIT: Also be aware that you will see some errors in the Application Event Log, about the Packet Privacy, needing to be set to PacketPrivacy, when indeed it is. I am not sure if this is a bug with Get-WMIObject, or where the bug is, but I just ignore them.

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Don't Retire Technet