我正在通过Power Shell脚本创建应用程序池。
即使环顾四周,我也找不到如何将“立即启动应用程序池”设置为true。我怎样才能做到这一点?
此外,如果提供了用户名/密码,那么我想设置它,否则应为应用程序池标识。我做得对吗?
这是函数
Function Create-AppPools($appPoolName, $appPoolDotNetVersion, $managedPipelineMode, $startMode, $userName, $password) {
if(!$appPoolName){
return;
}
Write-Host " "
#navigate to the app pools root
cd IIS:\AppPools\
#check if the app pool exists
if (!(Test-Path $appPoolName -pathType container))
{
Write-Host "`t Creating AppPool: " + $appPoolName
#create the app pool
$appPool = New-Item $appPoolName
if($appPoolDotNetVersion){
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $appPoolDotNetVersion
}
if(@managedPipelineMode){
$appPool | Set-ItemProperty -Name "managedPipelineMode" -Value $managedPipelineMode
}
if($startMode){
$appPool | Set-ItemProperty -Name "startMode" -Value $startMode
}
if($userName -and $password){
$apppool | Set-ItemProperty -Name processmodel -value @{userName = $userName;password=$password;identitytype=3}
}
else{
$apppool | Set-ItemProperty -Name "ProcessModel.IdentityType" -value 3
}
Write-Host "`t`t AppPool: " + $appPoolName + " created successfully" -ForegroundColor Green
}
else{
Write-Host "`t AppPool " + $appPoolName + " already exists" -ForegroundColor Blue
}
}
更新1: 我的问题得到回答后,请检查github中的示例脚本。
答案 0 :(得分:1)
您的第四个if()
中有一个错字。
除此之外,您的代码可以按预期工作。
我建议通过#Requires
语句指定代码所依赖的模块。
#Requires -RunAsAdministrator
#Requires -Modules WebAdministration
Function Create-AppPools(
$appPoolName = "TestPool2",
$appPoolDotNetVersion = "v4.0",
$managedPipelineMode = "Integrated",
$startMode = "OnDemand",
$userName,
$password
) {
if (!$appPoolName) {
return;
}
Write-Host " "
#navigate to the app pools root
cd IIS:\AppPools\
#check if the app pool exists
if (!(Test-Path $appPoolName -pathType container)) {
Write-Host "`t Creating AppPool: " + $appPoolName
#create the app pool
$appPool = New-Item $appPoolName
if ($appPoolDotNetVersion) {
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $appPoolDotNetVersion
}
if ($managedPipelineMode) {
$appPool | Set-ItemProperty -Name "managedPipelineMode" -Value $managedPipelineMode
}
if ($startMode) {
$appPool | Set-ItemProperty -Name "startMode" -Value $startMode
}
if ($userName -and $password) {
$apppool | Set-ItemProperty -Name processmodel -value @{userName = $userName; password = $password; identitytype = 3 }
}
else {
$apppool | Set-ItemProperty -Name "ProcessModel.IdentityType" -value 3
}
Write-Host "`t`t AppPool: " + $appPoolName + " created successfully" -ForegroundColor Green
}
else {
Write-Host "`t AppPool " + $appPoolName + " already exists" -ForegroundColor Blue
}
}
Create-AppPools