Powershell export-csv ... how to include null or error condition for any object that failed?


I have spreadsheet of computers I'm looking to get information on.  I'm running a powershell command against that list of computers, exporting to a CSV, which is working fine.  

However, in the exported CSV, any computers that the command returned an error on are skipped in the CSV.  
I'm looking to include the error condition (ie. "Cannot find an object with identity <computername>") for these in the exported CSV, or even simply a blank row with just the computer name (so I can see which computers the command skipped/failed on).

For example, instead of getting only the following in the CSV (skipping Computer2 which could not be found)...

Computer1        True
Computer3        True
Computer4        True

... I'm looking for something like this:

Computer1        True
Computer2        "error condition" or <blank row>
Computer3        True
Computer4        True

Here's the powershell command I'm running:

$computers = Import-CSV list.csv
$computers | foreach-object {Get-ADComputer -Identity ($_.Servername) -Properties IPv4Address,OperatingSystem,LastLogonTimeStamp,CreateTimeStamp,CanonicalName | select-object Name,IPv4Address,OperatingSystem,@{Name="Lastlogon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},CreateTimeStamp,CanonicalName} | Export-csv servers.csv -notypeinformation

Open in new window

Note: Googling how to achieve this, the closest I saw was https://stackoverflow.com/questions/29550324/how-can-i-output-all-the-output-of-a-called-command-in-powershell-script ... but that appeared more complex that I'm looking for.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Try it like this:
Import-Csv list.csv |
	ForEach-Object {
		$ComputerName = $_.Servername
		Try {
			Get-ADComputer -Identity $ComputerName -Properties IPv4Address, OperatingSystem, LastLogonTimeStamp, CreateTimeStamp, CanonicalName -ErrorAction Stop |
				Select-Object -Property *, @{Name='Lastlogon'; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}
		} Catch {
			$_.Exception.Message | Select-Object -Property @{n='Name'; e={$ComputerName}}, @{n='Error'; e={$_}}
	} |
	Select-Object -Property  Name, IPv4Address, OperatingSystem, Lastlogon, CreateTimeStamp, CanonicalName, Error |
	Export-Csv servers.csv -NoTypeInformation

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
JAMAuthor Commented:

Works and exactly what I was looking for , thank you so much!
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.