명령 프롬프트에서 PowerShell 스크립트에 부울 값을 전달하는 방법
배치 파일에서 PowerShell 스크립트를 호출해야합니다. 스크립트에 대한 인수 중 하나는 부울 값입니다.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -File .\RunScript.ps1 -Turn 1 -Unify $false
다음 오류와 함께 명령이 실패합니다.
Cannot process argument transformation on parameter 'Unify'. Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.
At line:0 char:1
+ <<<< <br/>
+ CategoryInfo : InvalidData: (:) [RunScript.ps1], ParentContainsErrorRecordException <br/>
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,RunScript.ps1
지금은 내 스크립트 내에서 부울 변환 문자열을 사용하고 있습니다. 그러나 부울 인수를 PowerShell에 어떻게 전달할 수 있습니까?
-File매개 변수를 사용할 때 powershell.exe가 스크립트 인수를 완전히 평가하지 않는 것으로 보입니다 . 특히, $false인수는 아래 예제와 유사한 방식으로 문자열 값으로 처리됩니다.
PS> function f( [bool]$b ) { $b }; f -b '$false'
f : Cannot process argument transformation on parameter 'b'. Cannot convert value
"System.String" to type "System.Boolean", parameters of this type only accept
booleans or numbers, use $true, $false, 1 or 0 instead.
At line:1 char:36
+ function f( [bool]$b ) { $b }; f -b <<<< '$false'
+ CategoryInfo : InvalidData: (:) [f], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,f
를 사용하는 대신 -File시도해 볼 수 있습니다. -Command그러면 호출이 스크립트로 평가됩니다.
CMD> powershell.exe -NoProfile -Command .\RunScript.ps1 -Turn 1 -Unify $false
Turn: 1
Unify: False
David가 제안 했듯이 switch 인수를 사용하는 것도 더 관용적이어서 부울 값을 명시 적으로 전달할 필요를 제거하여 호출을 단순화합니다.
CMD> powershell.exe -NoProfile -File .\RunScript.ps1 -Turn 1 -Unify
Turn: 1
Unify: True
보다 명확한 사용법은 대신 스위치 매개 변수를 사용하는 것입니다. 그런 다음 Unify 매개 변수가 있다는 것만으로 설정되었음을 의미합니다.
이렇게 :
param (
[int] $Turn,
[switch] $Unify
)
매개 변수 유형을 다음과 같이 설정해보십시오 [bool].
param
(
[int]$Turn = 0
[bool]$Unity = $false
)
switch ($Unity)
{
$true { "That was true."; break }
default { "Whatever it was, it wasn't true."; break }
}
이 예는 기본 설정 $Unity에 $false아무런 입력이 제공되지 않는 경우.
용법
.\RunScript.ps1 -Turn 1 -Unity $false
이것은 오래된 질문이지만 실제로 PowerShell 설명서에 이에 대한 답변이 있습니다. 나는 똑같은 문제가 있었고 한 번 RTFM이 실제로 그것을 해결했습니다. 거의.
Documentation for the -File parameter states that "In rare cases, you might need to provide a Boolean value for a switch parameter. To provide a Boolean value for a switch parameter in the value of the File parameter, enclose the parameter name and value in curly braces, such as the following: -File .\Get-Script.ps1 {-All:$False}"
I had to write it like this:
PowerShell.Exe -File MyFile.ps1 {-SomeBoolParameter:False}
So no '$' before the true/false statement, and that worked for me, on PowerShell 4.0
I think, best way to use/set boolean value as parameter is to use in your PS script it like this:
Param(
[Parameter(Mandatory=$false)][ValidateSet("true", "false")][string]$deployApp="false"
)
$deployAppBool = $false
switch($deployPmCmParse.ToLower()) {
"true" { $deployAppBool = $true }
default { $deployAppBool = $false }
}
So now you can use it like this:
.\myApp.ps1 -deployAppBool True
.\myApp.ps1 -deployAppBool TRUE
.\myApp.ps1 -deployAppBool true
.\myApp.ps1 -deployAppBool "true"
.\myApp.ps1 -deployAppBool false
#and etc...
So in arguments from cmd you can pass boolean value as simple string :).
You can also use 0 for False or 1 for True. It actually suggests that in the error message:
Cannot process argument transformation on parameter 'Unify'. Cannot convert value
"System.String"to type"System.Boolean", parameters of this type only accept booleans or numbers, use$true,$false, 1 or 0 instead.
For more info, check out this MSDN article on Boolean Values and Operators.
'developer tip' 카테고리의 다른 글
| '모듈'에 'urlencode'속성이 없습니다. (0) | 2020.09.11 |
|---|---|
| Web.Config에서 변수 읽기 (0) | 2020.09.11 |
| Visual Studio 새 브라우저 인스턴스를 열지 않음 (0) | 2020.09.11 |
| Makefile, 헤더 종속성 (0) | 2020.09.11 |
| Visual Studio 확장을 디버깅하는 방법 (0) | 2020.09.11 |