Board logo

标题: [问题求助] PowerShell怎样把hash表的键值对从外部文本中调用过来并执行 [打印本页]

作者: 5i365    时间: 2022-4-8 22:17     标题: PowerShell怎样把hash表的键值对从外部文本中调用过来并执行

最下面是名为Test.ps1的代码, 可以显示结果:   小A
现在我想把$hash的下面两行键值内容放到同目录下的test.txt文件中,
A = "小A"
B = "小B"

然后,我想实现执行Test.ps1得到相同的运行结果,
下面这样改代码, 行不通, 请路过大侠指引,提前感谢!
$hash = [ordered]@{
gc Test.txt
}
  1. $hash = [ordered]@{
  2.         A = "小A"
  3.         B = "小B"
  4. }
  5. $hash.A
复制代码

作者: 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

本帖最后由 5i365 于 2022-4-9 06:28 编辑

回复 2# went


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




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2