在远程计算机上运行 vbs 代码时没有错误

在远程计算机上运行 vbs 代码时没有错误

我希望此 vbs 代码能够在远程计算机上执行 oShell.run 方法。但事实似乎并非如此,执行此脚本时没有显示任何错误。

on error resume next

dim oShell, strComputer, intProcessID

strComputer = InputBox("IP or Host Name:", "DELETER - [email protected]")

set oShell = WScript.CreateObject("Wscript.shell") & strComputer

oShell.run "cmd.exe /C rd \\%PC%\Users\1*.* /s /q", null, null, intProcessID
oShell.run "cmd.exe /C rd \\%PC%\Users\2*.* /s /q", null, null, intProcessID
oShell.run "cmd.exe /C rd \\%PC%\Users\3*.* /s /q", null, null, intProcessID
set oShell = nothing

在远程计算机上执行这些命令的正确方法是什么?

答案1

首先 - 如果您删除了“出错时继续下一步”,您会立即看到此错误。

接下来,我建议您尝试在远程 PC 上使用文件夹删除方法,而不是运行 cmd。

此后,您无法针对 C:\ 运行 cmd.exe,也无法以某种方式让其针对远程 PC 运行,除非您使用 psexec 或类似程序启动 VBS 并瞄准远程 PC。

我认为这更接近你所追求的:

'First, we get the host name
strComputerName = InputBox ("Enter Hostname or IP to delete from")

'Next, we check the user actually entered a name or IP - you missed this!
if len(strComputerName) < 1 then
    WScript.Echo "No Computer Name Entered - Quitting"
    WScript.Quit
end if

'next, we check it exists - you missed this bit too!  no point in trying to remove folders from a non existant PC
If Reachable(strComputerName) Then
    DeleteAFolder("\\" & strComputerName & "\c$\users\me\desktop\test")
    DeleteAFolder("\\" & strComputerName & "\C$\folder\folder2")
Else 
    WScript.Echo "Computer is Unreachable! - Quitting!"
End If


'This function is the one called to check if the computer is reachable in line 11!
Function Reachable(strComputerName)
    Dim wmiQuery, objWMIService, objPing, objStatus
    wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputerName & "'"
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set objPing = objWMIService.ExecQuery(wmiQuery)
    For Each objStatus in objPing
        If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
            Reachable = False 'if computer is unreacable, return false
        Else
            Reachable = True 'if computer is reachable, return true
        End If
    Next
End Function

'This function takes the name of the folders (lines 12 and 13) and deletes them
Sub DeleteAFolder(filespec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   'True in the line below includes read only folders
   fso.DeleteFolder(filespec, true)
End Sub

只是一个简短的说明...如果这不起作用 - 这可能是权限问题,但我无意扩展这个脚本 - 所以你从这里开始就靠你自己了。

相关内容