Reputation: 665
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.
Upvotes: 39
Views: 124963
Reputation: 75
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.Upvotes: 2
Reputation: 2085
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.
Upvotes: 13
Reputation: 61
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
Upvotes: 1
Reputation: 1108
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}
Upvotes: 45
Reputation: 27
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.
Upvotes: 3
Reputation: 7
I found using the simple command below the easiest.
D:\PS\psexec \server_name iisreset
Upvotes: -3
Reputation: 2301
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.
Upvotes: 1
Reputation: 1625
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
Upvotes: 0