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

String :-)
String :-) used Ask the Experts™
on
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= @()
$DaysAgo=(Get-Date).AddDays(-30)
$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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
String :-)Windows Engineer

Author

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
String
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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= @()
$DaysAgo=(Get-Date).AddDays(-30) 
$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 = {
	Param([string]$Publisher)
	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}}
	}
	$result
} | Export-Csv -NoTypeInformation -Path $logFile

Open in new window

String :-)Windows Engineer

Author

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
String :-)Windows Engineer

Author

Commented:
Life saver. thanks champ

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial