【发布时间】:2016-11-02 10:57:13
【问题描述】:
我正在尝试创建一个运行如下命令的函数:
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
print runCommand("git --help")
function runCommand(commandStr)
set objShell = CreateObject("Wscript.Shell")
Set objExec = objShell.Exec(commandStr)
Do Until objExec.Status
Wscript.Sleep 10
Loop
runCommand = objExec.StdOut.ReadAll()
end function
sub print(str)
stdout.WriteLine str
end sub
效果很好,但是我想在更高级别使用 objShell,所以我决定将 objShell 设为全局:
set objShell = CreateObject("Wscript.Shell")
Set fso = CreateObject ("Scripting.FileSystemObject")
Set stdout = fso.GetStandardStream (1)
print runCommand(objShell.CurrentDirectory)
print runCommand("git --help")
function runCommand(commandStr)
Set objExec = objShell.Exec(commandStr)
Do Until objExec.Status
Wscript.Sleep 10
Loop
runCommand = objExec.StdOut.ReadAll()
end function
sub print(str)
stdout.WriteLine str
end sub
但是,现在当我运行它时,我得到了错误:
WshShell.Exec: Access is denied.
它引用了set objShell = CreateObject("Wscript.Shell") 行。如果我尝试创建两个不同的变量 objShell 和 objShell2 我会得到同样的错误。我该如何解决这个问题?
【问题讨论】:
-
在这种情况下,您需要去掉函数开头的“set objShell = CreateObject("Wscript.Shell")”!不要做两次!
-
@duDE 对不起,那是一个类型-o。删除。行为仍然失败。我错过了我还添加的打印功能。
标签: windows vbscript windows-7