簡體   English   中英

將整個腳本作為參數傳遞給powershell.exe

[英]Pass whole script as argument to powershell.exe

我曾經找到一個腳本,如果我使用多個牆紙,可以輕松更改我的牆紙。

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys("^ ")
WshShell.SendKeys("+{F10}")
WshShell.SendKeys("n")
WshShell.SendKeys("{ENTER}")

但是,我需要一個單獨的鏈接來調用腳本。 現在,我想知道是否可以將這些多行代碼作為參數直接傳遞給powershell.exe。

我確實讀過我可以使用來聲明多行內容; 並嘗試構建單線,但是它沒有運行……或者說它確實在運行,沒有反饋,沒有任何變化。 如果我將命令行粘貼到正在運行的Powershell實例中,它將退出。

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -Command "set WshShell = WScript.CreateObject('WScript.Shell'); WshShell.SendKeys('^ '); WshShell.SendKeys('+{F10}'); WshShell.SendKeys('n'); WshShell.SendKeys('{ENTER}')"

我基本上只是將它們連接起來,並用單引號替換了雙引號以轉義特殊字符,那么為什么它不能作為單獨的腳本工作?

在PowerShell中:

$WSH = New-Object -ComObject 'WScript.Shell'
$WSH.SendKeys('^ ')
$WSH.SendKeys('+{F10}')
$WSH.SendKeys('n')
$WSH.SendKeys('{ENTER}')

將其另存為腳本( Filename.ps1 ),然后從.bat.cmd文件調用它:

%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -File "path\filename.ps1"

或者,作為命令文件中的單行代碼:

%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -Command "$WSH = New-Object -ComObject 'WScript.Shell';$WSH.SendKeys('\^ ');$WSH.SendKeys('+{F10}');$WSH.SendKeys('n');$WSH.SendKeys('{ENTER}')"

暫無
暫無

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

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