Currently I have a script that does a bunch of things, one thing is that it checks a .txt file to get all the current servers and then just does a ping to check connectivity before it does the rest of the script. I currently have that setup to add the servers it could not ping into an array so that it will not slow down the rest of the process attempting on failed servers. At the end of the script I have it all converted to an HTML document for ease of viewing. I would like to add the servers that failed the connection to the end of the HTML document to show that those servers failed.
As the script is now, it just prints the .Length property of the array I put those servers in. Here is the code I have that sets up the array, adds servers to it, and then the convertto-html part.
$servers = Get-Content "path\to\text.txt"
$failedConn = New-Object 'System.Collections.Generic.List[System.Object]'
foreach ($server in $servers)
{
$pingResult = Get-Ciminstance -ClassName win32_pingstatus -Filter "address='$server' and timeout=1000"
if ($pingResult.StatusCode -eq 0)
{
Write-Host "$server ping successful
}
else
{
Write-Host "$server ping Fail" -ForegroundColor Red
$failedConn.Add($server)
<# Also tried $failedConn = ($server) with the same error #>
}
}
<# Code for a forloop to do tasks where it adds an HTML variable called CombinedBody styling the tables whatnot #>
$CombinedBody = $failedConn | ConvertTo-Html | Out-File -Append C:\Path\To\html.html
The result just puts the failed connections at the very bottom of the HTML document, but it prints it as the length of each server name. How do I get it to print the actual server name? Any help is greatly appreciated!!
CodePudding user response:
Length
is the only property in the String
object that ConvertTo-Html
sees so that's what gets output. As a workaround, you can wrap the server names in another object that only have a single property containing the name, then it should output the actual names. Like this:
$failedConn | foreach{[pscustomobject]@{"Failed Servers" = $_}} | ConvertTo-Html | Out-File -Append C:\Path\To\html.html
Note that I've removed $CombinedBody =
since Out-File
doesn't return any output so that won't do anything.