Add-PsSnapin WebAdministration in Windows7

时间秒杀一切 提交于 2019-11-28 23:31:12

Here is the function I have in my profile.ps1 for loading the WebAdmin module. It covers IIS 7 and 7.5

# Web administration is loaded as a module on Windows 2008 R2 but as a set of snapins
# for Windows 2008 (not R2)

function Util-Load-Web-Administration
{
    $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)
    {
        try
        {
            if ((Get-PSSnapin -Registered | ForEach-Object {$_.Name}) -contains $ModuleName)
            {
                if ((Get-PSSnapin -Name $ModuleName -ErrorAction SilentlyContinue) -eq $null) 
                    { Add-PSSnapin $ModuleName }

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

        catch
        {
            Write-Error "`t`t$($MyInvocation.InvocationName): $_"
            Exit
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!