1

我收到此错误: 图片

当我尝试运行此代码时:

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 。这可以使代码运行并执行它应该执行的操作,但是会发生此错误,这真的很糟糕。

4

2 回答 2

1

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

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

nErrorCode = WshShell.Run(strCommand,0,True)
于 2012-07-13T10:04:54.500 回答
0

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

strCommand = "powershell.exe -NoExit -NoLogo -Command ""Start-Service -PassThru -InputObject (Get-Service -Computer " & strComputer & " -Name " & strService & ")"""
于 2012-07-13T10:10:33.577 回答