在脚本中运行命令时出错,但在单独运行时却没有错误

时间:2019-05-15 20:16:45

标签: powershell iis

在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
    }
}

0 个答案:

没有答案
相关问题