如何在線運行我的 powershell 腳本?

of_0691

我想執行 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);

我想在線獲得相同的結果,但我遇到了這個問題

克萊門特0

您的代碼有 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在運行時在 powershell 腳本中顯示 xcopy 的輸出

Powershell 運行先前編輯的腳本

如何在不顯示窗口和超時問題的情況下運行 powershell 腳本

PowerShell腳本中的隨機換行符

如何跳過PowerShell腳本中的空值

Powershell 腳本 - 如何讓此腳本僅輸出與某個字符串匹配的值

Python 運行與我的腳本不同的腳本

LUA - 如何停止運行腳本

如何在 shell 腳本中運行 sacct 命令

如何在多個數據目錄上運行我的腳本

用於獲取正在運行的虛擬機列表並停止它們的 Powershell 腳本

Powershell - 如何將變量傳遞給腳本塊

如何安排從anaconda運行的python腳本?

Python InDesign 腳本:如何運行預檢?

如何從 Python 腳本運行 nohup 命令?

導入後如何運行腳本?

如何選擇 CPU 來運行 python 腳本?

如何與始終運行的 python 腳本通信

有沒有辦法每次雙擊使用我的 PowerShell 編譯腳本啟動文件?

將外部 C# 腳本添加到 PowerShell 腳本作為源

如何在 AWS CloudFormation 模板中運行 bash 腳本

Laravel 8,如何在後台運行一個作業(腳本)?

如何在 Mac 上從 Dockerfile 運行 bash 腳本

當我運行整個腳本時,powrshell 命令不運行

當我將 PowerShell 設置為默認應用程序時,為什麼我的某些 PowerShell 腳本停止工作?

對 Powershell 腳本獲取內容感到困惑

用於刪除 Microsoft Team 的 Powershell 腳本

用於電子郵件通知的 PowerShell 腳本

將 Powershell 腳本轉換為 jenkins 作業