By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

VB.NET using WMI to uninstall programs remotely

P: n/a
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(myManag ementScope.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
Sep 23 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Luke" wrote:
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(myManag ementScope.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?
--
urkec
Sep 24 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.