2010-02-11 97 views
6

我試圖創建創建一個新的IIS 6的網站,並設置之類的應用程序池,通配符應用程序映射,ASP.NET版本等如何使用PowerShell

PowerShell腳本來更新現有的IIS 6網站

經過在互聯網上的大量搜索後,我發現了一個腳本,允許我創建一個新的網站,但不能修改我需要的所有屬性。

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

任何有關如何擴大上述示例的幫助,我們將不勝感激。

+0

對不起,您需要修改哪些屬性? – 2010-02-11 12:02:28

回答

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

首先,非常感謝jrista指着我在正確的方向。

我還發現這個article非常有用。

這裏下面是一個PowerShell腳本來創建應用程序池,網站和SelfSsl證書:

 

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 
+0

真棒。會upvote 10x,如果我可以... – mwjackson 2010-06-02 10:57:46

+0

只有改變使刪除網站之前,你刪除應用程序池,否則它的錯誤(打破刪除成自己的功能) – mwjackson 2010-06-02 12:48:32

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

先運行它,然後運行應用程序池刪除測試。
我知道你已經將這些標記爲測試,但如果網站存在,退出或刪除它肯定是有用的。