Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI VB.NET

Status
Not open for further replies.

HyperRon

Programmer
Aug 5, 2003
21
US
I need to run application on a remote machine. I have this code in VB 6 but, can't seem to get it working in VB.NET (windows 2000 network).


Dim strComputer As String
Dim objWMIService As Object
Dim errReturn As Long
Dim intProcessID As Long
strComputer = "dev2"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process")
errReturn = objWMIService.Create("notepad.exe", Null, Null, intProcessID)
If errReturn = 0 Then
MsgBox "notepad.exe was started with a process ID of " _
& intProcessID & "."
Else
MsgBox "notepad.exe could not be started due to error " & _
errReturn & "."
End If
 
I originally tried doing this using xp_cmdshell but, it kept hanging so I tried this (which i found via search) and it worked:

CREATE PROCEDURE dbo.sp_cmdshell(@cmd varchar(255), @Wait int = 0) AS
--Create WScript.Shell object
DECLARE @result int, @OLEResult int, @RunResult int
DECLARE @ShellID int

EXECUTE @OLEResult = sp_OACreate 'WScript.Shell', @ShellID OUT
IF @OLEResult <> 0 SELECT @result = @OLEResult
IF @OLEResult <> 0 RAISERROR ('CreateObject %0X', 14, 1, @OLEResult)


EXECUTE @OLEResult = sp_OAMethod @ShellID, 'Run', Null, @cmd, 0, @Wait
IF @OLEResult <> 0 SELECT @result = @OLEResult
IF @OLEResult <> 0 RAISERROR ('Run %0X', 14, 1, @OLEResult)

EXECUTE @OLEResult = sp_OADestroy @ShellID

return @result

GO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top