2010-08-12 3 views
3

나는 실행하고자하는 일련의 명령을 가지고 결과에 대한 리턴 코드 검사를하므로 실행을 위해 배열에 넣는 것이 쉽다는 것을 알았습니다.Powershell : 배열의 항목에서 매개 변수가있는 exe를 호출 하시겠습니까?

의 예로 들어이 하나 보자 : 나는 어떤 따옴표없이 내 배열에 넣을 때 지금

C:\windows\system32\inetsrv\AppCmd.exe set config "Default Web Site/" /section system.webServer/webdav/authoring /enabled:true /commit:apphost 

을, 그것은 즉시 명령이 실행되는 명령으로 해석이야와 결과는에 기록된다 배열 :

$commands = @() 
$commands += C:\windows\system32\inetsrv\AppCmd.exe set config "Default Web Site/" /section system.webServer/webdav/authoring /enabled:true /commit:apphost 

따옴표를 사용하여 문자열로 배열에 넣으면 실행하려고 시도하지 않습니다.

PS> $commands = @() 
PS> $commands += "C:\windows\system32\inetsrv\AppCmd.exe set config ""Default Web Site/"" /sectio n:system.webServer/webdav/authoring /enabled:true /commit:apphost" 
PS> $commands[0] 
C:\windows\system32\inetsrv\AppCmd.exe set config "Default Web Site/" /section:system.webServer/webdav/authoring /enabled:true /commit:apphost 
PS> & $commands[0] 
The term 'C:\windows\system32\inetsrv\AppCmd.exe set config "Default Web Site/" /section:system.webServer/webdav/authoring /enabled:true /commit:apphost' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 
     At line:1 char:2 
     + & <<<< $commands[0] 
      + CategoryInfo   : ObjectNotFound: (C:\windows\syst.../commit:apphost:String) [], CommandNotFoundException 
      + FullyQualifiedErrorId : CommandNotFoundException 

그래서, 내가 어떻게 올바르게 배열, 큐에이 명령을 넣어 않거나 적절한시기에 어떤 가장은 그래서 그것을 실행할 수 있습니다 적합?

답변

3

호출 연산자 &을 사용할 때 다음 문자열은 실행될 명령 만 지정해야하며 매개 변수는 지정하지 않아야합니다. 사용자가 설정 한 내용으로 대신 예를 들어, 호출 표현을 사용할 수 있습니다 : 호출 표현에 대한

iex $command[0] 

한 경고가 원치 않는 명령을 주입하는 데 사용할 수있는 모든 입력은 사용자로부터 오는 경우이 명령을 사용하여 조심 예 : 음

Read-Host "Enter commit value" 
Enter commit value: apphost; remove-item c:\ -r -force -ea 0 -confirm:0 # 
+0

아, invoke-expression입니다. 나는 invoke-command를 시도했다. 다음 번에 더 잘 문서를 읽어야합니다. 감사! –

0

, 내가 늦게 파티에 연간 해요,하지만 당신은이 작업을 수행 할 수 있습니다

새로운 별명 -name 원숭이 - 값 "$의 ENV : WINDIR \ system32를 \ inetsrv를 \ APPCMD 명령어 .exe "

0

심지어 나중에 : Roman Kuzmin 사용자가 this answerthis other answer을 입력하면 변수에 실행 파일 이름 및 모든 매개 변수를 배열에 저장하여이 문제를 자세히 처리 할 수 ​​있습니다.

관련 문제