Powershell output issue

Hi i have the following script which works for the computer. csv out put but the ping failed and wmi elements dont seem to work

$Info1=foreach ($b in $q) {
IF (test-connection -ComputerName $b -count 1 -errorAction SilentlyContinue ) {
try {
get-wmiobject -ComputerName $b win32_OperatingSystem -errorAction Stop |  select CSName,Caption,OtherTypeDescription,Version
}
catch {Write-Output "$b" | Export-Csv $Locationoutput\computer_WMI_Failed.csv}
}
ELSE {Write-Output "$b" | Export-Csv $Locationoutput\computer_Ping_Failed.csv}
}

$Info1 |  Export-Csv $Locationoutput\computer.csv


I get this output from the ping_failed.csv (see below), but nothing from the wmi_failed.csv. Any ideas please.

#TYPE System.String
"Length"
"12"
LVL 15
cwstad2Asked:
Who is Participating?

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

x
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.

footechCommented:
When using Test-Connection as a conditional, I'd say it's best to use the -quiet parameter, so that it returns true or false.

The output you're seeing is what happens you pipe a string to Export-CSV (because the only property of a string is length).  Another problem is that you're not appending to a file, so each time you had a problem with the WMI query or ping, the file would be overwritten.  I'd suggest the following changes.
catch {Write-Output "$b" | Add-Content $Locationoutput\computer_WMI_Failed.txt}
}
ELSE {Write-Output "$b" | Add-Content $Locationoutput\computer_Ping_Failed.txt}

Open in new window

cwstad2Author Commented:
Thanks for your reply. Unfortrunatley i would like the output to be delimited as i had the .txt which didnt work for what i intended

Any ideas?
footechCommented:
Not sure how you plan to have a single column delimited.

In any case, what version of PS are you using?
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

cwstad2Author Commented:
Hi im using version 4. Is there another recommended way?

thanks for your help
footechCommented:
You can create an object with the desired property, and then pipe it to Export-CSV, making sure to use the -append parameter so the file isn't overwritten with only the last result.
catch {New-Object PsObject -property @{ computer = $b } | Export-Csv $Locationoutput\computer_WMI_Failed.csv -append}
}
ELSE {New-Object PsObject -property @{ computer = $b } | Export-Csv $Locationoutput\computer_Ping_Failed.csv -append}

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
cwstad2Author Commented:
Thank you works a treat
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
Powershell

From novice to tech pro — start learning today.