The PowerShell WebAdministration module can be finicky. If you’re seeing COMException error messages, here are a few things you could try.
WebAdministration Errors
If you import the module using the 32-bit version of PowerShell, you might see the following.
PS > Import-Module WebAdministration
PS > Get-Website
Get-Website : Retrieving the COM class factory for component with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed
due to the following error: 80040154.
At line:1 char:12
+ Get-Website <<<<
+ CategoryInfo : NotSpecified: (:) [Get-Website], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.IIs.PowerShell.Provider.GetWebsite
Command
If you import the module using the 64-bit version of PowerShell, you might see this error.
PS > Import-Module WebAdministration
PS > Get-Website
format-default : Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
+ CategoryInfo : NotSpecified: (:) [format-default], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.FormatDefaultCommand
Solutions
Enable IIS Management Service
Firstly, you should make sure the IIS Management Service is enabled. To do so, follow these steps:
1. Click Start and then click Control Panel
2. In Control Panel, click Programs and then click Turn Windows features on or off.
3. In the Windows Features dialog box, verify that IIS Management Service is checked and then click OK
Try both 32-bit PowerShell and 64-bit PowerShell
If you're on a 64-bit platform, you have both the 32-bit and 64-bit versions of PowerShell installed. In my personal experience, using 64-bit PowerShell worked for me. However, people on various internet forums have claimed that the 32-bit version of PowerShell worked for them. Try running your script from both versions to see which one works for you.
64-bit PowerShell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
32-bit PowerShell: C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
Test Script
To test each version, try running these commands and make sure you don't get errors.
Import-Module WebAdministration Get-Website