Powershell - Help adding error checking to my script :-)

Hi Experts..

I wonder if you could assist me to polish off my script below.  

The idea is to audit Cisco AMP AV software across all servers in the domain. My 1st draft works OK, and provides the version installed on each server, however it is missing some important info as follows:

1. Some servers may need powered off, or my account may not have rights to query some servers in the domain, therefore is it possible to capture the  PSComputerName and errors created in the output CSV?  

2. Some servers may not have the software installed at all. Is it possble to add ""PsComputerName - Software not found"" in the output for these servers?

Many thanks for any tips, assistance. cheers String

My script so far:
$Output= @()
$servers = (Get-ADComputer -SearchBase "OU=Services,DC=AD,DC=ACME,DC=COM,DC=AU" -filter {(enabled -eq $True) -and (PwdLastSet -gt $DaysAgo) -and (LastLogonTimeSTamp -gt $DaysAgo)} | Where-Object {$_.DistinguishedName -notlike "*ou=Domain Controllers,*" -and $_.DistinguishedName -notlike "*ou=Non-Windows Servers,*" -and $_.DistinguishedName -notlike "*ou=Disabled Server Accounts,*" }).name

$sb = {Get-ItemProperty HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*|
        Where-Object {$_.Publisher -eq 'Cisco Systems, Inc.' }
Invoke-Command -ComputerName $servers -ScriptBlock $sb |
      Select-Object PsComputerName, DisplayName, DisplayVersion, Publisher, InstallDate | format-table | export-csv "D:\temp\CiscoAMP-Audit.csv" -NoTypeInformation
String :-)Windows EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

String :-)Windows EngineerAuthor Commented:
PS - I am studying Powershell myself (half way through Month of lunches), so please add any comments that will help me learn for next time. Cheers
Dollars to donuts that your first draft as posted is not "working okay".
You're passing the output of Format-Table to Export-Csv, which will fail with a vengeance. Well, not really with a vengeance, but with a csv that does not contain what you expect.
The Format-* cmdlets are mostly used for eye candy, that is, console output. They should usually be the very last element in the pipeline. Do not use Format-* to export to a csv; Export-Csv knows what it's doing all by itself.
This will catch errors and create a record for each outcome (success, software not found, error accessing the server). The result will contain a property "Error" containing the respective reason.
$Output= @()
$logFile = "D:\temp\CiscoAMP-Audit.csv"
$servers = Get-ADComputer -SearchBase "OU=Services,DC=AD,DC=ACME,DC=COM,DC=AU" -Filter {(enabled -eq $True) -and (PwdLastSet -gt $DaysAgo) -and (LastLogonTimeSTamp -gt $DaysAgo)} |
	Where-Object {$_.DistinguishedName -notlike "*ou=Domain Controllers,*" -and $_.DistinguishedName -notlike "*ou=Non-Windows Servers,*" -and $_.DistinguishedName -notlike "*ou=Disabled Server Accounts,*" }
	Select-Object -ExpandProperty Name

$sb = {
	Get-ItemProperty 'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' |
		Where-Object {$_.Publisher -eq $Publisher}
$servers | ForEach-Object {
	$computer = $_
	Write-Host "Processing $($computer) ..."
	Try {
		$result = Invoke-Command -ErrorAction Stop -ComputerName $computer -ScriptBlock $sb -ArgumentList 'Citrix Systems, Inc.' |
			Select-Object -Property PsComputerName, DisplayName, DisplayVersion, Publisher, InstallDate, Error
		If (-not $result) {
			$result = '' | Select-Object @{n='PsComputerName'; e={$computer}}, DisplayName, DisplayVersion, Publisher, InstallDate, @{n='Error'; e={'Software not found'}}
	} Catch {
		$result = $_ | Select-Object @{n='PsComputerName'; e={$computer}}, DisplayName, DisplayVersion, Publisher, InstallDate, @{n='Error'; e={$_.Exception.Message}}
} | Export-Csv -NoTypeInformation -Path $logFile

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
String :-)Windows EngineerAuthor Commented:
LOL - I have to get up pretty early in the morning to catch you out oBdA ;-)

Yes nice script - works well - thanks a bunch.

Useful feedback for sure on the format *. Now I know why my script worked before I added the | export-csv bit. OK lesson learned - thanks

Lastly, I followed all parts of your script so thats progress for me.  I like the use of Try, If, Catch and will try and incorporate that in my next one.
Thanks again
String :-)Windows EngineerAuthor Commented:
Life saver. thanks champ
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.