Page 1 of 2

Unable to query remote registry using PowerShell script

Posted: Wed Aug 08, 2018 6:57 pm
by ITEngineer
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.

Re: Unable to query remote registry using PowerShell script

Posted: Wed Aug 08, 2018 7:22 pm
by jvierra
In your code "$_" is an object and not a name.

Re: Unable to query remote registry using PowerShell script

Posted: Wed Aug 08, 2018 8:46 pm
by ITEngineer
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?

Re: Unable to query remote registry using PowerShell script

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

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

Re: Unable to query remote registry using PowerShell script

Posted: Wed Aug 08, 2018 10:07 pm
by ITEngineer
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:

Re: Unable to query remote registry using PowerShell script

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

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

Re: Unable to query remote registry using PowerShell script

Posted: Wed Aug 08, 2018 11:13 pm
by ITEngineer
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 ...
....

Re: Unable to query remote registry using PowerShell script

Posted: Wed Aug 08, 2018 11:40 pm
by jvierra
You cannot use the DN for most commands. Test-Connection cannot use the DN. The registry cannot use the DN.

Re: Unable to query remote registry using PowerShell script

Posted: Wed Aug 08, 2018 11:52 pm
by ITEngineer
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?

Re: Unable to query remote registry using PowerShell script

Posted: Thu Aug 09, 2018 7:57 am
by jvierra

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'}
    }
}