2017-01-06 55 views
-3

從CMD批處理文件中調用powershell命令,但CMD保持PowerShell模式。如何退出PowerShell模式(在命令提示符下)並繼續批處理文件? 當我運行當前批處理文件時,必須鍵入exit才能退出powershell會話。然後批處理文件繼續停止。如何在ps1運行後回到CMD提示恢復批處理文件

+5

歡迎堆棧溢出。請包含您正在尋求幫助的代碼。這裏是關於包含[mcve] – BenH

+2

的指南是的,如果你運行'PowerShell -Command ...',它會在命令完成時退出。你在做什麼呢? – Jaykul

+0

這是一個批處理文件,用於調用其他包含powershell命令的批處理文件。下面的解決方案。 –

回答

1

簡單。

Shell腳本

@echo off 
setlocal 
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -file <full path to PowerShell file> 
echo PowerShell returned rc %ERRORLEVEL%. 
endlocal 

簡單的PowerShell文件

Write-Host -Object 'Hello World'; 
exit (Get-Random -Minimum -10 -Maximum 0); 
+0

謝謝,這工作!但是我必須在powershell.exe之後添加-ExecutionPolicy Bypass。 –