VB.NET using WMI to uninstall programs remotely

L

Luke

Hey all,

i'm currently working on a project for our IT group. what this
program
does is lists all the installed programs on the remote machine for
the
admin. what i'm trying to add is functionality for the admin to
select
a program from the list and click an uninstall button on the program
to uninstall the program remotely. We are rebuilding this program
from
VB6 into a .net environment. below is the VB6 code and the VB.NET
code
that i'm currently using to try and replicate the same functionality.
any ideas or help is welcome.


Thanks,


Luke


VB6 Code


Set objWMIService = GetObject("winmgmts:
{impersonationLevel=impersonate}!\\" & frmMain.ComputerName & "\root
\cimv2")
Set colSoftware = objWMIService.execquery("Select * from
Win32_Product Where Name = '" & ApptoUninst & "'")


For Each objSoftware In colSoftware
frmStatus.lblStatus.Caption = "Uninstalling " &
lstApplications.Text & ". Please Wait...."
objSoftware.Uninstall
If Err.Number = 0 Then
frmStatus.lblStatus.Caption = "Uninstall
Complete."
Call frmMain.cmdApps_Click


frmStatus.Hide
Else
MsgBox "Error: " & Err.Description
frmStatus.Hide


End If
Next


VB.NET Code


Dim myConnectionOptions As New System.Management.ConnectionOptions
With myConnectionOptions
.Impersonation =
System.Management.ImpersonationLevel.Impersonate
.Authentication =
System.Management.AuthenticationLevel.Packet
End With


Dim myManagementScope As System.Management.ManagementScope
myManagementScope = New System.Management.ManagementScope("\
\"
& FrmMain.ComputerName & "\root\cimv2")
myManagementScope.Connect()
If myManagementScope.IsConnected = False Then
MsgBox("Could not connect to WMI namespace")
Exit Sub
End If


Dim myObjectSearcher As
System.Management.ManagementObjectSearcher
Dim myCollection As
System.Management.ManagementObjectCollection
Dim myObject As System.Management.ManagementObject
Dim ApptoUninst As String
ApptoUninst =
Trim(DGVApps.SelectedRows.Item(0).Cells("AppName").Value)
myObjectSearcher = New
System.Management.ManagementObjectSearcher(myManagementScope.Path.ToString,
"Select * From Win32_Product Where Name = '" & ApptoUninst & "'")
myCollection = myObjectSearcher.Get()
For Each myObject In myCollection
If myObject.GetPropertyValue("Caption") = ApptoUninst
Then
frmStatus.lblStatus.Text = "Uninstalling " &
ApptoUninst & ". Please Wait...."
frmStatus.Show()


''''''''''''''''''''''' I THINK THE UNINSTALL CODE SHOULD GO HERE
''''''''''''''''''''''''''''''''''''''''


myObject.InvokeMethod("Uninstall", Nothing) < - LINE DOESN'T WORK IT
THROWS AND EXCEPTION


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''­'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''­'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''­'''''''''


If Err.Number = 0 Then
MsgBox("Uninstall Complete!", MsgBoxStyle.OkOnly,
"Success")
frmStatus.Hide()
Else
MsgBox("Error: " & Err.Number & " : " &
Err.Description)
frmStatus.Hide()
Exit For
End If
End If
 
U

urkec

Luke said:
Hey all,

i'm currently working on a project for our IT group. what this
program
does is lists all the installed programs on the remote machine for
the
admin. what i'm trying to add is functionality for the admin to
select
a program from the list and click an uninstall button on the program
to uninstall the program remotely. We are rebuilding this program
from
VB6 into a .net environment. below is the VB6 code and the VB.NET
code
that i'm currently using to try and replicate the same functionality.
any ideas or help is welcome.


Thanks,


Luke


VB6 Code


Set objWMIService = GetObject("winmgmts:
{impersonationLevel=impersonate}!\\" & frmMain.ComputerName & "\root
\cimv2")
Set colSoftware = objWMIService.execquery("Select * from
Win32_Product Where Name = '" & ApptoUninst & "'")


For Each objSoftware In colSoftware
frmStatus.lblStatus.Caption = "Uninstalling " &
lstApplications.Text & ". Please Wait...."
objSoftware.Uninstall
If Err.Number = 0 Then
frmStatus.lblStatus.Caption = "Uninstall
Complete."
Call frmMain.cmdApps_Click


frmStatus.Hide
Else
MsgBox "Error: " & Err.Description
frmStatus.Hide


End If
Next


VB.NET Code


Dim myConnectionOptions As New System.Management.ConnectionOptions
With myConnectionOptions
.Impersonation =
System.Management.ImpersonationLevel.Impersonate
.Authentication =
System.Management.AuthenticationLevel.Packet
End With


Dim myManagementScope As System.Management.ManagementScope
myManagementScope = New System.Management.ManagementScope("\
\"
& FrmMain.ComputerName & "\root\cimv2")
myManagementScope.Connect()
If myManagementScope.IsConnected = False Then
MsgBox("Could not connect to WMI namespace")
Exit Sub
End If


Dim myObjectSearcher As
System.Management.ManagementObjectSearcher
Dim myCollection As
System.Management.ManagementObjectCollection
Dim myObject As System.Management.ManagementObject
Dim ApptoUninst As String
ApptoUninst =
Trim(DGVApps.SelectedRows.Item(0).Cells("AppName").Value)
myObjectSearcher = New
System.Management.ManagementObjectSearcher(myManagementScope.Path.ToString,
"Select * From Win32_Product Where Name = '" & ApptoUninst & "'")
myCollection = myObjectSearcher.Get()
For Each myObject In myCollection
If myObject.GetPropertyValue("Caption") = ApptoUninst
Then
frmStatus.lblStatus.Text = "Uninstalling " &
ApptoUninst & ". Please Wait...."
frmStatus.Show()


''''''''''''''''''''''' I THINK THE UNINSTALL CODE SHOULD GO HERE
''''''''''''''''''''''''''''''''''''''''


myObject.InvokeMethod("Uninstall", Nothing) < - LINE DOESN'T WORK IT
THROWS AND EXCEPTION


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''­'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''­'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''­'''''''''


If Err.Number = 0 Then
MsgBox("Uninstall Complete!", MsgBoxStyle.OkOnly,
"Success")
frmStatus.Hide()
Else
MsgBox("Error: " & Err.Number & " : " &
Err.Description)
frmStatus.Hide()
Exit For
End If
End If


What exception is thrown?
Does the code work locally?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top