如何使用PowerShell更新现有的IIS 6网站

时间:2010-02-11 11:54:02

标签: powershell web iis-6 wmi

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

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

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

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

3 个答案:

答案 0 :(得分:9)

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

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

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


function CreateAppPool ([string]$name, [string]$user, [string]$password)
{
    # check if pool exists and delete it - for testing purposes
    $tempPool  = gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPoolSetting" -filter "Name like '%$name%'"
    if (!($tempPool -eq $NULL)) {$tempPool.delete()}

    # create Application Pool
    $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()
}

function CreateWebSite ([string]$name, [string]$ipAddress, [string]$localPath, [string] $appPoolName, [string] $applicationName)
{
    # 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()}

    # Switch the Website to .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)

    # Assign App Pool
    $webServerSettings  = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'"
    $webServerSettings.AppPoolId = $appPoolName
    $webServerSettings.put()

    # Add wildcard map
    $wildcardMap = "*, c:\somewildcardfile.dll, 0, All"
    $iis = [ADSI]"IIS://localhost/W3SVC"
    $webServer = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $name }
    $webVirtualDir = $webServer.children | where { $_.keyType -eq "IIsWebVirtualDir" }
    $webVirtualDir.ScriptMaps.Add($wildcardMap)

    # Set Application name
    $webVirtualDir.AppFriendlyName = $applicationName

    # Save changes
    $webVirtualDir.CommitChanges()

    # Start the newly create web site
    if (!($webServer -eq $NULL)) {$webServer.start()}
}

function AddSslCertificate ([string] $websiteName, [string] $certificateCommonName)
{
    # This method requires for you to have selfssl on your machine
    $selfSslPath = "\program files\iis resources\selfssl"

    $certificateCommonName = "/N:cn=" + $certificateCommonName

    $certificateValidityDays = "/V:3650"
    $websitePort = "/P:443"
    $addToTrusted = "/T"
    $quietMode = "/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 $quietMode
}

$myNewWebsiteName = "TestWebsite"
$myNewWebsiteIp = "192.168.0.1"
$myNewWebsiteLocalPath = "c:\inetpub\wwwroot\"+$myNewWebsiteName
$appPoolName = $myNewWebsiteName + "AppPool"
$myNewWebsiteApplicationName = "/"
$myNewWebsiteCertificateCommonName = "mynewwebsite.dev"

CreateAppPool $appPoolName "Administrator" "password"
CreateWebSite $myNewWebsiteName $myNewWebsiteIp $myNewWebsiteLocalPath $appPoolName $myNewWebsiteApplicationName
AddSslCertificate $myNewWebsiteName $myNewWebsiteCertificateCommonName

答案 1 :(得分:1)

$ 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();

答案 2 :(得分:1)

这是一个有用的PowerShell代码段。

我试过运行这个,我遇到了删除测试的问题。当站点仍然存在时,删除对应用程序池不起作用。当然你应该先运行网站删除测试。

# 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()}

首先运行,然后运行应用程序池删除测试 我意识到你已将这些标记为测试,但如果网站存在,退出或删除肯定是有用的。