内容简介:翻译自:https://stackoverflow.com/questions/13096061/call-a-powershell-script-in-a-new-clean-powershell-instance-from-within-anothe
在我的循环中,我正在运行powershell.exe -command<path to script>.我不知道这是否是最好的方法,或者两个实例是否彼此完全分开.
在一个干净的PowerShell实例中运行脚本的首选方法是什么?或者我应该说“会话”?
使用powershell.exe似乎是一个很好的方法,但当然有其优点和缺点.
优点:
>在单独的干净会话中调用每个脚本.
>即使崩溃也不会停止整个测试过程.
缺点:
>调用powershell.exe有点慢.
>测试取决于退出代码,但0并不总是意味着成功.
没有提到的缺点是一个潜在的问题.
演示脚本如下.它已经过PS v2和v3测试.脚本名称
可能包括特殊字符,如空格,撇号,括号,反引号,
美元.注释要求中提到的一个是获取脚本路径的能力
在他们的代码中.通过提出的方法,脚本可以获得自己的路径
$MyInvocation.MyCommand.Path
# make a script list, use the full paths or explicit relative paths
$scripts = @(
'.\test1.ps1' # good name
'.\test 2.ps1' # with a space
".\test '3'.ps1" # with apostrophes
".\test [4].ps1" # with brackets
'.\test `5`.ps1' # with backticks
'.\test $6.ps1' # with a dollar
'.\test ''3'' [4] `5` $6.ps1' # all specials
)
# process each script in the list
foreach($script in $scripts) {
# make a command; mind &, ' around the path, and escaping '
$command = "& '" + $script.Replace("'", "''") + "'"
# invoke the command, i.e. the script in a separate process
powershell.exe -command $command
# check for the exit code (assuming 0 is for success)
if ($LastExitCode) {
# in this demo just write a warning
Write-Warning "Script $script failed."
}
else {
Write-Host "Script $script succeeded."
}
}
翻译自:https://stackoverflow.com/questions/13096061/call-a-powershell-script-in-a-new-clean-powershell-instance-from-within-anothe
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- MySQL使用UDF调用shell脚本
- Nodejs 调用 R 脚本 / Nodejs Call R Script
- Go调用企业微信API发送zabbix告警信息脚本
- 新姿势!Redis中调用Lua脚本以实现原子性操作
- 使用Python和Java调用Shell脚本时的死锁陷阱
- sysbench测试Oracle时写一个lua脚本调用sqlload快速造数据
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
虚拟现实:最后的传播
聂有兵 / 中国发展出版社 / 2017-4-1 / 39.00
本书对“虚拟现实”这一诞生自70年代却在今天成为热门话题的概念进行了历史发展式的分析和回顾,认为虚拟现实是当今最重大的社会变革的技术因素之一,对虚拟现实在未来百年可能给人类社会的各个层面带来的影响进行说明,结合多个大众媒介的发展趋势,合理地推演未来虚拟现实在政治、经济、文化等领域的态势,并基于传播学理论框架提出了几个新的观点。对于普通读者,本书可以普及一般的虚拟现实知识;对于传媒行业,本书可以引导......一起来看看 《虚拟现实:最后的传播》 这本书的介绍吧!
JSON 在线解析
在线 JSON 格式化工具
RGB转16进制工具
RGB HEX 互转工具