2010-02-11 23 views
6

Tôi đang cố gắng tạo tập lệnh PowerShell tạo trang web IIS 6 mới và đặt những thứ như App Pool, bản đồ ứng dụng Wildcard, phiên bản ASP.NET, v.v.Cách cập nhật trang web IIS 6 hiện có bằng PowerShell

Sau khi tìm kiếm rộng rãi trên Internet, tôi tìm thấy một tập lệnh cho phép tôi tạo một trang Web mới nhưng không sửa đổi tất cả các thuộc tính tôi cần.

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

Bất kỳ trợ giúp nào về cách mở rộng ví dụ trên đều được đánh giá cao.

+0

Xin lỗi, bạn cần sửa đổi đặc tính nào? –

Trả lời

1

Đối tượng $ result chứa đường dẫn đến đối tượng IIsWebServer mới được tạo. Bạn có thể truy cập vào thư mục ảo, nơi bạn có thể định cấu hình nhiều thuộc tính hơn bằng cách thực hiện các thao tác sau:

$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

Trước hết, cảm ơn jrista đã chỉ cho tôi đúng hướng.

Tôi cũng tìm thấy điều này article rất hữu ích.

gì sau đây là một kịch bản PowerShell để tạo ra ứng dụng hồ bơi, Website và chứng chỉ 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

tuyệt vời. sẽ upvote 10x nếu tôi có thể ... – mwjackson

+0

chỉ thay đổi để thực hiện là xóa các trang web trước khi bạn xóa các hồ bơi ứng dụng, nếu không nó lỗi (phá vỡ các xóa vào chức năng riêng của họ) – mwjackson

1

Đây là một đoạn mã PowerShell hữu ích.

Tôi đã thử chạy tính năng này và tôi gặp sự cố với các thử nghiệm xóa. Xóa không hoạt động đối với nhóm ứng dụng khi trang web vẫn tồn tại. Chắc chắn bạn nên chạy thử trang web xóa trước.

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

Chạy ứng dụng này trước, sau đó chạy thử nghiệm xóa nhóm ứng dụng.
Tôi nhận ra rằng bạn đã đánh dấu chúng là các thử nghiệm nhưng chắc chắn sẽ hữu ích khi thoát hoặc xóa nếu trang web tồn tại.