2010-02-11 16 views
6

Sto cercando di creare uno script PowerShell che crea un nuovo sito Web IIS 6 e imposta le cose come App Pool, mappe di applicazione jolly, versione di ASP.NET, eccCome aggiornare IIS 6 Sito Web esistente utilizzando PowerShell

Dopo una lunga ricerca su Internet ho trovato uno script che mi permette di creare un nuovo sito Web ma non di modificare tutte le proprietà di cui ho bisogno.

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

Qualsiasi aiuto su come espandere l'esempio sopra è molto apprezzato.

+0

Siamo spiacenti, quali proprietà è necessario modificare in modo specifico? –

risposta

1

L'oggetto $ result contiene il percorso dell'oggetto IIsWebServer appena creato. È possibile ottenere l'accesso alla directory virtuale, dove è possibile configurare più proprietà, effettuando le seguenti operazioni:

$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

Prima di tutto, grazie a grandi jrista per avermi nella giusta direzione.

Ho trovato anche questo article molto utile.

Ciò che segue è uno script PowerShell per creare pool di applicazioni, sito Web e un certificato 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

fantastico. farebbe un aumento di 10 volte se potessi ... – mwjackson

+0

cambiare solo per fare è eliminare il sito Web prima di eliminare il pool di app, altrimenti si commette errori (scomporre le eliminazioni nelle proprie funzioni) – mwjackson

1

Questo è un utile frammento di PowerShell.

Ho provato a eseguire questo e ottengo problemi con i test di eliminazione. Elimina non funziona contro il pool di app quando il sito esiste ancora. Sicuramente dovresti eseguire prima il test di eliminazione del sito web.

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

Eseguire prima questo, quindi eseguire il test di eliminazione del pool di app.
Mi rendo conto che li hai contrassegnati come test ma sicuramente è utile uscire o eliminare se il sito web esiste.

Problemi correlati