Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将命令作为脚本的一部分运行时出错,但单独运行时不出错_Powershell_Iis - Fatal编程技术网

Powershell 将命令作为脚本的一部分运行时出错,但单独运行时不出错

Powershell 将命令作为脚本的一部分运行时出错,但单独运行时不出错,powershell,iis,Powershell,Iis,作为脚本的一部分运行该命令时,或在Powershell ISE和Powershell终端中进行调试时,出现以下错误。我在这两个领域都是以管理员的身份运行的 Rename-Item -Path IIS:\AppPools\webdba2 -NewName WEBDBA 重命名项:索引超出范围。必须是非负的且更少 大于集合的大小。参数名称:索引 powershell中的实际代码是 Rename-Item -Path "IIS:\Sites\$CurrentWebsiteName" -NewName

作为脚本的一部分运行该命令时,或在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
    }
}

你能在你的问题中添加更多的脚本吗?当您使用
ForEach对象
在集合中迭代时,或者错误地将变量类型设置为
@()
时,通常会出现此错误。在脚本中添加了更多内容。在我看来,
$CurrentAppPool=($sm.ApplicationPools | Where Object{$\ Name-类似于$AppPoolSearchVar}).Name
返回应用池名称数组。在这种情况下,决定要做什么:使用
foreach()
循环,只需重命名第一个
$CurrentAppPool[0]
,或者将
-like
操作符更改为
eq
它只返回一个应用程序池。PS C:\Windows\system32>($sm.ApplicationPools |其中对象{$\.Name-类似$AppPoolSearchVar}).Name uuuuuuuuuuuuuuuuuuuuu返回:webdba2