Ask your 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.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Wed Aug 08, 2018 7:22 pm
In your code "$_" is an object and not a name.
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
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.
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
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
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Wed Aug 08, 2018 10:10 pm
What is the output from this line?
Write-Host "Processing $($_) ..."
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
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.
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
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'}
}
}
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked