2012-07-12 104 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