인수 배열을 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
'it-source' 카테고리의 다른 글
어린이 중 한 명이 포커스를 받을 경우 블러 이벤트가 발생하지 않도록 합니다. (0) | 2023.11.02 |
---|---|
Using RepositoryRestResource annotation to change RESTful endpoint not working (0) | 2023.11.02 |
Android M - 런타임 권한 확인 - 사용자가 "다시 묻지 않음"을 선택했는지 확인하는 방법은? (0) | 2023.10.28 |
32비트 응용 프로그램은 64비트 Linux에서 시스템 호출을 어떻게 합니까? (0) | 2023.10.28 |
셀러리 없는 장고에서의 백그라운드 처리 (0) | 2023.10.28 |