我想爲使用PowerShell的服務器創建統計信息。並學習PowerShell。PowerShell新對象和HTML輸出問題
這是我的代碼有
Function Get-PingHost
{
[cmdletbinding()]
Param([string]$Hostname = "ServerName")
"The HostName is " + $Hostname
$status=get-wmiobject win32_pingstatus -Filter "Address='$Hostname'" | Select-Object statuscode
"The Status Code is " + $status.statuscode
if($status.statuscode -eq 0)
{
$HostNameStatusInfo = $Hostname + " is REACHABLE"
}
else
{
$HostNameStatusInfo = $Hostname + " is NOT REACHABLE"
}
New-Object -TypeName PSObject -Property $HostNameStatusInfo
**<COMMENT>** If I remove the above New-Object code is still get an incorrect output as mentioned below in the **HTML OutPut**
}
$fragments = @()
$fragments+=$top
$fragments+="<a href='javascript:toggleAll();' title='Click to toggle all sections'>Expand All/Collapse All</a>"
$Text = "SQL Server Ping Status"
$div = $Text.Replace(" ","_")
$fragments+= "<a href='javascript:toggleDiv(""$div"");' title='click to collapse or expand this section'><h2>$Text</h2></a><div id=""$div"">"
$fragments+= Get-PingHost -Hostname $Hostname | ConvertTo-Html -Fragment -As List
$fragments+="</div>"
$fragments+= $html.InnerXml
$fragments+="</div>"
$fragments+= "<p class='footer'>$(get-date)</p>"
我HTML得到的輸出是: SQL服務器Ping狀態 *:31 *:20
錯誤消息 EW -Object:無法綁定參數'Property'。無法將類型爲「System.String」的「ServerName是REACHABLE」值轉換爲鍵入「System.Collections.IDictionary」。 At line:38 char:45 + New-Object -TypeName PSObject -Property $ HostNameStatusInfo + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(:) [新對象],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.NewObjectCommand
所以基本上你不需要這個對象。 –