Powershell Get-WMIObject fails with “Invalid Namespace” error

iis-7powershellwindows-server-2008

Here's the situation: I'm trying to use PowerShell to automatically recycle an AppPool (Windows Server 2008 with IIS7). I'm attempting to do this with the PowerShell script found here:

$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" 
                         -class "IIsApplicationPool" 
                  | Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"}
$appPool.Recycle()

However, this results in a 'Invalid Namespace' error:

get-wmiobject namespace error

I googled around and found that I may have been missing the "IIS Management Scripts and Tools" role service which I have since set up for IIS.

How can I fix this issue, or alternatively, can I recycle an AppPool with Powershell without WMI?

Edit

  • To clarify this is Windows Sever 2008 Enterprise without Hyper-V (SP2) x64.

  • Running the above command in the x64 version of Powershell results in the same error.

  • Running Add-PSSnaping WebAdministration from 32bit:

    Add-PSSnapin : The Windows PowerShell snap-in 'WebAdministration' is not installed on this machine.

  • And in the 64bit environment:

    Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 2.

  • (64bit) Get-PSSnapin -Registered returns nothing

  • (32bit) Get-PSSnapin -Registered returns Windows.ServerBackup, PSVersion 1.0

Best Answer

Try http://technet.microsoft.com/en-us/library/ee807823.aspx

add-pssnapin WebAdministration
Restart-WebItem 'IIS:\AppPools\DefaultAppPool'