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?
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

0
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?
0
footechCommented:
Not sure how you plan to have a single column delimited.

In any case, what version of PS are you using?
0
Top Threats of Q1 & How to Defend Against Them

WEBINAR: Join WatchGuard CTO and our Threat Research Team on Aug. 2nd to hear the findings from our Q1 Internet Security Report! Learn more about the top threats detected in the first quarter and how you can defend your business against them!

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

thanks for your help
0
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

0

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.