it-source

인수 배열을 Powershell 명령줄에 전달하는 방법

criticalcode 2023. 11. 2. 21:49
반응형

인수 배열을 Powershell 명령줄에 전달하는 방법

파워셸 스크립트 파일에 인수 배열을 전달하려고 합니다.

저는 명령 줄에서 이렇게 명령 줄을 통과하려고 했습니다.

Powershell - 파일 "InvokeBuildscript.ps1" "z:\" "Component1", "Component2"

하지만 보이는 매개변수를 사용하지는 않습니다.제가 무엇을 빠뜨리고 있나요?여러 가지 논쟁을 통과시키는 방법은?

짧은 답변: 큰따옴표를 더 많이 사용하는 것이 도움이 될 수 있습니다.

스크립트가 "test.ps1"이라고 가정합니다.

param(

    [Parameter(Mandatory=$False)]
    [string[]] $input_values=@()

)
$PSBoundParameters

배열 @(123, abc", x,y,z")을 전달한다고 가정합니다.

Powershell 콘솔에서 여러 값을 배열로 전달하려면 다음과 같이 하십시오.

.\test.ps1 -input_values 123,abc,"x,y,z"

Windows 명령 프롬프트 콘솔 또는 Windows 작업 스케줄러에서 큰따옴표는 세 개의 큰따옴표로 바꿉니다.

powershell.exe -Command .\test.ps1 -input_values 123,abc,"""x,y,z"""

누군가에게 도움이 될 수 있기를 바랍니다.

해라

Powershell -command "c:\pathtoscript\InvokeBuildscript.ps1" "z:\" "Component1,component2"

한다면test.ps1다음과 같습니다.

$args[0].GetType()
$args[1].gettype()

도스 셸에서 다음과 같이 부릅니다.

C:\>powershell -noprofile -command  "c:\script\test.ps1" "z:" "a,b"

반환:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
True     True     Object[]                                 System.Array

배열 변수를 명령줄 인수로 전달할 수도 있습니다.예:

다음 Powershell 모듈을 고려합니다.

파일: PrintElements.ps1

Param(
    [String[]] $Elements
)

foreach($element in $Elements)
{
   Write-Host "element: $element"
}

위의 파워셸 모듈을 사용하려면:

#Declare Array Variable
[String[]] $TestArray = "Element1", "Element2", "Element3"

#Call the powershell module
.\PrintElements.ps1 $TestArray

TestArray를 연결하여 공간으로 구분된 요소의 단일 문자열로 전달하려면 아래와 같이 인수를 따옴표로 둘러싸 PS 모듈을 호출할 수 있습니다.

#Declare Array Variable
[String[]] $TestArray = "Element1", "Element2", "Element3"

#Call the powershell module
.\PrintElements.ps1 "$TestArray"

언급URL : https://stackoverflow.com/questions/13264369/how-to-pass-array-of-arguments-to-powershell-commandline

반응형