1

I have a PowerShell 2 script to automatically register managed modules with IIS7. With PowerShell 2 I used import-module webadministration rather than the snap-in.

Is it possible to install the IIS7 web administration module (rather than the snap-in) on Window Server 2008 R1 when PowerShell 2 is installed? If so, how?

This would have the benefit of simplifying my script, otherwise, I will need to attempt to target two different server platforms.

2

Unfortunately, it isn't possible to load the IIS provider as the same thing on both 2008 and 2008R2. On 2008 the IIS provider is provided as only a snapin, and on 2008R2 it is provided as only a module.

With a little bit of coding, you can actually determine which to use, and dynamically load the module or snapin in your script, depending on which is necessary. I took this code from http://forums.iis.net/t/1166784.aspx when I was having a similar problem.

$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
    }
}

Before attempting to do anything with the IIS provider, check to ensure that $ModuleLoaded is true, and you should be good to go.

  • I wound up taking a similar approach, however, this version is much more robust than what I came up with. Thanks!! – Ryan Taylor Nov 15 '10 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.