简体   繁体   English

将整个脚本作为参数传递给powershell.exe

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

I once found a script to easily change my Wallpaper if I use multiple ones. 我曾经找到一个脚本,如果我使用多个墙纸,可以轻松更改我的墙纸。

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

However I need a separate link that calls the script. 但是,我需要一个单独的链接来调用脚本。 Now I wonder if I can pass these multiple lines of code as parameter to powershell.exe directly. 现在,我想知道是否可以将这些多行代码作为参数直接传递给powershell.exe。

I did read that I can allign multiple lines using ; 我确实读过我可以使用来声明多行内容; and tried building the one-liner, however it doesn't run...or rather it does run, there is no feedback, nothing changes. 并尝试构建单线,但是它没有运行……或者说它确实在运行,没有反馈,没有任何变化。 If I paste the command line into a running powershell instance it just exits. 如果我将命令行粘贴到正在运行的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}')"

I basically just concatenated them and replaced the double quotes with single quotes to escape the special characters, so why does it not work as the separate script? 我基本上只是将它们连接起来,并用单引号替换了双引号以转义特殊字符,那么为什么它不能作为单独的脚本工作?

In PowerShell: 在PowerShell中:

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

Save this as a script ( Filename.ps1 ), and call it from a .bat or .cmd file: 将其另存为脚本( Filename.ps1 ),然后从.bat.cmd文件调用它:

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

Alternatively, as a one-liner in a command file: 或者,作为命令文件中的单行代码:

%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