Hello,
im kinda run of ideas although i believe the solution must be simple.
I have CSV, containing 3 columns.
hvserver;virtserver;iLO
hvserver1;virtserver1;10.0.0.1
hvserver2;virtserver2;10.55.254.5
hvserver3;virtserver4;10.0.0.1
;virtserver4;
I want to check physical and virtual server for port 3389 (open/closed) and ilo for IP address availability.
For checking RDP i use this code:
$Connection = New-Object Net.Sockets.TcpClient try{ $Connection.Connect($server,"3389") if ($Connection.Connected){ $Response = Open $Connection.Close() } } catch [System.Management.Automation.MethodInvocationException]{ $Response = Closed/Filtered }
For iLo:
Test-Connection -ComputerName $line.ilo -Quiet -Count 2
For single input (just computernames ) its easy ..
$servers = "server1.domain.com","localhost","","server4.domain.com" $TCPObject = @() foreach ($server in $servers) { if ($server){ $Connection = New-Object Net.Sockets.TcpClient try { $Connection.Connect($server,"3389") if ($Connection.Connected) { $Response = Open $Connection.Close() } } catch [System.Management.Automation.MethodInvocationException] { $Response = Closed/Filtered } $hash = @{ Server = $server Response = $Response } $Object = New-Object PSObject -Property $hash $TCPObject += $Object } else { $hash = @{ Server = $server Response = Closed/Filtered } $Object = New-Object PSObject -Property $hash $TCPObject += $Object } } $TCPObject |ft -AutoSize
I am having trouble with CSV as input file and having the following result:
HVserver;ResonseHV;VirtServer;ResponseVirt;IloIPAddress;iLOValid
hvserver1 Closed/Filtered virtserver1 Closed/Filtered 10.0.0.1 False
hvserver2 Closed/Filtered virtserver2 Closed/Filtered 10.20.254.5 True
hvserver3 Closed/Filtered virtserver4 Closed/Filtered 10.0.0.1 False
hvserver3 Closed/Filtered virtserver4 Closed/Filtered 10.0.0.1 False
I understand, I need to start with
$servers = import-csv .\source.csv -delimiter ";"
Foreach ($line in $servers){
$hv = $line.hvserver
$virt = $line.virtserver
$ilo = $line.ilo
}
.. after that. I just get buggy results. I think I need add some IF/Else conditions or something like that.
I need advice how to make my custom hash table with all 5 columns with required output.
Note: some info on input CSV might be missing, as some servers don't have iLO or doenst host any virtual server..