我使用VS2010、PowerShell v2.0、Windows Server2008Web标准来为web应用程序创建部署脚本ps1 (IIS7)。
我想使用Powershell以编程方式管理IIS7(网站、appPools、virtualDirs等)。
我对使用Powershell管理IIS的几种方法感到困惑。
推荐的方式是什么?
1)。使用Microsoft.Web.Administration.dll
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")2)。根据操作系统版本或IIS版本(?),使用Import-Module与Add-PSSnapin
检测操作系统版本:
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 }检测IIS版本
$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;
}
}
}模块作为管理单元加载和加载:
$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
}
}参考文献:
http://forums.iis.net/t/1166784.aspx/1
PowerShell: Load WebAdministration in ps1 script on both IIS 7 and IIS 7.5
发布于 2013-07-15 10:03:47
由于您说您使用的是Server2008Module和Powershell V2或更高版本,因此导入模块是首选方法。模块比管理单元更强大,并且添加了更多的功能。然而,最大的区别是模块不需要注册(添加到注册表中)。
Snapins是Powershell V1的遗留特性,它不支持模块。Server2008(原始配方)没有开箱即用的V2,仍然使用针对IIS、ActiveDirectory、Exchange等的管理单元
Server2008Server包含Powershell V2,因此可以使用模块。
它归结为以下几点:
if (YourSystems are all Server 2008 R2) {
Import the module and ignore the rest.
} elseif (One or more of the servers being managed is still on 2008 (original)) {
Use the snapin.
}https://stackoverflow.com/questions/14874366
复制相似问题