/build/static/layout/Breadcrumb_cap_w.png

Powershell script in Custom INventory Rule

Dear All,

I've developed a powershell script to get the value of an UninstallString in the registry. That script matches the DisplayName with a parameter and if that does the value in the UninstallString will return. I've added in the Custom Inventory Rule of a customized Managed Installation the function ShellCommandTextReturn with the powershell script. When the script is launched on a WIndows 8.1 x64 system, no information are displayed in the Custom Inventory Fields of that system. 

That's the complete function syntax:

ShellCommandTextReturn(powershell.exe -NoLogo -NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -Command "& {(Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\uninstall\ -Recurse) | foreach-object {gp $_.pspath} | where {$_.DisplayName -like "*java*"} | select -ExpandProperty UninstallString}).

Adding Backslashes before special characters and single or double quotes in the powershell command doesn't return the expected value. The CI field displays a powershell error message.

What do i wrong? Could someone help me with this problem?

Kinds regards,

Patrice Renard


0 Comments   [ + ] Show comments

Answers (5)

Posted by: SMal.tmcc 9 years ago
Red Belt
3
Why are you reinventing the wheel?  You create unecessary overhead on your workstations by having to run this task every inventory cycle as a CIR.  If you want to run this yourself run it as a script.  You do not need a CIR or script to get this info, you can just create a simple wizard report to find this.







Posted by: gerane 8 years ago
Yellow Belt
1
This is why I hate the lack of a 64 bit client and has given me some major headaches. The following can be used to pull the proper values back for both 64 bit and 32 bit systems. It is rather ridiculous that you have to go to these extremes to have a single inventory shell return item return properly for 32 and 64.

ShellCommandTextReturn(cmd.exe /c if defined ProgramFiles(x86) (C:\windows\sysnative\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\uninstall\ -Recurse | foreach-object {gp $_.pspath} | where {$_.DisplayName -like '*java*'} | select -ExpandProperty UninstallString") else (powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\uninstall\ -Recurse | foreach-object {gp $_.pspath} | where {$_.DisplayName -like '*java*'} | select -ExpandProperty UninstallString") 2>nul)

I know SMal.tmcc stated,

Why are you reinventing the wheel?  You create unecessary overhead on your workstations by having to run this task every inventory cycle as a CIR.  If you want to run this yourself run it as a script.  You do not need a CIR or script to get this info, you can just create a simple wizard report to find this.
While for this specific example, there may be better ways, I find the question to still be valid. I find that answer leaves many without an answer to their similar question that may require a custom inventory object. The question Prenard was asking is relevant for a large number of similar issues. I have found that using shelltextreturns to pull data for both 64 bit and 32 bit machines to be completely broken. There are just too many cmd, powershell or other tools and utilities that just do not work properly on a 64 bit machine when called from a 32 bit process.

SMal.tmcc also pointed this out,

wmic product where "name like 'Java 7%%' or name like 'Java 8%%'" get uninstall > java.txt

And if you just want to get all versions of java off the machine prior to pushing a new version use a call like:

wmic product where "name like 'Java 7%%' or name like 'Java 8%%'" call uninstall

The get uninstall example won't actually return any uninstall strings since they aren't in the property list. However, in this example, wmic works on both 32 and 64 which is a good example of using an alternative method to solve a 32/64 bit issue. Careful when doing this before installing a new version of the software being uninstalled. I have found that when uninstalling in bulk via wmic, it triggers the need for a reboot. So if you are trying to script the uninstall of all old versions and the install of new version, you may encounter an error when trying to install the newer software. Java is actually the software I have had the most issues with in regards to that issue.

I wanted to share my solution to this particular problem since the wmic and reporting solutions that SMal.tmcc provided do not work in all of my own scenarios. I have used those solutions for some of my problems, but they are not viable in all. 

A quick example would be creating a Custom Inventory Item that is a registry entry that can be in multiple places and use multiple folder structures. I can create a single rule that searches for said Reg value in both 32 and 64 bit contexts and then returns the value as the object. I like to send any errors to nul so I do not get false positives. If you want, you can create a second rule without the nul if you want to weed out any potential errors and just monitor that Custom Inventory object for errors or bad return values. 

I use this to monitor for certain improperly set configurations and then trigger a managed install to correct the bad values. This just isn't viable trying to use the built in reporting, scripting or managed installations. 

I hope this helps, as it took me a while to troubleshoot this sort of custom inventory object.

Comments:
  • 3 years later, but I wanted to thank you for this. It was helpful. Was trying to generate a list of attached webcams and the only way that has been succesful was using powershell. And the variant that was working for me locally wasn't working for ShellCommandTextReturn and I think it was related to this 32 vs 64 bit issue you talked about. - five. 6 years ago
Posted by: rileyz 9 years ago
Red Belt
0
Heya, remember powershell will run in 64bit mode on a..err 64bit system (:

You can either put some smarts into the script to work with 32 and 64 bit systems. Or you can explicitly launch the 32bit powershell process in the SYSWOW64 folder.

Or you could look in the registry WOW6432Node path.
Posted by: flip1001 9 years ago
Black Belt
0
Try this CIR. I also changed -like to -match because I never get double quotes to work inside the main command.

cmd /q /c powershell.exe -Command "(Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\uninstall\ -Recurse) | foreach-object {gp $_.pspath} | where {$_.DisplayName -match 'java'} | select -ExpandProperty UninstallString"
Posted by: SMal.tmcc 9 years ago
Red Belt
0
wmic product where "name like 'Java 7%%' or name like 'Java 8%%'" get uninstall > java.txt

And if you just want to get all versions of java off the machine prior to pushing a new version use a call like:

wmic product where "name like 'Java 7%%' or name like 'Java 8%%'" call uninstall


 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ