如何在powershell中循环直到成功?
%WINDIR%\system32\inetsrv\appcmd.exe set site /site.name:"WebRole_IN_0_CB" /[Path='/'].applicationPool:"ASP.NET v4.0" >>CBLog.log
powershell.exe -command Set-ExecutionPolicy Unrestricted
powershell.exe .\CheckIfSuccessful.ps1
我想运行上面的脚本,如果 appcmd.exe 由于网站尚未启动并准备就绪而无法执行它,我会在 CBLog.log 文件中收到以下错误消息。
错误(消息:找不到带有标识符的 SITE 对象 “WebRole_IN_0_CB”。 )
在 CheckIfSuccessful.ps1 中,我想创建一个要循环的 Powershell 脚本,该脚本运行 appcmd 命令并再次检查错误。然后休眠5秒然后重试,直到成功。
我该如何在 Powershell 中执行此操作?
非常感谢,
更新:
好的,非常感谢 $lastexitcode 的提示,虽然它看起来很有希望。我似乎在 Powershell 中转换单引号时遇到问题:(以下是我最初的 appcmd 命令的一部分)
/[Path='/'].applicationPool:"ASP.NET v4.0"
如何在 powershell 中引用它?我试图简化它,以便 powershell 的麻烦更少,但看来我的命令现在是错误的,正如它现在所说:
错误(消息:找不到标识符为“v4.0”的 SITE 对象。)
& $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
}
}
更新 II: 我已经正确地逃脱了。但我仍然收到错误消息。如果我在创建该站点后单独运行 appcmd,日志文件就可以了。请问有什么建议吗?
%WINDIR%\system32\inetsrv\appcmd.exe set site /site.name:"WebRole_IN_0_CB" /[Path='/'].applicationPool:"ASP.NET v4.0" >>CBLog.log
powershell.exe -command Set-ExecutionPolicy Unrestricted
powershell.exe .\CheckIfSuccessful.ps1
I would like to run the script above and if appcmd.exe can't execute it due the WebSite not yet being up and ready I would get the following error message in the CBLog.log file.
ERROR ( message:Cannot find SITE object with identifier
"WebRole_IN_0_CB". )
In the CheckIfSuccessful.ps1 I would like to create a Powershell script to loop, which runs the appcmd command and checks the error again. Then sleeps for 5 seconds and then retries, until it succeeds.
How would I do this in Powershell?
highly appreciated,
UPDATE:
Ok, many thanks for the tip with $lastexitcode, while it looks promising. It seems I have problems with converting single quoates in Powershell: (The below is part of my initial appcmd command)
/[Path='/'].applicationPool:"ASP.NET v4.0"
How do I quote this in powershell? I tried to simplify it so that powershell has less trouble with it, but it seems my command is now wrong, as it says now:
ERROR ( message:Cannot find SITE object with identifier "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
}
}
UPDATE II:
I have escaped it properly. But still I get the error message. If I run the appcmd alone after creating that site, the log file is ok with it. Any suggestions please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
特殊变量
$lastexitcode
为您提供最后运行的本机可执行文件(即不是 cmdlet)的退出代码。根据操作结果,这将具有不同的值。例如,如果appcmd遇到访问被拒绝,则该值为5。如果最后一个命令成功完成,它应该为 0。确保在运行 appcmd 后立即在同一脚本中检查此变量。如果没有,您可能会从 powershell.exe 而不是从 appcmd.exe 获取退出代码
The special variable
$lastexitcode
gives you the exit code of the last native executable (i.e. not cmdlet) that was run. This will have different values depending on the results of the operation. For example, it will be 5 if appcmd encounters access denied. It should be 0 if the last command completed successfuolly.Make sure you check this variable immediately after you run appcmd and in the same script. If not, you risk getting the exitcode from powershell.exe and not from appcmd.exe
我意识到最初的问题可能早已得到解决,但为了 Google 员工解决这个问题,我编写了一个通用(高级)函数来重试 shell (cmd) 命令。除了 $LASTEXITCODE 之外,它还可以解析错误流(使用某些工具,退出代码可能是谎言)。
有关更多信息(包括完整文档),请参阅 https: //www.ohadsoft.com/2016/04/invoking-任意-shell-cmd-commands-in-powershell。
在你的情况下你会做类似的事情
I realize that the original issue has likely longs been resolved, but for the sake of Googlers hitting this question, I wrote a general purpose (advanced) function for retrying shell (cmd) commands. In addition to $LASTEXITCODE it can also parse the error stream (with some tools, the exit code can be a lie).
For more information (including full documentation) see https://www.ohadsoft.com/2016/04/invoking-arbitrary-shell-cmd-commands-in-powershell.
In your case you would do something like
简单版本:
我注意到
$lastexitcode
仅适用于应用程序。 Powershell 命令如ls
(get-childitem
) 不会设置它,因此您必须从其输出中获取结果。Simple version:
I'd note
$lastexitcode
is only available for applications. Powershell commands such asls
(get-childitem
) do not set it, so you'd have to get the result from their output instead.