Как передать здесь-строку в качестве параметра скрипту powershell в терминале? - PullRequest
0 голосов
/ 24 мая 2019

У меня есть скрипт Powershell с именем "file1.ps1", как показано ниже, который прекрасно работает:

$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> "@

Как в этом случае указать здесь-строку в качестве параметра?

Я попытался построить здесь-строку как одну строку в одинарных кавычках, а также три последовательных двойных кавычки.Кроме того, попытались использовать backtick n и backtick r

. Либо это приводит к синтаксической ошибке, либо не дает ожидаемых результатов.

1 Ответ

0 голосов
/ 25 мая 2019

Я не знаю, почему ваш PowerShell выполняется при появлении новой строки вместо того, чтобы попросить вас выполнить команду. Меня устраивает. Это PS 5.1. Может быть, обновить его, если его старая версия.

enter image description here

...