简体   繁体   English

如何使Powershell等待内部命令完成?

[英]How to make Powershell wait on internal command to finish?

I have a PS script that creates a new app pool and then changes it's .Net version to 4.0: 我有一个PS脚本,该脚本创建一个新的应用程序池,然后将其.Net版本更改为4.0:

new-webapppool -name 'ABC' -force
Set-ItemProperty IIS:\AppPools\ABC managedRuntimeVersion v4.0

This script is run on an AWS EC2 instance non-interactively as part of a Cloudformation stack creation. 作为Cloudformation堆栈创建的一部分,此脚本在AWS EC2实例上非交互运行。

My problem is that randomly I will get the following error: 我的问题是,随机出现以下错误:

Set-ItemProperty : Cannot find path 'IIS:\AppPools\ABC' because it does not exist.

Do I need some additional logic to force the PS script to wait until the "new-webapppool" command completes? 我是否需要其他逻辑来强制PS脚本等待“ new-webapppool”命令完成? If so, what is the correct way? 如果是这样,正确的方法是什么?

From my Googling I am finding that one should only have to explicitly tell it to wait if an external program (.exe, etc) is being executed. 从我的Google搜索中,我发现只有在执行外部程序(.exe等)的情况下,才应明确告诉它等待。 Thoughts? 思考?

You can add a while loop, which will wait until the previous command has fully replicated to your connected cloud instance. 您可以添加一个while循环,该循环将等待直到之前的命令已完全复制到您所连接的云实例。 I feel like this is a side effect of cloud. 我觉得这是云的副作用。

new-webapppool -name 'ABC' -force

while (!(get-childitem IIS:\AppPools\ABC -errorAction SilentlyContinue)) {
     start-sleep 5 # recheck every 5 seconds
}

Set-ItemProperty IIS:\AppPools\ABC managedRuntimeVersion v4.0

You can also store your -name as a variable and use the same variable in the while loop. 您还可以将-name存储为变量,并在while循环中使用相同的变量。 That way you don't have to update the String in multiple places for each subsequent run of the code. 这样,您不必为以后的每次代码运行都在多个位置更新String。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM