sourcecode

공백이 있는 문자열을 PowerShell로 전달하려면 어떻게 해야 합니까?

codebag 2023. 8. 11. 21:47
반응형

공백이 있는 문자열을 PowerShell로 전달하려면 어떻게 해야 합니까?

주어진:

# test1.ps1
param(
    $x = "",
    $y = ""
)

&echo $x $y

다음과 같이 사용:

powershell test.ps1

출력:

> <blank line>

하지만 이것은 잘못된 것입니다.

test.ps1 -x "Hello, World!" -y "my friend"

출력:

Hello,
my

저는 다음을 보기를 기대했습니다.

Hello, World! my friend

음, 이건.cmd.exe문제입니다. 하지만 그것을 해결할 몇 가지 방법은

  1. 작은 따옴표 사용

    powershell test.ps1 -x 'hello world' -y 'my friend'
    
  2. 사용-file논쟁

    powershell -file test.ps1 -x "hello world" -y "my friend"
    
  3. 작성.bat다음 내용이 포함된 포장지

    @rem test.bat
    @powershell -file test.ps1 %1 %2 %3 %4
    

    그런 다음에 다음과 같이 부릅니다.

    test.bat -x "hello world" -y "my friend"
    

백틱을 사용하여 공백을 이스케이프할 수 있습니다.

PS & C:\Program` Files\\....

제 경우 가능한 해결책은 단일 인용문과 이중 인용문을 내포하는 것이었습니다.

test.ps1 -x '"Hello, World!"' -y '"my friend"'

저도 비슷한 문제가 있었지만, 저의 경우 cmdlet을 실행하려고 했고, 통화는 Cake 스크립트 내에서 이루어지고 있었습니다.이 경우 단일 따옴표와-file인수가 작동하지 않았습니다.

powershell Get-AuthenticodeSignature 'filename with spaces.dll'

결과 오류:Get-AuthenticodeSignature : A positional parameter cannot be found that accepts argument 'with'.

가능하다면 배치 파일은 피하고 싶었습니다.

해결책

/S와 함께 cmd 래퍼를 사용하여 외부 따옴표를 풀었습니다.

cmd /S /C "powershell Get-AuthenticodeSignature 'filename with spaces.dll'"

언급URL : https://stackoverflow.com/questions/28311191/how-do-i-pass-in-a-string-with-spaces-into-powershell

반응형