Home > Back-end >  is there a way to Filter Powershell Add-Content Data?
is there a way to Filter Powershell Add-Content Data?

Time:10-27

im working on a Script that is Supposed to check if certain Machines are online, and then document it in a csv file.

The Area marked green shows how it is Supposed to look.

The Area marked red shows how it looks with my current Script.

I want to get rid of the Things like "@{Datum=" in the Data lines.

What things do i need to change in the code, in order to get the raw data in the CSV file?

`

$Report = @( )
$Datum = Get-Date -Format "dd.MM.yy"
$Zeit = Get-Date -Format "HH:mm:ss"
$Computers ="computer1","computer2"
foreach ($Computer in $Computers) {
    if (Test-Connection $Computer -Count 1 -quiet) {
        $TestResult = New-Object psobject -Property @{
            Datum = $Datum
            Zeit = $zeit
            Computer = $Computer
            Result    = "Online"
        }
    }
    else {
        $TestResult = New-Object psobject -Property @{
            Datum = $Datum
            Zeit = $zeit
            Computer = $Computer
            Result    = "Offline"
        }
    }
    $Report  = $TestResult
}       
$Report | Select-Object Datum, Zeit, Computer, Result | Add-Content C:\kr\powershell\serverstatus4.csv 

`

I want to get rid of the Things like "@{Datum=" in the Data lines.

What things do i need to change in the code, in order to get the raw data in the CSV file?

CodePudding user response:

Think the issue you are facing is related to add-content. As you have objects and want to get a csv you should use export-csv cmdlet, so change the last line of your code to:

$Report | export-csv "C:\kr\powershell\serverstatus4.csv" -delimiter ";"
  • Related