在新的,干净的PowerShell实例中调用PowerShell脚本(在另一个脚本中)

栏目: 编程工具 · 发布时间: 6年前

内容简介:翻译自: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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

网络多人游戏架构与编程

网络多人游戏架构与编程

格雷泽 (Joshua Glazer)、马达夫 (Sanjay Madhav) / 王晓慧、张国鑫 / 人民邮电出版社 / 2017-10-1 / CNY 109.00

本书是一本深入探讨关于网络多人游戏编程的图书。 全书分为13章,从网络游戏的基本概念、互联网、伯克利套接字、对象序列化、对象复制、网络拓扑和游戏案例、延迟、抖动和可靠性、改进的延迟处理、可扩展性、安全性、真实世界的引擎、玩家服务、云托管专用服务器等方面深入介绍了网络多人游戏开发的知识,既全面又详尽地剖析了众多核心概念。 本书的多数示例基于C++编写,适合对C++有一定了解的读者阅读。本......一起来看看 《网络多人游戏架构与编程》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具