gpt4 book ai didi

powershell - 如何在另一个 session (Powershell ISE 选项卡)中使用一个变量?

转载 作者:行者123 更新时间:2023-12-03 23:57:29 24 4
gpt4 key购买 nike

这是我想要实现的目标的准系统代码..

$destinationDir = "subdir1"   

#creating tab
$newTab = $psise.PowerShellTabs.Add()
Do
{sleep -m 100}
While (!$newTab.CanInvoke)


#running required script in tab
$newTab.Invoke({ cd $destinationDir})

由于 $destinationDir 是在父选项卡中初始化的,它的范围仅限于它,我在子选项卡中得到以下错误

cd : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a non-null value.

如何克服这个问题并使用子选项卡中的值?

最佳答案

简短的回答:你不能。 PowerShell ISE 中的每个选项卡都是使用新的运行空间创建的。没有提供用于将变量注入(inject)此运行空间的方法。

长答案:总有变通办法。这里有两个。

<强>1。使用调用脚本 block 将变量传输到新的运行空间:

$destinationDir = "subdir1"
#creating tab
$newTab = $psise.PowerShellTabs.Add()
Do
{sleep -m 100}
While (!$newTab.CanInvoke)

$scriptblock = "`$destinationDir = `"$($destinationDir)`"
cd `$destinationDir"

#running required script in tab
$newTab.Invoke($scriptblock)

<强>2。使用 en 环境变量:

$env:destinationDir = "subdir1"   

#creating tab
$newTab = $psise.PowerShellTabs.Add()
Do
{sleep -m 100}
While (!$newTab.CanInvoke)

#running required script in tab
$newTab.Invoke({ cd $env:destinationDir})

关于powershell - 如何在另一个 session (Powershell ISE 选项卡)中使用一个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29080146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com