특정 명명 된 매개 변수를 따르는 값 모자를 수집하는 함수를 작성하고 있습니다. 코드는-weatch와 정규 표현식에 관한 powershell의 형변환 문제
[email protected]("ddddd","-anotherswitch2","djfjdsfa","-recurse",1,2,3,"-anotherswitch","-recurse",5,6, "-anotherswitch","kslsdfgvl")
$searchswitch="-recurse"
[email protected]()
$matches=0
$counter=0
while (($Counter -lt $commandline.length-1) -AND ($matches -lt 1))
{
if ($commandline[$counter] -eq $searchswitch)
{
$matches++
$counter++
# while argument does not start with "-" and we haven't reached the end of the array
While(-NOT ($commandline[$counter] -match "^-") -AND ($counter -lt $commandline.length))
{
$Paths+= $($commandline[$counter])
$counter++
}
}
else
{
$counter++
}
}
$Paths
코드는 예상대로 거의 작동 (데모 목적으로이 등 매개 변수를 구문 분석하지 않고만을 발췌 한 것입니다)하지만, 다음과 같은 오류 메시지가 발생 다음과 같다.
Der Wert "System.Collections.Hashtable" vom Typ "System.Collections.Hashtable" kann nicht in den Typ "System.Int32" konvertiert werden.
Bei D:\Unbenannt5.ps1:18 Zeichen:67
+ While(-NOT ($commandline[$counter] -match <<<< "^-") -AND ($counter -lt $commandline.length))
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
1
2
3
오류 메시지가 "선택 System.Int32" 유형으로 변환 할 수없는 유형의 값 "System.Collections.Hashtable" "System.Collections.Hashtable"의 독일어 버전입니다.
나는 이것을 명령 행 배열의 8 번째 요소 (-anotherswitch)로 좁혔다. 아무도 도와 줄 수 있니? 미리 감사드립니다.
네, 맞아요. 필자는 §args 기반 접근 방식에이 기능을 부여하여 함수에 구문 분석을 수행하는 기본 Powershell 매개 변수를 얻으려고했습니다. 'param()'으로 네이티브 powershell 접근법을 사용하면 전체 함수는 쓸모 없게됩니다 :-) 그러나 나는 에러의 원인을 아는 데 관심이 있습니다. 매개 변수 이름에 하이픈 - 빼기 기호를 사용해야 할 수도 있습니다. – user3210625