Sign in to follow this  
Followers 0
JLogan3o13

Remote Application Enumeration and Uninstall

5 posts in this topic

I am attempting to take a snippet of code I have been using in a VBScript for a couple of years, and convert it to AutoIt. I'm relatively new at AutoIt, but enjoying it much more than other tools. Below is the code I have thus far, with the changes I've made from vbs to autoit, but I am receiving the error below. I may well be calling the function completely wrong; but any suggestions would be greatly appreciated.

<code>

$Machine = InputBox("Computer name?", "Uninstall Remote Application")

$AdminUser = ("My Domain Name")

$Password = ("My Password")

$Locator = ObjCreate("WbemScripting.SWbemLocator")

$Service = $Locator.ConnectServer($Machine, "root\cimv2", $AdminUser, $Password)<-here is where I get the error "The requested action with this object has failed." Not sure if I'm calling this incorrectly.

I haven't gotten as far as the second and third blocks of code due to the error, but below is what I'm thinking. Any constructive critiques are welcome and appreciated.

;get a list of installed products

Dim $Msg, $Name

$Products = $Service.ExecQuery("SELECT * " & "FROM Win32_Product")

For $element in $Products

$Msg = MsgBox(4, "Product: " & $element & "Uninstall?")

If $Msg = 6 Then

$Name = $element.Name

EndIf

Next

;Get the named package

$Products2 = $Service.ExecQuery("SELECT * " & "FROM Win32_Product WHERE Name = '" & $Name & "'")

For $element in $Products2

;uninstall it

$Product.Uninstall

;done!

MsgBox (0, "Uninstalled " & $Name)

Next


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites



Please define a COM error handler so you can see what's going wrong.

I get an error when I query the PC the script is running on. So I changed the code accordingly:

$oMyError = ObjEvent("AutoIt.Error", "_AD_ErrorHandler") ; Install a custom error handler

$Machine = InputBox("Computer name?", "Uninstall Remote Application")
$AdminUser = ("")
$Password = ("")
$Locator = ObjCreate("WbemScripting.SWbemLocator")
If $Machine = @ComputerName Then 
    $Service = $Locator.ConnectServer($Machine, "root\cimv2")
Else
    $Service = $Locator.ConnectServer($Machine, "root\cimv2", $AdminUser, $Password)
EndIf

Func _AD_ErrorHandler()

    Local $bHexNumber = Hex($oMyError.number, 8)
    ConsoleWrite("COM Error Encountered in " & @ScriptName & @CRLF & _
            "Scriptline = " & $oMyError.scriptline & @CRLF & _
            "NumberHex = " & $bHexNumber & @CRLF & _
            "Number = " & $oMyError.number & @CRLF & _
            "WinDescription = " & StringStripWS($oMyError.WinDescription, 2) & @CRLF & _
            "Description = " & StringStripWS($oMyError.description, 2) & @CRLF & _
            "Source = " & $oMyError.Source & @CRLF & _
            "HelpFile = " & $oMyError.HelpFile & @CRLF & _
            "HelpContext = " & $oMyError.HelpContext & @CRLF & _
            "LastDllError = " & $oMyError.LastDllError & @CRLF & _
            "========================================================" & @CRLF)

EndFunc ;==>_AD_ErrorHandler

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks for the reply. I added the error handling as suggested, and it shows me the error is on line 16 and 26, which are the two for statements. $element must be incorrect. In vbs I could just say "For Each oProduct in cProducts", but AutoIT doesn't seem to work quite the same. Thanks for the assistance, with the error handling. I am still trying to find the right verbiage, but this definitely helps.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This works fine for me and returns a list of software installed for the local and remote machines.

$oMyError = ObjEvent("AutoIt.Error", "_AD_ErrorHandler") ; Install a custom error handler

$Machine = InputBox("Computer name?", "Uninstall Remote Application")
$AdminUser = ("<Your User>")
$Password = ("<Your Password>")
$Locator = ObjCreate("WbemScripting.SWbemLocator")
If $Machine = @ComputerName Then
    $Service = $Locator.ConnectServer($Machine, "root\cimv2")
Else
    $Service = $Locator.ConnectServer($Machine, "root\cimv2", $AdminUser, $Password)
EndIf
;get a list of installed products
$Products = $Service.ExecQuery("SELECT * FROM Win32_Product")
For $element In $Products
    ConsoleWrite($element.name & @CRLF)
Next


Func _AD_ErrorHandler()

    Local $bHexNumber = Hex($oMyError.number, 8)
    ConsoleWrite("COM Error Encountered in " & @ScriptName & @CRLF & _
            "Scriptline = " & $oMyError.scriptline & @CRLF & _
            "NumberHex = " & $bHexNumber & @CRLF & _
            "Number = " & $oMyError.number & @CRLF & _
            "WinDescription = " & StringStripWS($oMyError.WinDescription, 2) & @CRLF & _
            "Description = " & StringStripWS($oMyError.description, 2) & @CRLF & _
            "Source = " & $oMyError.Source & @CRLF & _
            "HelpFile = " & $oMyError.HelpFile & @CRLF & _
            "HelpContext = " & $oMyError.HelpContext & @CRLF & _
            "LastDllError = " & $oMyError.LastDllError & @CRLF & _
            "========================================================" & @CRLF)

EndFunc ;==>_AD_ErrorHandler

I think your problem is line

$Msg = MsgBox(4, "Product: " & $element & "Uninstall?")

$element is an object. You should change it to $element.name

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks again for the assistance. I actually think I am going to go in a new direction with this script. After searching the forums and help pages a little more thoroughly, I have the script below. I think it is a little more visual, which as it is for Helpdesk folks is a good thing. Right now the enumeration portion works well; I'll work on adding a second column for the uninstall string later on (Ubound is still new to me). It actually produces a nice popup (screen print). I am looking back through the posts to see where I got it, so I can give author proper credit and thanks.

#include <Array.au3>

$Computer = InputBox( "Enter Asset Tag", "What machine would you like to connect to?" )

$ret = _SoftwareInfo()

_ArrayDisplay($ret, '')

Func _SoftwareInfo($s_RemoteComputer = $Computer)

Local $Count = 1

If $s_RemoteComputer <> '' Then $s_RemoteComputer = '\\' & StringReplace($s_RemoteComputer, '\', '') & '\'

Local Const $regkey = $s_RemoteComputer & 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'

While 1

$key = RegEnumKey ($regkey, $Count)

If @error <> 0 then ExitLoop

$line = RegRead ($regkey & '\' & $key, 'Displayname')

$line = StringReplace ($line, ' (remove only)', '')

If $line <> '' Then

If Not IsDeclared('avArray') Then Dim $avArray[1]

ReDim $avArray[uBound($avArray) + 1]

$avArray[uBound($avArray) - 1] = $line

EndIf

$Count = $Count + 1

WEnd

If Not IsDeclared('avArray') Or Not IsArray($avArray) Then

Return(SetError(1, 0, ''))

Else

$avArray[0] = UBound($avArray) - 1

Return(SetError(0,0, $avArray))

EndIf

EndFunc


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0