如何将 here-string 作为参数传递给终端中的 powershell 脚本?

How do I pass here-string as a parameter to a powershell script in a terminal?

我有一个名为 "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 的终端中 运行 this 时,我无法粘贴或传递 $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

要么导致语法错误,要么没有给出预期的结果。

我不知道为什么你的 powershell 在出现新行时执行,而不是提示你完成命令。这个对我有用。这是 PS 5.1。如果它是旧版本,也许会更新它。