powershell - 将命令作为脚本的一部分运行但单独运行时不会出错
问题描述
当我将命令作为脚本的一部分运行或在 Powershell ISE 和 Powershell 终端中进行调试时,出现以下错误。我在两者中都以管理员身份运行。
Rename-Item -Path IIS:\AppPools\webdba2 -NewName WEBDBA
重命名项目:索引超出范围。必须是非负数且小于集合的大小。参数名称:索引
powershell中的实际代码是
Rename-Item -Path "IIS:\Sites\$CurrentWebsiteName" -NewName $OrgUrl
现在在脚本之外运行变量或硬编码工作。我在 try catch 语句中运行它。
脚本:
$MassSiteData = Import-Csv $strPath -Header url,apppool,shorturl
$intRowMax = $MassSiteData.count
$intRow = 0
ForEach($WebSite in $MassSiteData)
{
$intRow++
$OrgAppPool = $WebSite.apppool
$OrgUrl = $WebSite.url
$OrgShortUrl = $WebSite.shorturl
$AppPoolSearchVar = $WebSite.apppool + "*"
$ShortUrlSearchVar = $WebSite.shorturl + "*"
[Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$sm = New-Object Microsoft.Web.Administration.ServerManager
$site = Get-Website | Where-Object {$_.Name -like $ShortUrlSearchVar}
$CurrentWebsiteName = $site.Name
$CurrentWebPath = $site.PhysicalPath
$CurrentAppPool = ($sm.ApplicationPools | Where-Object {$_.Name -like $AppPoolSearchVar}).Name
$CurrentAppPath = split-path -parent $CurrentWebPath
try {
##Rename AppPool
Rename-Item -Path "IIS:\AppPools\$CurrentAppPool" -NewName $OrgAppPool
$logMsg = "Renamed App Pool to $OrgAppPool"
Write-Host "Renamed App Pool to $OrgAppPool"
$logMsg | Out-File $logfilelocation -Append
} catch {
$logMsg = "App Pool didnt need updated"
write-host "App Pool Errored out. Most likely didnt need updated"
$logMsg | Out-File $logfilelocation -Append
}
}
解决方案
推荐阅读
- angularjs - 如何处理单个对象的“ng-repeat”和“ng-model”。?
- html - 我的最大宽度:1140px 不工作它不能在我的边缘:0 自动
- flutter - 具有多个 TextField 和一个 DropDown 的行,其中一个 TextField 应该更大,并且都应该具有相同的高度
- c# - 如何在 C# 中将主体动态添加到标记为“静态外部”的方法中?
- reactjs - 如何获得组件列表的累积宽度?
- google-apps-script - 从 Enterprise G-Suite 中的电子邮件地址获取名字和姓氏
- entity-framework - 当抛出 EntitySqlException 时,我可以看到正在执行的 SQL,还是可以覆盖 EntityCommandCompilationException?
- c# - 无法在 SortedList C# 中添加数据
- ruby-on-rails - 如何找到给定动作的所有视图路径?
- javascript - BABEL 解析错误:将 react js 转译成 js