首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将here-string作为参数传递给终端中的powershell脚本?

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

Stack Overflow用户
提问于 2019-05-25 03:43:30
回答 1查看 380关注 0票数 0

我有一个名为"file1.ps1“的Powershell脚本,如下所示,它可以很好地工作:

代码语言:javascript
复制
$herestring = @"
State = Defined
FiscalWeek =
"@

$hash = ConvertFrom-StringData $herestring

$hash | Format-Table

该文件给出了以下输出:

代码语言:javascript
复制
PS C:\temp> .\file1.ps1

Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
State                          Defined                                                                                                                                   
FiscalWeek                                                                                                                                                               

现在,我想将$herestring作为参数传递给这个文件。因此,修改后的脚本如下所示:

代码语言:javascript
复制
param ($herestring)

$hash = ConvertFrom-StringData $herestring

$hash | Format-Table

因此,当我在Powershell ISE或vscode的终端中运行此命令时,我无法粘贴或传递$herestring,因为它有新的行字符。

代码语言:javascript
复制
PS C:\Temp> .\file2.ps1 -herestring 

当我尝试粘贴时,每一行都被视为单独的命令。

代码语言:javascript
复制
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

它要么导致语法错误,要么不能提供预期的结果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-25 11:20:33

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56298714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档