vbscript中的对象必需错误

时间:2012-07-13 09:53:23

标签: powershell vbscript

我收到此错误: Image

当我尝试运行此代码时:

strComputer = "server01"
strCommand = "powershell.exe -NoLogo -Command Start-Service -InputObject $(Get-Service -Computer " & strComputer & " -Name " & strService & ")"

Set WshShell = WScript.CreateObject("WScript.Shell") 
Set objExec = WshShell.Run(strCommand,0,True)

我想在隐藏窗口中运行strCommand。这可以解决代码运行并做它应该做的事情,但是这个错误发生了,这真的很糟糕。

2 个答案:

答案 0 :(得分:1)

.Run() - 与.Exec()相反 - 返回错误代码(不是对象)。所以你不能使用

Set objExec = WshShell.Run(strCommand,0,True)

nErrorCode = WshShell.Run(strCommand,0,True)

答案 1 :(得分:0)

您需要在命令之前和结尾处添加双引号:

strCommand = "powershell.exe -NoExit -NoLogo -Command ""Start-Service -PassThru -InputObject (Get-Service -Computer " & strComputer & " -Name " & strService & ")"""