Powershell, IIS and driving myself nuts


I was asked to come up with a script to recycle the appPools on a variety of Web Servers
with a large number of Web Sites. Some of these servers are IIS6 and some are IIS7 or IIS7.5.
I had to come up with two separate functions because the names of things changed between IIS6 and IIS7.

IIS 6

function Recycle6 {  $strServer = “MyServer”
  $pool= “MyAppPool”
  $appPool = get-wmiobject -namespace "root\MicrosoftIISv2 -class "IIsApplicationPool" -computername $strServer -authentication pktPrivacy |
  where-object {$_.Name -eq "W3SVC/AppPools/$Pool"}
$appPool.Recycle()
Start-Sleep 2
$appPoolState = ([ADSI] "IIS://$strServert/W3SVC/AppPools/$Pool").AppPoolState
switch($appPoolState) {
	1 { $strState = "starting" }
	2 { $strState = "started" }
	3 { $strState = "stopping" }
	4 { $strState = "stopped"	}
	default { $strState = "Unknown"	}
}
	$strDate = Get-Date
	Write-Host "$strServer $pool $strState on $strDate"
If ($error) {
	Write-Host $Error }
	}

How about IIS 7 ?

function Recycle7 {
	$strServer = “MyServer”
	$pool= $combobox1.Text
	$namespace = "root\WebAdministration"
   Write-Host  "$strServer $pool $Namespace"
$strFile ="\\myServer\E$\scripts\ps1\RestartAppPool.log"
Get-WmiObject -Namespace 'root\webadministration' -Class ApplicationPool -ComputerName  $strServer -Authentication 6 -Filter "Name= $pool" |
Invoke-WmiMethod -Name Recycle
$a = Get-WmiObject -class ApplicationPool -computername $strServer -namespace $namespace -authentication 6 -Filter "Name = $pool"
$appPoolState = $a.GetState().ReturnValue
switch($appPoolState) {
	1 { $strState = "starting" }
	2 { $strState = "started" }
	3 { $strState = "stopping" }
	4 { $strState = "stopped"	}
	default { $strState = "Unknown"	}
		}
	$strDate = Get-Date
	Write-Host "$strServer $pool $strState on $strDate"
}

Three things jump out at you when you study these functions:

IIS 6 vs. IIS7

1. $namespace = root\MicrosoftIISv2 vs. -namespace “root\WebAdministration
2. -class IIsApplicationPool vs. –class ApplicationPool
3. -authentication pktPrivacy vs. -authentication 6 ( 6 is PacketPrivacy )

Advertisements

4 thoughts on “Powershell, IIS and driving myself nuts

    • Hi, I was using PowershelStudio and the combobox is part of the form I created.
      Here is the code it generated:

      function Call-ComboBox_pff {

      #----------------------------------------------
      #region Import the Assemblies
      #----------------------------------------------
      [void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
      [void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
      [void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
      [void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
      [void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
      [void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
      [void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
      [void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
      [void][reflection.assembly]::Load("System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
      #endregion Import Assemblies

      #----------------------------------------------
      #region Generated Form Objects
      #----------------------------------------------
      [System.Windows.Forms.Application]::EnableVisualStyles()
      $form1 = New-Object 'System.Windows.Forms.Form'
      $combobox1 = New-Object 'System.Windows.Forms.ComboBox'
      $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
      #endregion Generated Form Objects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s