The specified module 'WebAdministration' was not loaded (Windows 2003, IIS 6)

5.7k Views Asked by At

I am trying to run some IIS admin scripts on machine with -

OS - Windows 2003(with SP2)
IIS - V6.0
Powershell - V2

However when I run following commands, I get the error -

- Import-Module WebAdministration

  **Error**:  
    Import-Module : The specified module 'WebAdministration' was not loaded because no valid module file was found in any module directory.
    At line:1 char:14 + Import-Module <<<<  WebAdministration
        + CategoryInfo          : ResourceUnavailable: (WebAdministration:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

- Add-PSSnapIn WebAdministration

  **Error:**  
      Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 2.
        At line:1 char:13 + Add-PSSnapIn <<<<  WebAdministration
        + CategoryInfo          : InvalidArgument: (WebAdministration:String) [Add-PSSnapin], PSArgumentException
        + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

I checked which modules/snapin are available - here is the result -

Get-Module -ListAvailable

Result:
    BitsTransfer

Get-PSSnapIn

Result:
  Microsoft.PowerShell.Diagnostics
  Microsoft.WSMan.Management  
  Microsoft.PowerShell.Core
  Microsoft.PowerShell.Utility
  Microsoft.PowerShell.Host
  Microsoft.PowerShell.Management
  Microsoft.PowerShell.Security

Please guide what shall I do to run IIS administration scripts.

3

There are 3 best solutions below

0
On

Below link says Powershell SnapIn is not available for IIS 6.0:

http://forums.iis.net/p/1156851/1903821.aspx#1903821

WMI is the option to go with IIS 6.0 administration.
But WMI is not the option for me as soon we will be upgrading to IIS 7.5

0
On

In IIS6 on Windows 2k3 platform, I suggest you try accessing IIS via the old WMI provider ("Microsoftiisv2") or ADSI provider as both are accessible from the PowerShell.

Neither snapins nor WebAdministration module is available for IIS 6.0, so we can access IIS6 metabase from PowerShell using either

For IIS7.0, we can "import WebAdministration" module.

For example, I had to set the physical path for a virtual directory for IIS6, so I made use of a vbs script,iisvdir that comes along with IIS6 in c:/Windows/System32 .

Copying the code snippet

Function resetSiteLocation ($newPath)
{     

Write-Host "List of Virtual directories for the site Test123 before reset :"

C:\WINDOWS\system32\iisvdir /query Test/Test123

Write-Host "About to reset site location"

C:\WINDOWS\system32\iisvdir /delete Test/Test123/Test1

C:\WINDOWS\system32\iisvdir /create Test/Test123 Test1 C:\projects\Test\Test123\Test1

C:\WINDOWS\system32\iisvdir /delete Test/Test123/Test2

C:\WINDOWS\system32\iisvdir /create Test/Test123 Test2 C:\projects\Test\Test123\Test2

Write-Host "Finished to reset site location"

Write-Host "List of Virtual directories for the site Test123 after reset :"

C:\WINDOWS\system32\iisvdir /query Test/Test123

}

Since you would be making a switch to higher version of IIS, you could put a switch in your code to determine the IIS version and take action as appropriate.

I did this:

Write-Host "Checking Installed IIS version:"

$iisVersion = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\InetStp";

Write-host IIS major version : $iisVersion.MajorVersion

Write-host IIS minor version : $iisVersion.MinorVersion

Write-Host "Finished the check."
## IIS inclusion module
## Neither snapins nor WebAdministration module is available for IIS 6.0, so we can access IIS6 metabase 
## from PowerShell using either old WMI provider ("Microsoftiisv2") or ADSI provider as both are accessible from the PowerShell.
## For IIS7.0, we can import WebAdministration module

if (($iisVersion.MajorVersion -eq 7 ) -or ($iisVersion.MajorVersion -ge 7 ))
{
Write-host Detected IIS Major Version : $iisVersion.MajorVersion and Minor version : $iisVersion.MinorVersion. Hence importing WebAdministration module.
Import-Module WebAdministration;


Write-Host "About to reset app pool"
Restart-WebAppPool("Application")
Write-Host "Finished resetting app pool" 
resetSiteLocation

Write-Host "About to reset site"
Restart-WebItem("IIS:\Sites\My application")
Write-Host "Finished to reset site"

}
elseif ($iisVersion.MajorVersion -eq 6) 
{
Write-host IIS version 6 detected. Hence accessing IIS metabase using old WMI provider 

##2. Reset App Pool
Write-Host "About to reset app pool"

Write-Host "Finished resetting app pool" 

##3. Reset site location
resetSiteLocation

##4.Reset site
Write-Host "About to reset site"

Write-Host "Finished to reset site"    
}
else
{
     Write-host Detected IIS $iisVersion.MajorVersion         
}

Let me know if it helps you.

0
On

Here is some good information on using the WMI interface with IIS 6:

http://network-nick.blogspot.com/2015/01/powershell-and-iis-6.html

He also points to the Microsoft documentation of cmdlets for this environment, here:

https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms525265(v=vs.90)

During the article he develops and explains the following PowerShell script for listing a server's web sites and their virtual directories. I actually tried this and it works.

$WebSiteID = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServer | Select-Object -ExpandProperty Name

ForEach ( $Site in $WebSiteID ) {
    $WebSiteName = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebServerSetting | Where-Object { $_.Name -like "$site" } `
                    | Select-Object -Expandproperty ServerComment

    write-host "`r`n" $WebSiteName

    $AppPath = Get-WmiObject -Namespace "root/MicrosoftIISv2" -Class IIsWebVirtualDirSetting | Where-Object { $_.Name -like "$site/*" } `
                | select -expandproperty path

    $AppPath = $AppPath | select-object -unique | sort-object
    $AppPath
}