PowerShell: Load WebAdministration in ps1 script on both IIS 7 and IIS 7.5

前端 未结 6 1689
猫巷女王i
猫巷女王i 2021-01-31 04:57

I have a PowerShell script that configures web site and web application settings in IIS. So I use the cmdlets in the WebAdministration snap in. But this script needs to run on

相关标签:
6条回答
  • 2021-01-31 05:08

    I ran into this problem today and here is solution I used

    Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue

    Import-Module WebAdministration -ErrorAction SilentlyContinue

    The only situation this does not work in was if the iis powershell snapin is not installed at all.

    0 讨论(0)
  • 2021-01-31 05:10

    Another way using Windows version:

    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 }
    
    0 讨论(0)
  • 2021-01-31 05:21

    This is probably a bit late to help you, but here is how we do this:

    $iisVersion = Get-ItemProperty "HKLM:\software\microsoft\InetStp";
    if ($iisVersion.MajorVersion -eq 7)
    {
        if ($iisVersion.MinorVersion -ge 5)
        {
            Import-Module WebAdministration;
        }           
        else
        {
            if (-not (Get-PSSnapIn | Where {$_.Name -eq "WebAdministration";})) {
                Add-PSSnapIn WebAdministration;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-31 05:23

    Is it possible to catch the error from one or the other, and do the opposite. Dont have my shell handy but something like:

    $succeeded = import-module WebAdministration
    if (($succeeded -ne $null) -and ($succeeded.GetType() -eq [System.Exception]) {
      #Could not import, trying to snapin
      add-pssnapin WebAdministration
    }
    

    Actually thinking about this a bit more...

    $hasSnapin = get-pssnapin | Select { $_.Name.toLower().Trim() = "webadministration" }
    if ($hasSnapin -ne $null) {
      add-pssnapin WebAdministration
    } else {
      import-module WebAdministration
    }
    

    On the first one, I know the error type check will probably need to be modified. As far as the work going on, this can actually be done in C# by looking in the registry for loaded snapins, or the IIS version installed on the machine and then use the appropriate method.

    0 讨论(0)
  • If you want to create "pre-configured" PowerShell sessions, look into PowerShell console files e.g.:

    man Export-Console -full
    

    You can create one for use on Win7 and Server 2008 R2 and another for use on Server08.

    0 讨论(0)
  • 2021-01-31 05:27

    This is great. All I had to do was add an else so it would add the snap-in when run on Windows 2008. This works in scripts for my situation.

    Function Load-WebAdmin {
      $webAdminModule = get-module -ListAvailable | ? { $_.Name -eq "webadministration" }
      If ($webAdminModule -ne $null) {
        import-module WebAdministration
      }else{
        Add-pssnapin WebAdministration
       }
    }
    
    0 讨论(0)
提交回复
热议问题