【问题标题】:Running Powershell from vbs with command as parameter使用命令作为参数从 vbs 运行 Powershell
【发布时间】:2012-07-12 09:01:54
【问题描述】:

嘿,我想从一个 vbs 脚本运行一个 powershell 突击队。诸如启动 powershell.exe 并输入特定命令(例如 Restart-Service)。我认为类似的东西可以工作:

strCommand = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command Restart-Service [service name]"

    Set WshShell = WScript.CreateObject("WScript.Shell") 
    Set objExec = WshShell.Exec(strCommand)

有人知道我该如何管理吗?

【问题讨论】:

  • 这会有所帮助:ss64.com/vb/run.html
  • 我已经这样设置了 Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") objShell.run "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe & Restart-Service BranchCache" Set objShell = Nothing 但它不起作用。
  • 查看PowerShell.exe的命令行参数。使用它来告诉 PowerShell 运行命令...
  • 好的,我现在已经查找参数并设置如下:Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") objShell.run "PowerShell -Command {Start-Service PeerDistSvc}" 当我运行 vbs 时,它会运行并弹出并关闭 cmd 窗口,仅此而已,但服务没有'不开始我做错了什么?我是计算机的管理员。
  • 为我工作...

标签: powershell vbscript


【解决方案1】:

1) 将您的 powershell 命令存储为 powershell 脚本。
2)使用vbs运行powershell

  Set objShell = CreateObject("Wscript.Shell")
 objShell.Run("powershell.exe -noexit c:\scripts\test.ps1")

【讨论】:

  • 这是我在无法正常工作时尝试的最后一件事,我使用你的方法。
  • @HariHaran 你知道为什么当通过objShell.Run "powershell -file ""c:\scripts\test.ps1""", 0, true 的呼叫不起作用时这可能会起作用吗?
  • C:\Users\lpeder6\Desktop\PowerShell Scripts\GUI Files\Basic GUI Script - Combo Box.ps1 在 Windows PowerShell ISE 中打开,但当我从 VBScript 调用它时没有原因(例如:objShell.Run ("powershell.exe -noexit C:\Users\lpeder6\Desktop\PowerShell Scripts\GUI Files\Basic GUI Script - Combo Box.ps1"))?
【解决方案2】:

试试这个:

powershell -command '& {command to run}'

/G

【讨论】:

  • 我认为它有效,但我注意到我需要成为管理员才能启动/停止服务如何以不同用户身份运行 vbs 的这一部分?
  • 我不太确定。你可以试试这样的:stackoverflow.com/questions/1566969/…
  • 我认为还有一个开关(-verb runas)。尝试谷歌搜索
【解决方案3】:

您可以使用最短的版本。

CreateObject("Wscript.Shell").Run ("powershell.exe -noexit $c= 'lola'; write-host $c -ForegroundColor green")

【讨论】:

    【解决方案4】:

    试试这个:

    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run("powershell.exe -noexit .\c:\scripts\test.ps1")
    

    或者将文件保存在PS exe所在的同一个文件夹中,然后

    Objshell.Run("powershell.exe -noexit .\test.ps1")
    

    【讨论】:

    • 或者你可以使用 objShell.Run("powershell.exe -noexit -file c:\scripts\test.ps1")
    【解决方案5】:

    在研究如何使用 VBScript 运行 PowerShell 脚本时,我遇到了如下所示的问题:

    从那以后我就知道为什么它不起作用了。 PowerShell 无法读取脚本位置中的空格。如上图所示,当完整路径为C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays - Clear Method.ps1 时,错误在C:\Users\lpeder6\Desktop\PowerShell_Scripts\Arrays\Arrays 处停止。

    从我的路径名中删除所有空格和多余字符后,使用 VBScript 运行 PowerShell 脚本可以使用以下代码:

    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run("powershell.exe -noexit -command C:\Users\lpeder6\Desktop\PowerShellScripts\GUIFiles\BasicGUIScript-ComboBox.ps1"),1,True
    

    当我运行它时,我得到的是:

    我希望这对某人有所帮助,因为这是我真正可以使用的信息。 :)

    【讨论】:

    • 不用改路径,如果不能改路径,有空格怎么办?见Spaces cause split in path with PowerShell
    • @user692942 成功了!我从来没有听说过这样的事情,它在我们一直在研究的其他解决方案的开发过程中引起了很多问题。这将真正帮助我们使用 PowerShell 继续前进。
    猜你喜欢
    • 2013-04-11
    • 2023-04-06
    • 1970-01-01
    • 2015-08-17
    • 2014-05-29
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多