source

PowerShell을 통해 .cmd 파일 실행

ittop 2023. 8. 5. 11:01
반응형

PowerShell을 통해 .cmd 파일 실행

PowerShell로 원격 서버에서 .cmd 파일을 실행하려고 합니다.

.ps1 스크립트에서 저는 다음을 시도했습니다.

C:\MyDirectory\MyCommand.cmd

다음 오류가 발생합니다.

C:\MyDirectory\MyCommand.cmd is not recognized as the name of a cmdlet,
function, script file, or operable program.

그리고 이것은

Invoke-Command C:\MyDirectory\MyCommand.cmd

다음 오류가 발생합니다.

Invoke-Command : Parameter set cannot be resolved using the specified named
parameters.

PowerShell 스크립트에 매개 변수를 전달할 필요가 없습니다.내가 찾고 있는 올바른 구문은 무엇입니까?

Invoke-Item 파일 형식에 대한 기본 처리기를 찾아 실행하도록 지시합니다.

기본적으로 탐색기에서 파일을 두 번 클릭하거나 사용하는 것과 동일합니다.start.exe.

C로 이동:\내 디렉토리에서 다음을 시도합니다.

.\MyCommand.cmd

호출 시도cmd /c C:\MyDirectory\MyCommand.cmd그것은 효과가 있을 겁니다.

배치 파일을 실행하거나 PowerShell로 변환(특히 예약된 모든 작업 스크립트에 인증서를 서명하려는 경우)예를 들어 delete folders.ps1과 같은 PowerShell 스크립트를 생성합니다.

스크립트에 다음을 입력합니다.

cmd.exe /c "rd /s /q C:\#TEMP\test1"

cmd.exe /c "rd /s /q C:\#TEMP\test2"

cmd.exe /c "rd /s /q C:\#TEMP\test3"

*각 명령은 cmd.exe를 다시 호출하여 새 줄에 놓아야 합니다.

이제 PowerShell 출력에서 /cmd 명령 프롬프트로 직접 명령을 출력하여 이 스크립트를 서명하고 실행할 수 있습니다.

배치 파일을 실행하는 것보다 훨씬 안전한 방법입니다!

먼저 다음 폴더까지 접근할 수 있습니다: cd 'C:\MyDirectory'를 선택한 다음 ./MyCommand.cmd를 사용합니다.

언급URL : https://stackoverflow.com/questions/20423424/run-a-cmd-file-through-powershell

반응형