以下把代码片段保存为 test.vbs ,然后用cmd打开后运行:
cscript //nologo test.vbs 服务器地址 Administrator 密码
在远程服务器运行 ps 代码或程序:
Option Explicit
Dim objSWbemLocator, objWMIService, objProcess
Dim Err
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(Wscript.Arguments(0), "root\cimv2", Wscript.Arguments(1), Wscript.Arguments(2))
Set objProcess = objWMIService.Get("Win32_Process")
Err = objProcess.Create("cmd /c echo Add-Type -AssemblyName System.Windows.Forms > ps.ps1", null, null, 0)
Err = objProcess.Create("cmd /c echo [System.Windows.Forms.Screen]::AllScreens >> ps.ps1", null, null, 0)
Err = objProcess.Create("cmd /c Powershell.exe -executionpolicy remotesigned -File ps.ps1 > result.txt", null, null, 0)
在远程服务器查询服务状态或控制服务启停:
Option Explicit
Dim objSWbemLocator, objWMIService, objServices, objService
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(Wscript.Arguments(0), "Root\CIMv2", Wscript.Arguments(1), Wscript.Arguments(2))
Set objServices = objWMIService.ExecQuery( "Select * from Win32_Service where name='w32time'" )
For Each objService In objServices
Wscript.Echo objService.Name & "=" & objService.State
Next