Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell issue with formating information returned from a function

Posted on 2010-09-01
8
Medium Priority
?
572 Views
Last Modified: 2012-05-10
Ok,

I've got the following function that reads all of the installed programs from a Windows computer's registry.

...and it works great for telling me the exactly version and name of software installed on a Windows 32bit/64bit system.

However, if I call the funtion using this format:

$x = Get-InstalledPrograms <computer name>

The result is that I get  a variable with the infromation returned from the function.

So, I can then issue the command:

$x.keys and see a list of everything installed.  ...but is a runon sentance type list... not an array.

What I can't do is execute:

$x.keys[1] and get the first item in the list... and it doesn't seem to be translating into an array variable, as I'm getting this:

Unable to index into an object of type System.Collections.Hashtable+KeyCollection.
At line:1 char:32
+ $InstalledApplicationList.keys[ <<<< 1]
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

So, clearly, I need to figure out how to get $x.keys and $x.values (which have a 1 to 1 relationship) to be in an array.  This way I can check what's on a specific PC against a list of application that I'm looking for, from another source.

Thoughts?

The function is below:

function Get-InstalledPrograms($computer = '.') { 

$programs_installed = @{};
$error_action = 'Stop';
$reg_uninstall_paths = @('Software\Microsoft\Windows'`
	+ '\CurrentVersion\Uninstall');

$reg_uninstall_paths += @('Software\Wow6432Node\Microsoft'`
	+ '\Windows\CurrentVersion\Uninstall');
	$hkey = 'LocalMachine';
	
	$registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hkey, $computer);
	foreach ($reg_uninstall_path in $reg_uninstall_paths) {
		$reg_uninstall_key = $registry.OpenSubKey($reg_uninstall_path);
		if ($reg_uninstall_key -eq $null) {
			continue;
		}
		$key_names = $reg_uninstall_key.GetSubKeyNames();
		foreach ($key_name in $key_names) {
			$key_properties = $reg_uninstall_key.OpenSubKey($key_name);
			$name = $key_properties.GetValue('DisplayName');
			$version = $key_properties.GetValue('DisplayVersion');
			if ($name -ne $null) {
				$programs_installed.$name = $version;
			}
			$key_properties.close();
		}
		$reg_uninstall_key.close();
	}
	$registry.close();
	
	$reg_uninstall_paths = @('Software\Microsoft\Installer\Products');
	$hkey = 'CurrentUser';

	$registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hkey, $computer);
	foreach ($reg_uninstall_path in $reg_uninstall_paths) {
		$reg_uninstall_key = $registry.OpenSubKey($reg_uninstall_path);
		if ($reg_uninstall_key -eq $null) {
			continue;
		}
		$key_names = $reg_uninstall_key.GetSubKeyNames();
		foreach ($key_name in $key_names) {
			$key_properties = $reg_uninstall_key.OpenSubKey($key_name);
			$name = $key_properties.GetValue('ProductName');
			$version = $key_properties.GetValue('DisplayVersion');
			if ($name -ne $null) {
				$programs_installed.$name = $version;
			}
			$key_properties.close();
		}
		$reg_uninstall_key.close();
	}
	$registry.close();
	
	
return $programs_installed;
}

Open in new window

0
Comment
Question by:gerhardub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 33580247
If you just have to check the existatnce of an app, you can use:

$apps = Get-InstalledPrograms yourpc
if($apps.keys -contains "your app name"){ "Be happy with that"}
else {"Do something else"}

Otherwise:


$apps = Get-InstalledPrograms yourpc
$appsarray = $apps.keys | ForEach-Object {new-object -TypeName PSObject -Property @{name = $_; version = $apps.$_}}

Open in new window

0
 
LVL 1

Author Comment

by:gerhardub
ID: 33580349
Unfortunately, that does not always work....
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33580365
What does not always work?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Author Comment

by:gerhardub
ID: 33580476
Oh...oh... sorry about that... I reading one message and replied to another.

That seems like it would work... and be an easy solution.  Let me give that a try... on just the app name.

Where it fail for us, is that we ALSO need to be checking the version number, and it's concievable that the version number will not always be unique.

...and other ideas on how to get this to work?
0
 
LVL 1

Author Comment

by:gerhardub
ID: 33580536
Just to clarify (since I assumed a detailed look at the function):

So, when I do $x.values (which corresponds to the version for each of those apps listed in $x.keys), I get a long line of version numbers...

The ideal solution would to be to parse each line into either a multi-dimentional array $x[a,b] or a new array with the format $app.name and $app.version....  that supports standard $array[1].name formating.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33580660
That is what my code above does.

$appsarray[0].name gives the name of the first app and
$appsarray[0].version gives the version.
0
 
LVL 1

Author Comment

by:gerhardub
ID: 33581731
Sorry man... totally distracted...  Lemme plug it in tomorrow and then give you your 500 points when it works?
0
 
LVL 1

Author Closing Comment

by:gerhardub
ID: 33586779
Thank you sir, that appears to have given me enought to go on with!

Sincerely,

GB
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question