2008-11-04 11 views

Antwort

6

Es ist nicht die naheliegendste Prozess, aber hier ist das, was für mich gearbeitet ..

$AppPoolSettings = [wmiclass]'root\MicrosoftIISv2:IISApplicationPoolSetting' 
$NewPool = $AppPoolSettings.CreateInstance() 
$NewPool.Name = 'W3SVC/AppPools/MyAppPool' 
$Result = $NewPool.Put() 

Sie könnten einen Fehler mit dem Aufruf von Put() erhalten, aber es eine zweite (oder dritte) Zeit aufzurufen sollte es funktionieren. Dies ist auf ein Problem mit PowerShell V1 und WMI zurückzuführen.

+0

Danke, funktioniert sehr gut. Ich glaube, ich habe das versucht, aber ich wurde durch diesen Fehler in Verlegenheit gebracht. –

+0

Froh, dass es für dich funktioniert hat. Dieser Fehler ist etwas störend, aber das ist nur V1 von PowerShell. –

7

Ich dachte, ich könnte das Skript teilen, das ich mir ausgedacht habe. Danke geht an Steven und Leon.

# Settings 
$newApplication = "MaxSys.Services" 
$poolUserName = "BRISBANE\svcMaxSysTest" 
$poolPassword = "ThisisforT3sting" 

$newVDirName = "W3SVC/1/ROOT/" + $newApplication 
$newVDirPath = "C:\" + $newApplication 
$newPoolName = $newApplication + "Pool" 

#Switch the Website to .NET 2.0 
C:\windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -sn W3SVC/ 

# Create Application Pool 
$appPoolSettings = [wmiclass] "root\MicrosoftIISv2:IISApplicationPoolSetting" 
$newPool = $appPoolSettings.CreateInstance() 
$newPool.Name = "W3SVC/AppPools/" + $newPoolName 
$newPool.PeriodicRestartTime = 0 
$newPool.IdleTimeout = 0 
$newPool.MaxProcesses = 2 
$newPool.WAMUsername = $poolUserName 
$newPool.WAMUserPass = $poolPassword 
$newPool.AppPoolIdentityType = 3 
$newPool.Put() 
# Do it again if it fails as there is a bug with Powershell/WMI 
if (!$?) 
{ 
    $newPool.Put() 
} 

# Create the virtual directory 
mkdir $newVDirPath 

$virtualDirSettings = [wmiclass] "root\MicrosoftIISv2:IIsWebVirtualDirSetting" 
$newVDir = $virtualDirSettings.CreateInstance() 
$newVDir.Name = $newVDirName 
$newVDir.Path = $newVDirPath 
$newVDir.EnableDefaultDoc = $False 
$newVDir.Put() 
# Do it a few times if it fails as there is a bug with Powershell/WMI 
if (!$?) 
{ 
    $newVDir.Put() 
} 

# Create the application on the virtual directory 
$vdir = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IISWebVirtualDir" -filter "Name = '$newVDirName'" 
$vdir.AppCreate3(2, $newPoolName) 

# Updated the Friendly Name of the application 
$newVDir.AppFriendlyName = $newApplication 
$newVDir.Put() 
+0

@robert wagner - Ich würde Ihre Fehlerbehandlung auf DO ändern {$ newVDir.Put()} WHILE (! $?) Dann müssen Sie nicht die erste Put() haben. Die Do/While-Schleife führt den Put solange aus, bis kein Fehler mehr auftritt. Nur ein Vorschlag. –

+0

Es könnte in einer Endlosschleife stecken bleiben, aber es ist ein Gedanke. –

+0

Sie könnten immer einen kleinen Zähler haben, ein erneuter Versuch bis zu einer maximalen Anzahl von Malen. – jrista

0

Alles ist gut! Ich habe den Code so geändert, dass nach dem ersten Fehler explizit der Befehl $ newPool.Put() aufgerufen wird. Danke für Ihre Hilfe!

Verwandte Themen