Pull Windows and other vendors cd keys

Topics: Developer Forum, User Forum
Jun 13, 2007 at 2:30 PM
I am not sure if this is proper place to post, if not please move.

I have a project that I am working on for around 2500 PCs. I need to be able to pull the Windows version and cd key as well as other software vendors such as Adobe. I need to be able to pull that and then create a folder under the root to output it to so I can use our recently purchased inventory software to capture that information. Can this be done easily with PowerShell?
Developer
Jun 13, 2007 at 6:02 PM
Edited Jun 13, 2007 at 6:19 PM
Short Answer: No.

Long Answer: PowerShell's default current providers can't attach to remote machines, so the only default thing that can actually return information about a remote machine is WMI. Windows version and CD key is easy: gwmi win32_operatingsystem where gwmi is short for Get-WmiObject ... getting it from a remote PC (assuming your current account has administrator rights on that PC) is just as easy: gwmi win32_operatingsystem -computer ComputerName ... and if your current user doesn't have access, but you know a login that does: gwmi win32_operatingsystem -computer ComputerName -cred UserName you'll be prompted for the password in a popup dialog. If you wanted to be able to query a bunch of PCs (who's names are in computers.txt, one on each line) without having to type the password each time, you could do it like so:

$acct = Get-Credential
cat computers.txt | foreach { gwmi win32_operatingsystem -computer $_ -cred $acct}

Anyway, I think you get the idea. You'll only have problems when you get to the "other software" part. It is possible to write a script to access remote drives or even remote registries, but you can't do so with default PowerShell commands, you have to delve into .Net ... just to give you an idea (and let you judge for yourself if this is something you can handle), here's a script that retrieves the ProductIDs of Office 11 (2003) products that are installed (bear in mind there could be more than one: Visio 2003 installs an extra subkey of "HKEYLOCALMACHINE\SOFTWARE\Microsoft\Office\11.0\Registration").

# Get-OfficeProductId
param([string]$Computer,[int]$Version=11)
 
#Access Remote Registry Key using the static OpenRemoteBaseKey method.
$rootkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computer)
 
$office = "Software\Microsoft\Office\$Version.0\Registration"
$rootkey.OpenSubKey($office).GetSubKeyNames() | % {Write-Output $dev2lm.OpenSubKey("$office\$_").GetValue("ProductId")}

When I invoke that Get-OfficeProductId 0801-rmst-dev2 -Version 11 it returns the serial numbers for both Office and Visio. I can even invoke it for multiple versions at once: 9..12 | % { .\Get-OfficeProductId.ps1 0801-rmst-dev2 -Version $_ 2> $null } and I just get the serial numbers for installed versions (notice I piped the standard error to $null, because non-existent versions cause error messages from the script).

That should be enough to show you that remote registry access is possible, but not easy ...
Jun 14, 2007 at 10:13 AM
Thanks for the prompt answer...I am a linux command line guy and that is why I thought I would give powershell a go. I guess I could do all of this in vbscript or as you suggest learn a little C#/.net.
Developer
Jun 14, 2007 at 11:37 PM
Just to be clear ... all that code is PowerShell, it's just that it requires using the .Net Base Class Library (which is accessible from PowerShell) rather than just existing PowerShell cmdlets.