簡體   English   中英

如何讓Powershell運行批處理文件然后保持打開狀態?

[英]How do I make Powershell run a batch file and then stay open?

例如; 使用舊的命令提示符,它將是:

cmd.exe /k mybatchfile.bat

放入cmd實例(或者實際上是PowerShell本身)並輸入:

powershell -?

你會看到powershell.exe有一個“-noexit”參數,告訴它在執行“啟動命令”后不要退出。

運行PowerShell.exe時只需提供-NoExit開關,如下所示:

PowerShell -NoExit -File "C:\SomeFolder\SomePowerShellScript.ps1"

PowerShell -NoExit -Command "Write-Host 'This window will stay open.'"

或者,如果要運行文件然后運行命令並使窗口保持打開狀態,則可以執行以下操作:

PowerShell -NoExit "& 'C:\SomeFolder\SomePowerShellScript.ps1'; Write-Host 'This window will stay open.'"

如果沒有提供-Command參數,這里我們使用來調用PowerShell腳本,並且; 分離PowerShell命令。

此外,在我的博客文章的底部, 顯示了一個快速的注冊表更改,您可以在執行腳本/命令后始終保持打開PowerShell,這樣您就不需要始終顯式提供-NoExit開關所有時間。

我相信你已經弄清楚了,但我發布了它

$CreateDate = (Get-Date -format 'yyyy-MM-dd hh-mm-ss')

$RemoteServerName ="server name"
$process = [WMICLASS]"\\$RemoteServerName\ROOT\CIMV2:win32_process"  
$result = $process.Create("C:\path to a script\test.bat") 
$result | out-file -file "C:\some path \Log-$CreatedDate.txt"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM