Unable to query remote registry using PowerShell script

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
ITEngineer
Posts: 109
Joined: Wed Oct 12, 2011 10:52 am

Unable to query remote registry using PowerShell script

Post by ITEngineer » Wed Aug 08, 2018 6:57 pm

People,

I need some help in fixing the below PowerShell script to collect the MS Word version in the few select PC in certain OU:

Code: Select all

Get-ADComputer -Filter {Enabled -eq $True} -SearchBase "OU=Laptop Users,DC=MyDomain,DC=com" |
	Where-Object {Test-Connection $_.Name -Count 1 -Quiet} | ForEach-Object {
	Write-Host "Processing $($_) ..."
	$result = '' | Select-Object -Property ComputerName, OfficeVersion, Error
	$result.ComputerName = $_
	$output = & reg.exe query "\\$($_)\HKLM\Software\Classes\Word.Application\CurVer" /ve 2>&1
	If ($version = $output | Where-Object {$_ -match '.*?REG_SZ\s+Word\.Application\.(?<Version>\d+)'}) {
		$result.OfficeVersion = $Matches['Version']
		$officever = $version.'(default)'
		  $result.OfficeVersion = switch ($officever){
		    "Word.Application.17" {"Office 2019"}
		    "Word.Application.16" {"Office 2016"}
		    "Word.Application.15" {"Office 2013"}
		    "Word.Application.14" {"Office 2010"}
		    "Word.Application.12" {"Office 2007"}
		    "Word.Application.11" {"Office 2003"}
		} Else {
			$result.Error = ($output | Out-String).Split("`r`n")[0]
		}
		$result
	}
} | Export-Csv -NoTypeInformation -Path C:\Result.csv
Somehow the result is empty .CSV, even though there are hundreds of online PC when running the first line below:

Code: Select all

Get-ADComputer -Filter {Enabled -eq $True} -SearchBase "OU=Laptop Users,DC=MyDomain,DC=com" |	Where-Object {Test-Connection $_.Name -Count 1 -Quiet}
Thanks in advance.
/* IT Engineer */

User avatar
jvierra
Posts: 12950
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Unable to query remote registry using PowerShell script

Post by jvierra » Wed Aug 08, 2018 7:22 pm

In your code "$_" is an object and not a name.

User avatar
ITEngineer
Posts: 109
Joined: Wed Oct 12, 2011 10:52 am

Re: Unable to query remote registry using PowerShell script

Post by ITEngineer » Wed Aug 08, 2018 8:46 pm

jvierra wrote:
Wed Aug 08, 2018 7:22 pm
In your code "$_" is an object and not a name.
OK, I have amended into $result.ComputerName = $_.Name

However, the result is still blank?
/* IT Engineer */

User avatar
jvierra
Posts: 12950
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Unable to query remote registry using PowerShell script

Post by jvierra » Wed Aug 08, 2018 10:03 pm

You need to fix it everywhere.

Run in the debugger and check all lines to see your mistakes.

User avatar
ITEngineer
Posts: 109
Joined: Wed Oct 12, 2011 10:52 am

Re: Unable to query remote registry using PowerShell script

Post by ITEngineer » Wed Aug 08, 2018 10:07 pm

jvierra wrote:
Wed Aug 08, 2018 10:03 pm
You need to fix it everywhere.

Run in the debugger and check all lines to see your mistakes.
There is no error in compiling the script, but somehow the result is not shown.
I need some help hence I post in this forum :cry:
/* IT Engineer */

User avatar
jvierra
Posts: 12950
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Unable to query remote registry using PowerShell script

Post by jvierra » Wed Aug 08, 2018 10:10 pm

What is the output from this line?

Write-Host "Processing $($_) ..."

User avatar
ITEngineer
Posts: 109
Joined: Wed Oct 12, 2011 10:52 am

Re: Unable to query remote registry using PowerShell script

Post by ITEngineer » Wed Aug 08, 2018 11:13 pm

jvierra wrote:
Wed Aug 08, 2018 10:10 pm
What is the output from this line?

Write-Host "Processing $($_) ..."
The Distinguished name of the laptops:

Processing CN=WS001234,OU=Laptop Users,DC=MyDomain,DC=com ...
Processing CN=WS003454,OU=Laptop Users,DC=MyDomain,DC=com ...
Processing CN=WS004241,OU=Laptop Users,DC=MyDomain,DC=com ...
....
/* IT Engineer */

User avatar
jvierra
Posts: 12950
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Unable to query remote registry using PowerShell script

Post by jvierra » Wed Aug 08, 2018 11:40 pm

You cannot use the DN for most commands. Test-Connection cannot use the DN. The registry cannot use the DN.

User avatar
ITEngineer
Posts: 109
Joined: Wed Oct 12, 2011 10:52 am

Re: Unable to query remote registry using PowerShell script

Post by ITEngineer » Wed Aug 08, 2018 11:52 pm

jvierra wrote:
Wed Aug 08, 2018 11:40 pm
You cannot use the DN for most commands. Test-Connection cannot use the DN. The registry cannot use the DN.
OK, I have now updated the script with $_.Name, somehow I got this error:
Else : The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ } Else {
+ ~~~~
+ CategoryInfo : ObjectNotFound: (Else:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The .CSV file is now got a few lines of data but with empty OfficeVersion and Error columns?
/* IT Engineer */

User avatar
jvierra
Posts: 12950
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Unable to query remote registry using PowerShell script

Post by jvierra » Thu Aug 09, 2018 7:57 am

Code: Select all

function Get-OfficeVersion{
    param(
        [string]$ComputerName = $env:COMPUTERNAME
    )
    # code to extract Word/Office version
    $hive = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('localmachine',$ComputerName)
    if($key = $hive.OpenSubKey('Software\Classes\Word.Application\CurVer')){
        $wordApp = $key.GetValue($null)
    }
    switch ($wordApp){
        'Word.Application.17' {'Office 2019'}
        'Word.Application.16' {'Office 2016'}
        'Word.Application.15' {'Office 2013'}
        'Word.Application.14' {'Office 2010'}
        'Word.Application.12' {'Office 2007'}
        'Word.Application.11' {'Office 2003'}
        default {'Not found'}
    }
}

Locked