我有一个名为"file1.ps1“的Powershell脚本,如下所示,它可以很好地工作:
$herestring = @"
State = Defined
FiscalWeek =
"@
$hash = ConvertFrom-StringData $herestring
$hash | Format-Table该文件给出了以下输出:
PS C:\temp> .\file1.ps1
Name Value
---- -----
State Defined
FiscalWeek 现在,我想将$herestring作为参数传递给这个文件。因此,修改后的脚本如下所示:
param ($herestring)
$hash = ConvertFrom-StringData $herestring
$hash | Format-Table因此,当我在Powershell ISE或vscode的终端中运行此命令时,我无法粘贴或传递$herestring,因为它有新的行字符。
PS C:\Temp> .\file2.ps1 -herestring 当我尝试粘贴时,每一行都被视为单独的命令。
PS C:\Temp> .\file2.ps1 -herestring @"
The string is missing the terminator: "@.
At line:0 char:0
PS C:\Temp> State = Defined
State : The term 'State' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1
+ State = Defined
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (State:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Temp> FiscalWeek =
FiscalWeek : The term 'FiscalWeek' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
+ FiscalWeek =
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (FiscalWeek:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Temp> "@在这种情况下,我如何提供here-string作为参数?
我尝试将here-string构造为单引号内的单行以及三个连续的双引号。此外,我还尝试了使用反标记n和反标记r
它要么导致语法错误,要么不能提供预期的结果。
发布于 2019-05-25 11:20:33
我不知道为什么powershell会在出现新行时执行,而不是提示您完成命令。这对我很管用。这是PS 5.1。如果它是一个旧版本,也许可以更新它。

https://stackoverflow.com/questions/56298714
复制相似问题