批处理之家's Archiver

5i365 发表于 2022-4-8 22:17

PowerShell怎样把hash表的键值对从外部文本中调用过来并执行

最下面是名为Test.ps1的代码, 可以显示结果:   小A
现在我想把$hash的下面两行键值内容放到同目录下的test.txt文件中,
[color=#ff0000]A = "小A"
B = "小B"[/color]
[size=3]然后,我想实现执行Test.ps1得到相同的运行结果, [/size]
[size=3]下面这样改代码, 行不通, 请路过大侠指引,提前感谢![/size]
$hash = [ordered]@{
[color=#ff0000]gc Test.txt[/color]
}[code]$hash = [ordered]@{
        A = "小A"
        B = "小B"
}
$hash.A[/code]

went 发表于 2022-4-8 23:02

@'
$hash = [ordered]@{{
    {0}
}}
'@ -f (Get-Content 'test.txt' | Out-String) | Invoke-Expression

$hash.A

5i365 发表于 2022-4-9 06:14

[i=s] 本帖最后由 5i365 于 2022-4-9 06:28 编辑 [/i]

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=253978&ptid=62272]2#[/url] [i]went[/i] [/b]


   感谢大侠支招, 能得到结果

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.