使用VBscript从CMD提示符执行Exchange PowerShell命令

时间:2014-10-21 02:02:54

标签: windows powershell vbscript cmd

我正在创建一个VBScript,它将从命令提示符运行Exchange PowerShell命令。 当我尝试运行下面的脚本时,expected end of statement出现了。

Dim oShell 
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe ""C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"" -psconsolefile ""C:\Program Files\Microsoft\Exchange Server\V14\Bin\exshell.psc1"" -file ""C:\script\script.ps1"" "

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

(1)<br>必须去。

(2)避免你的报价同步,如

... -file """C:\script\script.ps1" "

使用更有条理的方式构建复杂的字符串:

Option Explicit

Function qq(s) : qq = """" & s & """" : End Function

Dim oShell : Set oShell = WScript.CreateObject ("WScript.Shell")
Dim sCmd   : sCmd       = Join(Array( _
     "%comspec%" _
   , "/c" _
   , qq("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe") _
   , "-psconsolefile" _
   , qq("C:\Program Files\Microsoft\Exchange Server\V14\Bin\exshell.psc1") _
   , "-file" _
   , qq("C:\script\script.ps1") _
))
WScript.Echo sCmd

输出:

cscript 26477799.vbs
%comspec% /c "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -psconsolefile "C:\Program Files\Microsoft\Exchange Server\V14\Bin\exshell.psc1" -file "C:\script\script.ps1"
相关问题