Powershell启动过程,用于启动Powershell会话并传递局部变量 [英] Powershell Start-Process to start Powershell session and pass local variables

查看:118
本文介绍了Powershell启动过程,用于启动Powershell会话并传递局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Powershell Start-Process cmdlet启动新的Powershell会话并传递带有局部变量的脚本块(其中一次将是数组)?

Is there a way to use the Powershell Start-Process cmdlet to start a new Powershell session and pass a scriptblock with local variables (once of which will be an array)?

示例:

$Array = @(1,2,3,4)

$String = "This is string number"

$Scriptblock = {$Array | ForEach-Object {Write-Host $String $_}}

Start-Process Powershell -ArgumentList "$Scriptblock"

谢谢.

推荐答案

通过将数组与"/"连接在一起以创建字符串,然后将脚本块输入具有适当参数的另一个.ps1脚本中,我能够使它起作用在第二个脚本中使用

I was able to get this to work by joining the array with "/" to create a string and entering the scriptblock into another .ps1 script with appropriate parameters and splitting the joined string back to an array within the second script and using

Start-Process Powershell -ArgumentList "&C:\script.ps1 $JoinedArray $String"

丑陋,但这是我可以使它工作的唯一方法.感谢您的所有答复.

Ugly, but it's the only way I could get it to work. Thanks for all the replies.

这篇关于Powershell启动过程,用于启动Powershell会话并传递局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆