2014-04-18 2 views
0

사용자에게 가변 행 set /p asset=을 묻는 배치 파일이 있습니다. 임 내가 보내는 방법 궁금 배치에서 powershell로 변수 전달

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps

SET ThisScriptsDirectory=%~dp0

처럼 내 PowerShell 스크립트를 호출 임 내 배치 파일에서 변수 '자산'PowerShell을. 당신이 매개 변수를 취하는 file.ps1이있는 경우

여기

param($asset) 
"The asset tag is $asset" 

당신은 인수로 변수에 전달할 수 있습니다, 내 .BAT 파일 내용

@Echo off 
cls 
Color E 
cls 

@echo Enter asset below 
set /p asset= 

@echo. 
@echo your asset is %asset% 
@echo. 
goto startusmt 

:startusmt 
@echo. 
@echo executing psexec ... 
@echo. 

SET ThisScriptsDirectory=%~dp0 
SET PowerShellScriptPath=%ThisScriptsDirectory%RemoteUSMT.ps1 
PowerShell -NoProfile -ExecutionPolicy Bypass -file %PowerShellScriptPath% %asset% 
psexec \\%asset% -u domain\username -p password cmd 

goto EOF 

:EOF 
PAUSE 

답변

3

입니다.

SET ThisScriptsDirectory=%~dp0 
SET PowerShellScriptPath=%ThisScriptsDirectory%file.ps1 
SET /p asset=Enter the asset name 

PowerShell -NoProfile -ExecutionPolicy Bypass -file "%PowerShellScriptPath%" "%asset%" 
+0

-file은 무엇을하고 내 명령에는 무엇이 필요합니까? – rangerr

+0

파일이 PowerShell 스크립트를 실행합니다. '-command'에서 파일을 호출하는 대신에'-file'을 사용할 수 있습니다. – Rynant

+0

'PowerShell -NoProfile -ExecutionPolicy Bypass -file "% PowerShellScriptPath %"% asset %'이렇게 하시겠습니까? – rangerr

2

당신은 파워 쉘에서 curent cmd를 환경 변수에 액세스 할 수 $env:variable_name 구문을 사용할 수 있습니다. , set "myvar=my value"을 수행 powershell를 시작 할 당신이 $env:asset
시도하기 위해 사용하는 거라고 당신의 변수의 유지, 개방 cmd를 얻으려면 $env:myvar (이 간단하게 인쇄 할 수 있지만, 물론 당신이 다른 PS 변수로 사용할 수 있습니다)

사이드 노트처럼 ps는 좋은 도움 시스템을 가지고 있습니다. help env을 수행하면 두 개의 관련 주제가 나열되며 자세한 내용은 차례로 조사하여 자세한 정보를 얻을 수 있습니다.

희망 하시겠습니까?

+0

예 옵션입니다. 그러나 일반적으로 PS 스크립트에 전달하는 인수를 사용하면 스크립트가 입력에 의존하는 기능 시그니처를 알 수 있습니다. 또한 PowerShell에서 직접 호출하는 경우 PowerShell 스크립트를 더 유용하게 만듭니다. – Rynant

+0

@Rynant은 일반적으로 네,하지만 언젠가 어딘가에서 당신은 당신이 스크립트를 제어 할 수 있습니다 찾을 수 있지만 그것은 호출/환경 그리고 그것은 편리 :-) – wmz

+0

온다 그래, 내가 그 경우에 유용하게 않습니다 동의합니다. – Rynant