39

Does anyone know how to perform IISRESET with a PowerShell script? I'm using the PowerGUI editor with PowerShell 1.0 installed on a Windows 2008 box.

1
  • any final solution with full source code sample working about it ? Commented Jul 12, 2013 at 10:16

8 Answers 8

45

You can do it with the Invoke-Command cmdlet:

invoke-command -scriptblock {iisreset}

UPDATE:

You can also simplify the command using the & call operator:

& {iisreset}

Sign up to request clarification or add additional context in comments.

Comments

13

Having used & {iisreset} with occasional failure lead me to this:

Start-Process "iisreset.exe" -NoNewWindow -Wait

Now it waits for iisreset.exe to end gracefully.

Comments

3

This works well for me. In this application, I don't care about the return code:

Start-Process -FilePath C:\Windows\System32\iisreset.exe -ArgumentList /RESTART -RedirectStandardOutput .\iisreset.txt
Get-Content .\iisreset.txt | Write-Log -Level Info

The Write-Log cmdlet is a custom one I use for logging, but you could substitute something else.

Comments

2

iisreset.exe supports computer names as a parameter. An example below shows basic idea how to reset IIS on multiple servers:

$servers = @()
$servers += 'server1'
$servers += 'server2'
...
$servers += 'serverN'

Since iisreset.exe doesn't support multivalued parameters we have to wrap it in a loop:

$servers | %{ iisreset $_ /restart /noforce }

You may want to add simple monitoring:

$servers | %{ Write-Host "`n`n$_`n" -NoNewline ; iisreset $_ /restart /noforce /timeout:30 }

If you have many servers you may be interested in failures only:

$servers | %{ Write-Host "`n`n$_`n" -NoNewline ; iisreset $_ /restart /noforce /timeout:30 | Select-String "failed" }

Multiline version for better readability:

foreach ( $server in $servers ) {
    Write-Host "`n`n$server`n" -NoNewline ; 
    iisreset $server /restart /noforce /timeout:30 | Select-String "failed"
}

I would strongly recommend testing your script with /status before implementing /reset action:

$servers | %{ iisreset $_ /status }

You may check stopped components with /status as well:

$servers | %{ Write-Host "`n`n$_`n" -NoNewline ; iisreset $_ /status | Select-String "Stopped" }

Reference

  • /restart is the default parameter. iisreset.exe is using /restart in case no other actions params specified
  • /noforce will prevent iisreset.exe from running in case of an error.
  • /timeout - sometime you need to allow server more time to process request to avoid IIS stuck in Stopped state.

Comments

1

I know that this is very old, but you can run any command line processes from Powershell's command line. So you would just need a script that calls IISReset with whatever switches you need.

Comments

1

IIS Stop or Start (tested)

WaitForExit and ExitCode work fine

[System.Reflection.Assembly]::LoadWithPartialName("System.Diagnostics").FullName
$procinfo = New-object System.Diagnostics.ProcessStartInfo
$procinfo.CreateNoWindow = $true
$procinfo.UseShellExecute = $false
$procinfo.RedirectStandardOutput = $true
$procinfo.RedirectStandardError = $true
$procinfo.FileName = "C:\Windows\System32\iisreset.exe"
$procinfo.Arguments = "/stop"
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo = $procinfo
[void]$proc.Start()
$proc.WaitForExit()
$exited = $proc.ExitCode
$proc.Dispose()

    Write-Host $exited

Comments

0

Not sure what you are looking for exactly, but create a script with a body of "iisreset /noforce"

Here's an example: http://technet.microsoft.com/en-us/library/cc785436.aspx

Comments

-3

I found using the simple command below the easiest.

D:\PS\psexec \server_name iisreset

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.