JAM
asked on
Powershell export-csv ... how to include null or error condition for any object that failed?
Hello,
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:
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.
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
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works and exactly what I was looking for , thank you so much!