问题
I am trying to create the script for checking if the servers are online, which will be displayed as HTML web page using "ConvertTo-Html" property. I also applied some small CSS into it.
Here is my code:
$Header = @"
<style>
table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: green;
color: white;
}
</style>
"@
$OS = Test-Connection -ComputerName Server1, Server2, Server3 | ConvertTo-Html
ConvertTo-Html -Body "$OS" -Title "Fragment" -Head $Header | Out-File StatusReport.html
Output result in the file "StatusReport.html" shows too much unnecessary information (photo attached). How can I display only few of them: Server name, ping result?
回答1:
Ass Lee commented, you'll need to select the properties you want from the output. There are quite a few properties but none of them are Server or result.
To see a list of the properties, pipe the output to Get-Member
Test-Connection google.com -count 1 | Get-Member
You can also use Format-List * to see the properties and values
Test-Connection google.com -count 1 | Format-List *
Once you know the properties you're after you can select them by name
Test-Connection google.com -count 1 | Select-Object Address,IPv4Address,StatusCode
If you want to rename the properties, you can use calculated properties
Test-Connection google.com -count 1 |
Select-Object @{n='Server';e={$_.Address}},IPv4Address,@{n='Result';e={'Successful'}}
However this does not account for any failed ping results. Here's a bit of code that will gather the successes and failures, then combine the two. Note that the property names need to match.
$targets = 'server1', 'server2', 'server3', 'dontexist'
$success = Test-Connection -ComputerName $targets -Count 1 -ErrorAction SilentlyContinue -ErrorVariable errors |
Select-Object @{n='Server';e={$_.address}},IPv4Address,@{n='Result';e={'Successful'}}
$failed = $errors.exception.message |
Where-Object {$_ -match "computer '(.+?)'"} |
Select-Object @{n='Server';e={$matches.1}},
@{n='IPv4Address';e={"N/A"}},
@{n='Result';e={'Failed'}}
$success + $failed
You can use splatting to make the code easier to read and maintain.
$params = @{
ComputerName = 'server1', 'server2', 'server3', 'dontexist'
Count = 1
ErrorAction = 'SilentlyContinue'
ErrorVariable = 'errors'
}
$success = Test-Connection @params |
Select-Object @{n='Server';e={$_.address}},
IPv4Address,
@{n='Result';e={'Successful'}}
$failed = $errors.exception.message |
Where-Object {$_ -match "computer '(.+?)'"} |
Select-Object @{n='Server';e={$matches.1}},
@{n='IPv4Address';e={"N/A"}},
@{n='Result';e={'Failed'}}
$success + $failed
Combining this with a slightly modified version of your code we end up with this as the final script.
$Header = @"
<style>
table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: green;
color: white;
}
</style>
"@
$params = @{
ComputerName = 'server1', 'server2', 'server3', 'dontexist'
Count = 1
ErrorAction = 'SilentlyContinue'
ErrorVariable = 'errors'
}
$success = Test-Connection @params |
Select-Object @{n='Server';e={$_.address}},
IPv4Address,
@{n='Result';e={'Successful'}}
$failed = $errors.exception.message |
Where-Object {$_ -match "computer '(.+?)'"} |
Select-Object @{n='Server';e={$matches.1}},
@{n='IPv4Address';e={"N/A"}},
@{n='Result';e={'Failed'}}
$success + $failed |
ConvertTo-Html -Title "Fragment" -Head $Header |
Set-Content -Path StatusReport.html -Encoding UTF8
来源:https://stackoverflow.com/questions/64828931/how-to-display-server-name-and-ping-result-only-from-powershell-script-in-html-w