当前位置:  开发笔记 > 后端 > 正文

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

如何解决《如何使用PowerShell更新现有的IIS6网站》经验,为你挑选了1个好方法。

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

在Internet上进行了大量搜索之后,我发现了一个脚本,它允许我创建一个新的Web站点,但不能修改我需要的所有属性.

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

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



1> Plamen Iliev..:

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

我也发现这篇文章非常有用.

以下是用于创建应用程序池,网站和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

推荐阅读
帆侮听我悄悄说星星
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有