Import-Module WebAdministration error [Answered]RSS

6 replies

Last post Dec 05, 2010 04:50 PM by MattDuguid

  • Import-Module WebAdministration error

    Apr 13, 2010 02:23 PM|Mozleron|LINK

    I downoladed and installed the x64 version of the IIS Snap-in found on this site.

    If i use the shortcut provided by the installer, i can access IIS:.  If i open a Powershell window using the orginal shortcut, i can not access that drive.  Also, when i run Import-Module WebAdministration, i get the following 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

    If i run & $env:programfiles\IIS\PowerShellSnapin\iisConsole.psc1, I get a new window that pops up with access to the IIS: drive.  This would work if i were doing things manually, but i am using an internal remote deployment tool that can run scripts in silent mode, and popping up new windows is of no use in that situation.

    I used all the default settings during install.  
    Is there a way to make the snap-in show up as part of the default Powershell environment? 
    If not, how do i get the Import-Module command to work?

    Thanks,
    Mozleron

    PowerShell IIS 7.0 snap-in

  • Re: Import-Module WebAdministration error

    Apr 13, 2010 04:27 PM|jrjespersen|LINK

     I'm assuming since you downloaded and installed it, you're running Server 2003 or Server 2008 (not Server 2008 R2).  If that's the case, you wouldn't use Import-Module; you need to use Add-PSSnapin WebAdministration.

    To have it load automatically, you can add the Add-PSSnapin command to your personal or the system profile.

  • Re: Import-Module WebAdministration error

    Apr 13, 2010 05:13 PM|Mozleron|LINK

    I am using Server 2008 x64 SP2.  Using the Add-PSSnapin seem to have done the trick.

    Does the Add-PSSnapin only work on certain Operating Systems?  If so, is there a quick and easy way to discriminate on the OS so that only the correct Import command is used?

  • Re: Import-Module WebAdministration error

    Apr 14, 2010 09:30 AM|jrjespersen|LINK

    Snapins were the way to extend powershell functionality in PowerShell v1.  Modules supercede snapins as the path for extensibility in PowerShell v2.  Add-PSSnapin will work across operating systems provided that what you are trying to load is a snapin.  If it's a module, you must use Import-Module.

    In this case, the IIS provider is provided as a module on Windows 2008 R2.  On Windows 2008, it is a snapin.

    Here is a snippet that I wrote this week just for this reason - it's not been thoroughly tested, but has been working without issue for me so far.

    $ModuleName = "WebAdministration"
    $ModuleLoaded = $false
    $LoadAsSnapin = $false

    if ($PSVersionTable.PSVersion.Major -ge 2)
    {
        if ((Get-Module -ListAvailable | ForEach-Object {$_.Name}) -contains $ModuleName)
        {
            Import-Module $ModuleName
            if ((Get-Module | ForEach-Object {$_.Name}) -contains $ModuleName)
            {
                $ModuleLoaded = $true
            }
            else
            {
                $LoadAsSnapin = $true
            }
        }
        elseif ((Get-Module | ForEach-Object {$_.Name}) -contains $ModuleName)
        {
            $ModuleLoaded = $true
        }
        else
        {
            $LoadAsSnapin = $true
        }
    }
    else
    {
        $LoadAsSnapin = $true
    }

    if ($LoadAsSnapin)
    {
        if ((Get-PSSnapin -Registered | ForEach-Object {$_.Name}) -contains $ModuleName)
        {
            Add-PSSnapin $ModuleName
            if ((Get-PSSnapin | ForEach-Object {$_.Name}) -contains $ModuleName)
            {
                $ModuleLoaded = $true
            }
        }
        elseif ((Get-PSSnapin | ForEach-Object {$_.Name}) -contains $ModuleName)
        {
            $ModuleLoaded = $true
        }
    }

  • Re: Import-Module WebAdministration error

    Apr 14, 2010 11:33 PM|Mozleron|LINK

     jrjesperse, Thank you for the clear definition and explanation. 

    I will certainly be able to incorporate what you have laid out here in the project I'm working on.

  • Re: Import-Module WebAdministration error

    Nov 29, 2010 09:24 AM|suchiwa|LINK

    Hi All,

    After download the snapin in a folder, can we add a "PsModulePath" environment path and his physical path (to folder where is the snapin) like we have on Windows 2008 Server R2 ?

    This is a snapin and not a module, so we don't have a .psm1 to change, anyway, it's just a suggestion...

     

    R2 psmodulepath psm1

  • Re: Import-Module WebAdministration error

    Dec 05, 2010 04:50 PM|MattDuguid|LINK

    Another way using Windows version...works well for us as we have a mixture of Windows 2008 and Windows 2008 R2 servers

    if ([System.Version] (Get-ItemProperty -path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion").CurrentVersion -ge [System.Version] "6.1") { Import-Module WebAdministration } else { Add-PSSnapin WebAdministration }