WMI Run Remote Process

如何使用 WMI 來遠端執行指定的應用程式, 經過網路搜尋雖然可以用 WIN32_Process 來執行遠端軟體, 但遠端電腦中不能顯示軟體的 UI.
(參考 MSDN WIN32_Process)
後來找到迂迴的方法, 在遠端建立指定軟體的排程工作 Win32_ScheduledJob, 然後再呼叫 schtasks 批次, 但是遠端建立的排程工作卻無法執行在(program files)資料夾中的的軟體, 所以最後利用 psexec 的工具軟體來實現遠端呼叫,
不過還是要設定防火牆的一些設定,
1. netsh firewall set service RemoteAdmin enable
2. netsh firewall add portopening protocol=tcp port=135 name=DCOM_TCP135
3. Do Administrative Tools->Local Security Settings->Local Policies->Security Options and look for the line "Network Access:Sharing and security model for local accounts". As of at most 6 months ago, the default value for this "key" was "Classic - local users authenticate as themselves". But new XP installations and those with recent security updates will have a default value of "Guest only - local users authenticate as Guest". Change it back to "Classic - local users... etc.,."
4. netsh firewall add allowedprogram program=%windir%\system32\wbem\unsecapp.exe name=UNSECAPP (以我的 case 不需要)


Dim options As New ConnectionOptions
With options
.Username = UserName
.Password = Password
End With
CMessage.Show("RemoteCall : " & RemoteIP, "Run remote process.", 26)
Dim scope As New ManagementScope(String.Format("\\{0}\root\cimv2", RemoteIP), options)
scope.Connect()
Dim getOptions As New ObjectGetOptions(Nothing, New TimeSpan(0, 0, 0, 3), True)
Dim processClass As New ManagementClass(scope, New System.Management.ManagementPath("Win32_Process"), getOptions)
For Each m As ManagementObject In processClass.GetInstances
If m("Name").ToString = ("Remote-Tool.exe") Then
m.InvokeMethod("Terminate", New Object() {0})
CMessage.Text("Terminate Process", 26) = "Stop Remote-Server..."
End If
Next
'==============Method Psexec Tool:
Shell(Application.StartupPath & String.Format("\psexec \\{0} -u {1} -p {2} -i -d ""notepad.exe""", RemoteIP, UserName, Password), AppWinStyle.NormalFocus, True)
Dim flag As Boolean
Do
For Each m As ManagementObject In processClass.GetInstances
If m("Name").ToString = "Remote-Tool.exe" Then
flag = True
Exit For
End If
Next m
Threading.Thread.Sleep(200)
Application.DoEvents()
Loop Until flag
End Using

留言

熱門文章