Restart IIS app pool error

Upon deployment completion, we are using the IIS AppPool - Restart step template to restart the app pool for our deployed WCF service.

We get this error during this step:

WARNING: The names of some imported commands from the module
Info 10:50:01
’Library_IISutils_635422277996919883’ include unapproved verbs that might make
Info 10:50:01
them less discoverable. To find the commands with unapproved verbs, run the
Info 10:50:01
Import-Module command again with the Verbose parameter. For a list of approved
Info 10:50:01
verbs, type Get-Verb.
Error 10:50:01
Add-PSSnapIn : An item with the same key has already been added.
Error 10:50:01
At C:\Users\octopus\AppData\Local\Tentacle\Temp\d6d02cb1-f14b-4784-a754-7e653d9
Error 10:50:01
87b9e.ps1:17 char:13
Error 10:50:01

  •         Add-PSSnapIn WebAdministration;
    

Error 10:50:01

  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

Error 10:50:01
+ CategoryInfo : NotSpecified: (:slight_smile: [Add-PSSnapin], ArgumentExcept
Error 10:50:01
ion
Error 10:50:01
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Co
Error 10:50:01
mmands.AddPSSnapinCommand
Info 10:50:02

PowerShell exit code: 1

Fatal 10:50:02
PowerShell script returned a non-zero exit code: 1

Note, I had to modify original template to work with older server / powershell 3.0. If I run this script manually outside of Octopus it seems to work without error. Not sure on why it fails in context of Octopus.

Set-StrictMode -Version Latest

Load IIS module:

Import-Module WebAdministration

http://stackoverflow.com/questions/1924217/powershell-load-webadministration-in-ps1-script-on-both-iis-7-and-iis-7-5/4325963#4325963

$iisVersion = Get-ItemProperty “HKLM:\software\microsoft\InetStp”;
if ($iisVersion.MajorVersion -eq 7)
{
if ($iisVersion.MinorVersion -ge 5)
{
Import-Module WebAdministration;
}
else
{
if (-not (Get-PSSnapIn | Where {$_.Name -eq “WebAdministration”;})) {
Add-PSSnapIn WebAdministration;
}
}
}

Get AppPool Name

$appPoolName = $OctopusParameters[‘appPoolName’]

Start App Pool if stopped else restart

if ((Get-WebAppPoolState($appPoolName)).Value -eq “Stopped”){
Write-Output "Starting IIS app pool $appPoolName"
Start-WebAppPool $appPoolName
}
else {
Write-Output "Restarting IIS app pool $appPoolName"
Restart-WebAppPool $appPoolName
}

Windows Version 6.0.6002
Powershell 3.0

please advise.

Hi,

We’ve tried many ways to load the IIS modules reliably and eventually settled on:

Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue
Import-Module WebAdministration -ErrorAction SilentlyContinue

You should be able to replace the whole of the if ($iisVersion.MajorVersion -eq 7) { ... } block with the above and the script will work on either version of IIS.

Hope that helps!

Paul

Paul, I tried the Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue
but in Win2008 R1 / POwershell 3.0, it errors with “An item with the same key has already been added.” at deployment time. The only way I was able to get this to work was to put a try/catch block around the Add-PSSnapin statement.

Hi,

That makes sense, as the snap-in was already added. You can add some error-handling logic to avoid re-loading everything:

if (!(Get-PSSnapin -Name “webadministration”))
{
Write-Output "WebAdministration snap-in not found. Adding it"
Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue
}

If (!(Get-Module -Name “webadministration”))
{
Write-Output "WebAdministration module not found. Importing it"
Import-Module WebAdministration -ErrorAction SilentlyContinue
}

Thanks!

Dalmiro

Dalmiro, I swapped out the code for your implementation. Here is the new error I get:

Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern ‘webadministration’ were found. Check the pattern
Error 18:44:34
and then try the command again.

Is it case sensitive?

Hi,

It is not case sensitive. I accidentally put the “-ErrorAction Silentlycontinue” on the wrong lines. It should be like this

if (!(Get-PSSnapin -Name “webadministration” -ErrorAction SilentlyContinue))
{
Write-Output "WebAdministration snap-in not found. Adding it"
Add-PSSnapin WebAdministration -ErrorAction Continue
}

If (!(Get-Module -Name “webadministration”))
{
Write-Output "WebAdministration module not found. Importing it"
Import-Module WebAdministration
}

Notice that I put the “-ErrorAction SilentlyContinue” at the end of the Get-M
dalmiro