我想執行 powershell inline ,但是當我嘗試執行所有內容時。我有Out-Null : A positional parameter cannot be found that accepts argument 'do'.
$OutputString = 'Hello World'
$WScript = New-Object -ComObject 'wscript.shell'
$WScript.Run('notepad.exe') | Out-Null
do {
Start-Sleep -Milliseconds 100
}
until ($WScript.AppActivate('notepad'))
$WScript.SendKeys($OutputString)
內聯powershell
powershell -Command $OutputString = 'Hello World';$WScript = New-Object -ComObject 'wscript.shell';$WScript.Run('notepad.exe') | Out-Null ;do{ Start-Sleep -Milliseconds 100};until($WScript.AppActivate('notepad'));$WScript.SendKeys($OutputString);
我想在線獲得相同的結果,但我遇到了這個問題
您的代碼有 3 個問題:
;
前缺少一個do
(;
在 PowerShell 中放置在一行上的多個語句必須用-separated 分隔)
until
之後缺少關鍵字do { ... }
;
,這會破壞do
語句。的未轉義使用|
,這意味著它cmd.exe
本身解釋了它,而不是預期的 PowerShell(我假設您是從 調用它cmd.exe
,否則就不需要通過 調用 PowerShell 的 CLI powershell.exe
)。
|
必須轉義^|
才能cmd.exe
將其傳遞給 PowerShell。修復這些問題產生的使用下面的命令來自cmd.exe
:
# From cmd.exe / a batch file:
powershell -Command $OutputString = 'Hello World'; $WScript = New-Object -ComObject 'wscript.shell'; $WScript.Run('notepad.exe') ^| Out-Null; do{ Start-Sleep -Milliseconds 100} until ($WScript.AppActivate('notepad')); $WScript.SendKeys($OutputString)
如果你想運行你的代碼從PowerShell中,你可以調用它直接,如在你的問題的第一個片段-有沒有必要建立另一個通過PowerShell進程powershell.exe
如果出於某種原因,您確實需要創建另一個 PowerShell 進程,請使用腳本塊語法 ( { ... }
) 傳遞命令,在這種情況下不需要轉義:
# From PowerShell
powershell -Command { $OutputString = 'Hello World'; $WScript = New-Object -ComObject 'wscript.shell'; $WScript.Run('notepad.exe') | Out-Null; do{ Start-Sleep -Milliseconds 100} until ($WScript.AppActivate('notepad')); $WScript.SendKeys($OutputString) }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句