2012-07-12 42 views
4

嘿,我想从一个vbs脚本运行一个powershell命令。像启动powershell.exe,并输入一个特定的命令,如重新启动服务。我以为类似的东西可以工作:从命令行运行Powershell作为参数

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) 

有人的想法我怎么能管理这个?

+1

这将帮助:http://ss64.com/vb/run.html – 2012-07-12 09:13:13

+1

我已经设置了这样的'点心objShell 设置objShell = WScript.CreateObject( “WScript.shell”) objShell。运行“C:\ WINDOWS \ system32 \ WindowsPowerShell \ v1.0 \ powershell.exe&重新启动服务BranchCache” Set objShell = Nothing'但是不起作用。 – Alesfatalis 2012-07-12 09:28:10

+1

看看PowerShell.exe的命令行参数。使用一个告诉PowerShell运行一个命令... – 2012-07-12 09:42:05

回答

2

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

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

这是我尝试的最后一件事,当我无法让它工作时,我使用你的方法。 – Alesfatalis 2012-07-12 10:59:48

+0

@HariHaran你好,你知道为什么这可能工作时,通过'objShell.Run“powershell -file”“c:\ scripts \ test.ps1”“”,0,true'没有? – Bassie 2016-03-16 17:23:32

3

试试这个:

powershell -command '& {command to run}' 

/G

+0

我认为它可行,但我注意到我需要成为管理员来启动/停止服务如何以不同的用户身份运行这部分的vbs? – Alesfatalis 2012-07-12 11:22:21

+0

我不太确定。你可以尝试这样的事情:http://stackoverflow.com/questions/1566969/showing-the-uac-prompt-in-powershell-if-the-action-requires-elevation – Gisli 2012-07-12 13:46:26

+0

我觉得还有一个开关( - 动词runas)。尝试使用谷歌搜索 – Gisli 2012-07-12 13:52:20

0

试试这个:

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

或存放的文件中与PS exe相同的文件夹,然后

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

或者你可以使用objShell.Run(“powershell.exe -noexit -file c:\ scripts \ test.ps1”) – abhinov 2013-10-03 10:50:17