Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
Iis 如何在powershell中循环直到成功?_Iis_Powershell_Appcmd - Fatal编程技术网

Iis 如何在powershell中循环直到成功?

Iis 如何在powershell中循环直到成功?,iis,powershell,appcmd,Iis,Powershell,Appcmd,我想运行上面的脚本,如果appcmd.exe无法执行它,因为网站尚未启动并准备就绪,我会在CBLog.log文件中得到以下错误消息 错误(消息:找不到标识符为的站点对象 “WebRole\u在\u 0\u CB中”。) 在CheckIfSuccessful.ps1中,我想创建一个Powershell脚本来循环,它运行appcmd命令并再次检查错误。然后休眠5秒钟,然后重试,直到成功 在Powershell中我该怎么做 高度赞赏 更新: 好的,非常感谢$lastexitcode的提示,尽管它看起来

我想运行上面的脚本,如果appcmd.exe无法执行它,因为网站尚未启动并准备就绪,我会在CBLog.log文件中得到以下错误消息

错误(消息:找不到标识符为的站点对象 “WebRole\u在\u 0\u CB中”。)

在CheckIfSuccessful.ps1中,我想创建一个Powershell脚本来循环,它运行appcmd命令并再次检查错误。然后休眠5秒钟,然后重试,直到成功

在Powershell中我该怎么做

高度赞赏

更新:

好的,非常感谢$lastexitcode的提示,尽管它看起来很有希望。似乎我在Powershell中转换单个quoates时遇到了问题:(下面是我最初的appcmd命令的一部分)

如何在powershell中引用此内容?我试图对其进行简化,以便powershell在这方面的问题更少,但现在看来我的命令是错误的,正如它现在所说:

错误(消息:找不到标识符为“v4.0”的站点对象)

更新II:
我完全逃脱了。但我还是收到了错误信息。如果在创建该站点后单独运行appcmd,日志文件就可以了。有什么建议吗?

特殊变量
$lastexitcode
提供上次运行的本机可执行文件(即非cmdlet)的退出代码。这将根据操作结果具有不同的值。例如,如果appcmd遇到拒绝访问,则为5。如果最后一个命令成功完成,则应为0


确保在同一脚本中运行appcmd后立即检查此变量。如果没有,您可能会从powershell.exe而不是从appcmd.exe获取exitcode。我知道原来的问题可能早就解决了,但为了让谷歌用户解决这个问题,我编写了一个通用(高级)函数,用于重试shell(cmd)命令。除了$LASTEXITCODE,它还可以解析错误流(使用某些工具,退出代码可能是谎言)

有关更多信息(包括完整文档),请参阅

在你的情况下,你会这样做

function Call-CommandWithRetries
{
 [CmdletBinding()]
 param( 
     [Parameter(Mandatory=$True)]
     [string]$Command,
     [Array]$Arguments,
     [bool]$TrustExitCode = $True,
     [int]$RetrySleepSeconds = 10,
     [int]$MaxAttempts = 10,
     [bool]$PrintCommand = $True
 )

 Process
 {
  $attempt = 0
  while ($true)
  {   
   Write-Host $(if ($PrintCommand) {"Executing: $Command $Arguments"} else {"Executing command..."}) 
   & $Command $Arguments 2>&1 | tee -Variable output | Write-Host

   $stderr = $output | where { $_ -is [System.Management.Automation.ErrorRecord] }
   if ( ($LASTEXITCODE -eq 0) -and ($TrustExitCode -or !($stderr)) )
   {
    Write-Host "Command executed successfully"
    return $output
   }

   Write-Host "Command failed with exit code ($LASTEXITCODE) and stderr: $stderr" -ForegroundColor Yellow
   if ($attempt -eq $MaxAttempts)
   {
    $ex = new-object System.Management.Automation.CmdletInvocationException "All retry attempts exhausted"
    $category = [System.Management.Automation.ErrorCategory]::LimitsExceeded
    $errRecord = new-object System.Management.Automation.ErrorRecord $ex, "CommandFailed", $category, $Command
    $psCmdlet.WriteError($errRecord)
    return $output
   }

   $attempt++;
   Write-Host "Retrying test execution [#$attempt/$MaxAttempts] in $RetrySleepSeconds seconds..."
   Start-Sleep -s $RetrySleepSeconds
  }
 }
}

好的,转义现在可以了,但我仍然收到错误消息,你知道这是什么吗?脚本将永远循环。即使我用这个名字创建了一个网站。出于某些原因,日志文件不断重复最初的错误,PowerShell不喜欢这个空间。我最终创建了自己的应用程序池,并将其分配给了网站。有完整的源代码示例和最终解决方案吗?很抱歉,这仍然不起作用。Powershell不喜欢在没有任何错误消息的情况下正确执行命令,而appcmd上的同一命令运行正常,有什么想法吗?
/[Path='/'].applicationPool:"ASP.NET v4.0"
 & $Env:WinDir\system32\inetsrv\appcmd.exe set site '/site.name:"WebRole_IN_0_CB"' "/[Path='/'].applicationPool:`"ASP.NET v4.0`"" >CBLog.log
if($lastexitcode -ne '0')
{
    while($lastexitcode -ne '0')
    {
        Start-Sleep -s 5
        & $Env:WinDir\system32\inetsrv\appcmd.exe set site '/site.name:"WebRole_IN_0_CB"' "/[Path='/'].applicationPool:`"ASP.NET v4.0`"" >CBLog.log
    }
}
function Call-CommandWithRetries
{
 [CmdletBinding()]
 param( 
     [Parameter(Mandatory=$True)]
     [string]$Command,
     [Array]$Arguments,
     [bool]$TrustExitCode = $True,
     [int]$RetrySleepSeconds = 10,
     [int]$MaxAttempts = 10,
     [bool]$PrintCommand = $True
 )

 Process
 {
  $attempt = 0
  while ($true)
  {   
   Write-Host $(if ($PrintCommand) {"Executing: $Command $Arguments"} else {"Executing command..."}) 
   & $Command $Arguments 2>&1 | tee -Variable output | Write-Host

   $stderr = $output | where { $_ -is [System.Management.Automation.ErrorRecord] }
   if ( ($LASTEXITCODE -eq 0) -and ($TrustExitCode -or !($stderr)) )
   {
    Write-Host "Command executed successfully"
    return $output
   }

   Write-Host "Command failed with exit code ($LASTEXITCODE) and stderr: $stderr" -ForegroundColor Yellow
   if ($attempt -eq $MaxAttempts)
   {
    $ex = new-object System.Management.Automation.CmdletInvocationException "All retry attempts exhausted"
    $category = [System.Management.Automation.ErrorCategory]::LimitsExceeded
    $errRecord = new-object System.Management.Automation.ErrorRecord $ex, "CommandFailed", $category, $Command
    $psCmdlet.WriteError($errRecord)
    return $output
   }

   $attempt++;
   Write-Host "Retrying test execution [#$attempt/$MaxAttempts] in $RetrySleepSeconds seconds..."
   Start-Sleep -s $RetrySleepSeconds
  }
 }
}
$appCmd = [io.path]::combine($env:WINDIR, 'system32', 'inetsrv', 'appcmd.exe')
$appCmdArg = @('set', 'site', '/site.name:"WebRole_IN_0_CB"', '/[Path=''/''].applicationPool:"ASP.NET v4.0"')
$output = Call-CommandWithRetries $appCmd $appCmdArgs
# write $output to log etc