Home > Blockchain >  Adding values from multiple computers with custom headers in a single csv
Adding values from multiple computers with custom headers in a single csv

Time:11-11

I need to accomplish the scenario below and for that I have to create a couple of powershell scripts to accomplish it.

The environment: Windows servers and Windows clients

Scenario 1- Create a script to be run in a specific time every day (with Task Scheduler) on windows clients. This script will push the current computer's hostname and IP address to a csv file with a specific headers (let's call these "Hostnames" and "IP Address"). These header shouldn't be changed as these scripts run from multiple computers at that time and all computers' data should be appended to each header, not overwrite them as this operation continues. 2- From a server, (after 15 mins) as fetching this "computer" list (csv), there should be a ping check for each of them using their IP addresses. If pings are successful on these remote computers, it should say "This computer is up" next to the each computer name and its IP address. If pings are unsuccessful, it should say "This computer is down" next to the each computer name and its IP address. Above these "status" information, there should be another header (let's say "IsAlive"). This header should be added to the csv as well.

So with this setup, I could be able to learn which computers are UP at a specific time and after I trigger some actions them, I could be able to learn if they're still up or down.

To be honest, I couldn't take a long way for it. I started to write a script about the first step but I couldn't combine the headers with values adding under them.

$header="Hostname"
$outputfile="\\10.10.10.40\reports\upcomputers.csv"

Add-Content $outputfile -Value $header
$hostname >> $outputfile

If I use this script (even if with one header), it's adding "NULL" after each alphabet of hostname and it doesn't append the other hostname under the "Hostname" header.

enter image description here

Additionally, I have no idea where to start adding the third header (IsAlive) and add each Test-NetConnection query's output as checking their IP addresses. I request you to show me a way to start with this section as well.

Finally, my output should be like that;

For the first step;

enter image description here

For the second step;

enter image description here

Thank you for your help and information

Stated on the main body of the request

CodePudding user response:

Stage 2: The easy way of doing this using PSCustomobject. Please find the sample code below:

$outputcsv = "C:\PowerShell\pingstatus.csv"
$hostlist = import-csv "C:\PowerShell\hostlist.csv"

$result = foreach($line in $hostlist){
    
    $pingtest = Test-Connection -ComputerName $line.Hostname -Quiet -Count 1 -ErrorAction SilentlyContinue

    if($pingtest) {
         $OutputMessage = "This computer is up"
      }
     else {
   
       $OutputMessage = "This computer is down"
          
     }
     [pscustomobject][ordered]@{
        HostName  = $line.Hostname
        IPAddress = $line.IPaddress
        IsAlive  = $OutputMessage
    }
     
}

 $result | Export-csv -Path $outputcsv -NoTypeInformation

The Hostname and IPAddress input will be taken as input. Note: Your input csv file should contain Hostname IPaddress as header. Stage1: why not?

$outputfile="\\10.10.10.40\reports\upcomputers.csv"
$serverDetails = [PSCustomObject]@{
Hostname = "$env:COMPUTERNAME"
IPAddress = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
}

$serverDetails | Export-csv $outputfile -Append -NoTypeInformation

There are multiple ways to get IP address a computer, I used Get-WMIObject. You may use other simple ways like Test-Connection or Get-NetIPAddress.

To learn more: Please see [PsCustomObject]: https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.3

  • Related