【问题标题】:Pass whole script as argument to powershell.exe将整个脚本作为参数传递给 powershell.exe
【发布时间】:2017-12-04 17:22:55
【问题描述】:

我曾经找到一个脚本,如果我使用多个壁纸,可以轻松更改我的壁纸。

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.exe 命令行执行脚本,请使用 -File 参数。 (您必须在 powershell.exe 命令行最后指定 -File。)
  • 那是 vbscript.. 不是 powershell
  • 我请尊敬的先生参考已经发布的评论,它指的是 powershell.exe 命令行(仅此而已)。

标签: powershell command-line vbscript arguments


【解决方案1】:

在 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}')"

【讨论】:

  • 谢谢你,那个单线正是我要找的!
猜你喜欢
  • 2016-05-04
  • 2017-07-03
  • 1970-01-01
  • 2017-11-23
  • 2013-12-12
  • 2017-09-26
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
相关资源
最近更新 更多