Hello,
I’m writing script that should read servers addressess from file and run ping command against each of them. Next I want to export data to csv file, but for results are correctly displayed only in list format.
$results = @()
$servers = (Get-Content .\servers.txt) -notmatch '^#'
$servers | ForEach-Object {
$server = $_
Write-Verbose "Pobrałem adres $_"
$ping = Get-CimInstance -Query "select * from win32_pingstatus where Address='$_'" | select-object -Expandproperty ResponseTime
$object = New-Object psobject -Property @{
$server = $ping
}
$results += $object
}
$results | fl
And I got result like below:
google.com : 17
onet.pl : 98
gmail.com : 14
When I display results as a table I got only one column:
google.com
----------
17
How can I change that?
There are a ton of scripts that check basic connectivity and do tests like a basic ping, so you can always do a search and find a ton of examples, but here is a revised example of what you are attempting:
$servers = 'google.com','onet.pl','gmail.com'
$results = foreach ($server in $servers) {
[pscustomobject]@{
Server = $server
Response = Get-CimInstance -Query "select * from win32_pingstatus where Address='$server'" | select-object -Expandproperty ResponseTime
}
}
$results
Thank you Rob for reply, but this is not quite what I want.
Result from your code is like below:
Server Response
------ --------
google.com 67
onet.pl 11
gmail.com 119
And my goal is to have result like below:
google.com onet.pl gmail.com
67 11 119
Reading the text file …
$results = @()
(Get-Content c:\temp\servers.txt) -notmatch '^#' | ForEach-Object {
$server = $_
$results += [pscustomobject]@{
Server = $server
Response = Get-CimInstance -Query "select * from win32_pingstatus where Address='$server'" | select-object -Expandproperty ResponseTime
}
}
$results
It’s breaking the way that Powershell wants to store information because you’re breaking filtering (e.g. $results | Where {$_.Server -eq ‘google.com’}), but if you want to just format it pretty…
$servers = 'google.com','onet.pl','gmail.com'
$props = @{}
foreach ($server in $servers) {
$props.Add($server, (Get-CimInstance -Query "select * from win32_pingstatus where Address='$server'" | select-object -Expandproperty ResponseTime))
}
$results = New-Object -TypeName PSObject -Property $props
$results
Rob thank you. I think this is what I wanted 
Now I can run this script in loop for an hour, save ping results to csv file and create chart from it.