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("winm gmts:
{impersonationL evel=impersonat e}!\\" & frmMain.Compute rName & "\root
\cimv2")
Set colSoftware = objWMIService.e xecquery("Selec t * from
Win32_Product Where Name = '" & ApptoUninst & "'")
For Each objSoftware In colSoftware
frmStatus.lblSt atus.Caption = "Uninstalli ng " &
lstApplications .Text & ". Please Wait...."
objSoftware.Uni nstall
If Err.Number = 0 Then
frmStatus.lblSt atus.Caption = "Uninstall
Complete."
Call frmMain.cmdApps _Click
frmStatus.Hide
Else
MsgBox "Error: " & Err.Description
frmStatus.Hide
End If
Next
VB.NET Code
Dim myConnectionOpt ions As New System.Manageme nt.ConnectionOp tions
With myConnectionOpt ions
.Impersonation =
System.Manageme nt.Impersonatio nLevel.Imperson ate
.Authentication =
System.Manageme nt.Authenticati onLevel.Packet
End With
Dim myManagementSco pe As System.Manageme nt.ManagementSc ope
myManagementSco pe = New System.Manageme nt.ManagementSc ope("\
\"
& FrmMain.Compute rName & "\root\cimv 2")
myManagementSco pe.Connect()
If myManagementSco pe.IsConnected = False Then
MsgBox("Could not connect to WMI namespace")
Exit Sub
End If
Dim myObjectSearche r As
System.Manageme nt.ManagementOb jectSearcher
Dim myCollection As
System.Manageme nt.ManagementOb jectCollection
Dim myObject As System.Manageme nt.ManagementOb ject
Dim ApptoUninst As String
ApptoUninst =
Trim(DGVApps.Se lectedRows.Item (0).Cells("AppN ame").Value)
myObjectSearche r = New
System.Manageme nt.ManagementOb jectSearcher(my ManagementScope .Path.ToString,
"Select * From Win32_Product Where Name = '" & ApptoUninst & "'")
myCollection = myObjectSearche r.Get()
For Each myObject In myCollection
If myObject.GetPro pertyValue("Cap tion") = ApptoUninst
Then
frmStatus.lblSt atus.Text = "Uninstalli ng " &
ApptoUninst & ". Please Wait...."
frmStatus.Show( )
''''''''''''''' '''''''' I THINK THE UNINSTALL CODE SHOULD GO HERE
''''''''''''''' ''''''''''''''' ''''''''''
myObject.Invoke Method("Uninsta ll", Nothing) < - LINE DOESN'T WORK IT
THROWS AND EXCEPTION
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''
If Err.Number = 0 Then
MsgBox("Uninsta ll Complete!", MsgBoxStyle.OkO nly,
"Success")
frmStatus.Hide( )
Else
MsgBox("Error: " & Err.Number & " : " &
Err.Description )
frmStatus.Hide( )
Exit For
End If
End If
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("winm gmts:
{impersonationL evel=impersonat e}!\\" & frmMain.Compute rName & "\root
\cimv2")
Set colSoftware = objWMIService.e xecquery("Selec t * from
Win32_Product Where Name = '" & ApptoUninst & "'")
For Each objSoftware In colSoftware
frmStatus.lblSt atus.Caption = "Uninstalli ng " &
lstApplications .Text & ". Please Wait...."
objSoftware.Uni nstall
If Err.Number = 0 Then
frmStatus.lblSt atus.Caption = "Uninstall
Complete."
Call frmMain.cmdApps _Click
frmStatus.Hide
Else
MsgBox "Error: " & Err.Description
frmStatus.Hide
End If
Next
VB.NET Code
Dim myConnectionOpt ions As New System.Manageme nt.ConnectionOp tions
With myConnectionOpt ions
.Impersonation =
System.Manageme nt.Impersonatio nLevel.Imperson ate
.Authentication =
System.Manageme nt.Authenticati onLevel.Packet
End With
Dim myManagementSco pe As System.Manageme nt.ManagementSc ope
myManagementSco pe = New System.Manageme nt.ManagementSc ope("\
\"
& FrmMain.Compute rName & "\root\cimv 2")
myManagementSco pe.Connect()
If myManagementSco pe.IsConnected = False Then
MsgBox("Could not connect to WMI namespace")
Exit Sub
End If
Dim myObjectSearche r As
System.Manageme nt.ManagementOb jectSearcher
Dim myCollection As
System.Manageme nt.ManagementOb jectCollection
Dim myObject As System.Manageme nt.ManagementOb ject
Dim ApptoUninst As String
ApptoUninst =
Trim(DGVApps.Se lectedRows.Item (0).Cells("AppN ame").Value)
myObjectSearche r = New
System.Manageme nt.ManagementOb jectSearcher(my ManagementScope .Path.ToString,
"Select * From Win32_Product Where Name = '" & ApptoUninst & "'")
myCollection = myObjectSearche r.Get()
For Each myObject In myCollection
If myObject.GetPro pertyValue("Cap tion") = ApptoUninst
Then
frmStatus.lblSt atus.Text = "Uninstalli ng " &
ApptoUninst & ". Please Wait...."
frmStatus.Show( )
''''''''''''''' '''''''' I THINK THE UNINSTALL CODE SHOULD GO HERE
''''''''''''''' ''''''''''''''' ''''''''''
myObject.Invoke Method("Uninsta ll", Nothing) < - LINE DOESN'T WORK IT
THROWS AND EXCEPTION
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '''''''''''
If Err.Number = 0 Then
MsgBox("Uninsta ll Complete!", MsgBoxStyle.OkO nly,
"Success")
frmStatus.Hide( )
Else
MsgBox("Error: " & Err.Number & " : " &
Err.Description )
frmStatus.Hide( )
Exit For
End If
End If
Comment