【问题标题】:How to update existing IIS 6 Web Site using PowerShell如何使用 PowerShell 更新现有的 IIS 6 网站
【发布时间】:2011-01-15 16:43:02
【问题描述】:

我正在尝试创建一个 PowerShell 脚本来创建一个新的 IIS 6 网站并设置应用程序池、通配符应用程序映射、ASP.NET 版本等内容。

在 Internet 上进行大量搜索后,我找到了一个脚本,它允许我创建一个新网站,但不能修改我需要的所有属性。

$newWebsiteName = "WebSiteName"  
$newWebsiteIpAddress = "192.168.1.100"  
$newWebSiteDirPath = "c:\inetpub\wwwroot\WebSiteName"  
$iisWebService  = Get-WmiObject -namespace "root\MicrosoftIISv2" 
                                -class "IIsWebService"  
$bindingClass = [wmiclass]'root\MicrosoftIISv2:ServerBinding'  
$bindings = $bindingClass.CreateInstance()  
$bindings.IP = $newWebsiteIpAddress  
$bindings.Port = "80"  
$bindings.Hostname = ""  
$result = $iisWebService.CreateNewSite
               ($newWebsiteName, $bindings, $newWebSiteDirPath)  

非常感谢有关如何扩展上述示例的任何帮助。

【问题讨论】:

  • 不好意思,具体需要修改哪些属性?

标签: powershell web iis-6 wmi


【解决方案1】:

首先,非常感谢 jrista 为我指明了正确的方向。

我还发现这个article 非常有用。

以下是用于创建应用程序池、网站和 SelfSsl 证书的 powershell 脚本:

函数 CreateAppPool ([string]$name, [string]$user, [string]$password) { # 检查池是否存在并将其删除 - 用于测试目的 $tempPool = gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPoolSetting" -filter "Name like '%$name%'" if (!($tempPool -eq $NULL)) {$tempPool.delete()} # 创建应用程序池 $appPoolSettings = [wmiclass] "root\MicrosoftIISv2:IISApplicationPoolSetting" $newPool = $appPoolSettings.CreateInstance() $newPool.Name = "W3SVC/AppPools/" + $name $newPool.WAMUsername = $user $newPool.WAMUserPass = $password $newPool.PeriodicRestartTime = 1740 $newPool.IdleTimeout = 20 $newPool.MaxProcesses = 1 $newPool.AppPoolIdentityType = 3 $newPool.Put() } 函数 CreateWebSite ([string]$name, [string]$ipAddress, [string]$localPath, [string] $appPoolName, [string] $applicationName) { # 检查网站是否存在并将其删除 - 用于测试目的 $tempWebsite = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'" if (!($tempWebsite -eq $NULL)) {$tempWebsite.delete()} # 将网站切换到 .NET 2.0 C:\windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -sn W3SVC/ $iisWebService = gwmi -namespace "root\MicrosoftIISv2" -class "IIsWebService" $bindingClass = [wmiclass]'root\MicrosoftIISv2:ServerBinding' $bindings = $bindingClass.CreateInstance() $bindings.IP = $ipAddress $bindings.Port = "80" $bindings.Hostname = "" $iisWebService.CreateNewSite($name, $bindings, $localPath) # 分配应用程序池 $webServerSettings = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'" $webServerSettings.AppPoolId = $appPoolName $webServerSettings.put() # 添加通配符映射 $wildcardMap = "*, c:\somewildcardfile.dll, 0, All" $iis = [ADSI]"IIS://localhost/W3SVC" $webServer = $iis.psbase.children |其中 { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $name } $webVirtualDir = $webServer.children |其中 { $_.keyType -eq "IIsWebVirtualDir" } $webVirtualDir.ScriptMaps.Add($wildcardMap) # 设置应用名称 $webVirtualDir.AppFriendlyName = $applicationName # 保存更改 $webVirtualDir.CommitChanges() # 启动新创建的网站 if (!($webServer -eq $NULL)) {$webServer.start()} } 函数 AddSslCertificate ([string] $websiteName, [string] $certificateCommonName) { # 这个方法需要你的机器上有selfssl $selfSslPath = "\程序文件\iis 资源\selfssl" $certificateCommonName = "/N:cn=" + $certificateCommonName $certificateValidityDays = "/V:3650" $websitePort = "/P:443" $addToTrusted = "/T" $quietMo​​de = "/Q" $webServerSetting = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '$websiteName'" $websiteId ="/S:" + $webServerSetting.name.substring($webServerSetting.name.lastindexof('/')+1) cd -path $selfSslPath .\selfssl.exe $addToTrusted $certificateCommonName $certificateValidityDays $websitePort $websiteId $quietMo​​de } $myNewWebsiteName = "测试网站" $myNewWebsiteIp = "192.168.0.1" $myNewWebsiteLocalPath = "c:\inetpub\wwwroot\"+$myNewWebsiteName $appPoolName = $myNewWebsiteName + "AppPool" $myNewWebsiteApplicationName = "/" $myNewWebsiteCertificateCommonName = "mynewwebsite.dev" CreateAppPool $appPoolName "管理员" "密码" CreateWebSite $myNewWebsiteName $myNewWebsiteIp $myNewWebsiteLocalPath $appPoolName $myNewWebsiteApplicationName AddSslCertificate $myNewWebsiteName $myNewWebsiteCertificateCommonName

【讨论】:

  • 唯一的更改是在删除应用程序池之前删除网站,否则会出错(将删除分解为自己的函数)
【解决方案2】:

$result 对象包含新创建的 IIsWebServer 对象的路径。您可以通过执行以下操作来访问虚拟目录,您可以在其中配置更多属性:

$w3svcID = $result.ReturnValue -replace "IIsWebServer=", ""
$w3svcID = $w3svcID -replace "'", ""
$vdirName = $w3svcID + "/ROOT";

$vdir = gwmi -namespace "root\MicrosoftIISv2" 
             -class "IISWebVirtualDir" 
             -filter "Name = '$vdirName'";
# do stuff with $vdir
$vdir.Put();

【讨论】:

    【解决方案3】:

    这是一个有用的 PowerShell sn-p。

    我尝试运行它,但删除测试出现问题。当站点仍然存在时,删除对应用程序池不起作用。当然你应该先运行网站删除测试。

    # check if web site exists and delete it - for testing purposes
    $tempWebsite  = gwmi -namespace "root\MicrosoftIISv2" 
                         -class "IISWebServerSetting" 
                         -filter "ServerComment like '%$name%'"
    if (!($tempWebsite -eq $NULL)) {$tempWebsite.delete()}
    

    先运行此程序,然后运行应用程序池删除测试。
    我知道您已将这些标记为测试,但如果网站存在,退出或删除肯定很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      相关资源
      最近更新 更多