powershell - Powershell 添加重复的 IIS 应用程序池
问题描述
我有以下 Powershell 脚本来在 IIS 中创建应用程序池和网站。该脚本在我第一次运行它时添加了应用程序池(当应用程序池不存在时)但是当我第二次运行脚本时,虽然应用程序池存在,但脚本找不到它并尝试再次创建它. 导致异常!
$WebsiteName="search-api"
$AppPoolName="search-api"
$Runtime="" # Empty = Not Managed
$Port = 3050
$PhysicalPath="C:\Applications\SearchApi"
import-module WebAdministration
clear
New-Item -Path $PhysicalPath -Force
$AppPool = Get-IISAppPool -Name $AppPoolName
If ($AppPool.Length -eq 0)
{
$AppPool = New-WebAppPool -Name $AppPoolName -Force
$appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $Runtime
}
$TheWebSite = Get-Website -Name $WebsiteName
If ($TheWebSite -eq $null)
{
New-Website -Name $WebsiteName -Port $Port -IPAddress "*" -ApplicationPool $AppPoolName -PhysicalPath $PhysicalPath -Force
}
解决方案
第一:避免PropertyNotFoundException:
If ( $AppPool.Length -eq 0 ) { }
在此对象上找不到属性“长度”。验证该属性是否存在。
改为使用If ( $null -eq $AppPool ) { }
。
第二:在使用 Get-IISAppPool 并在同一会话中创建新的应用程序池时,阅读AdminOfThings对 Odd 问题的回答。
要么申请Reset-IISServerManager
(可能需要确认),
或者,代替Get-IISAppPool -Name $AppPoolName
,使用
Get-ChildItem IIS:\AppPools | Where-Object Name -eq $AppPoolName
第三:New-WebSite
文档说关于-PhysicalPath
参数:
指定新站点的物理路径。指定的文件夹 必须已经存在。
利用New-Item -Path $PhysicalPath -Force -ItemType Directory
推荐阅读
- azure-devops - 如何查询初始“剩余工作”
- sql - 选择专家查询
- codeigniter - Codeigniter4 中的重定向问题
- spring-batch - StaxEventItemWriter XML 标头
- database - 在 oracle 中使用 rowid 条件时合并不起作用
- python - Gensim -- [Errno 2] 没有这样的文件或目录:'model.wv'
- android - Xamarin.Forms - firebase 与 monoandroid81 不兼容?
- java - USB(记忆棒)或任何其他驱动程序检测并在 JAVA 中列出其内容
- c# - 无效值“armeabi”
- php - 试图使用一个特征,但一直说没有找到