对象和HTML输出问题

我正在尝试为使用PowerShell的服务器创建统计信息。 并学习PowerShell。

这是我的代码

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 Server Ping状态*:31 *:20

错误信息 ew-Object:无法绑定参数'Property'。 无法将类型为“System.String”的“ServerName是REACHABLE”值转换为键入“System.Collections.IDictionary”。 在行:38个字符:45 +新对象类型名称PSObject属性$ HostNameStatusInfo + ~~~~~~~~~~~~~~~~~在新的对象],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.NewObjectCommand


道歉...问题已解决...代码如下: -

#GET OPERATING SYSTEM INFORMATION
Function Get-PingHost
{
    [cmdletbinding()]
    Param([string]$Hostname = "ServerName")

    $status=get-wmiobject win32_pingstatus -Filter "Address='$Hostname'" | Select-Object statuscode
    #"The Status Code is " + $status.statuscode
    if($status.statuscode -eq 0)
    {
        $Hostname + " is <b>REACHABLE</b>"

    }
    else
    {
       $Hostname + " is <b>NOT REACHABLE</b>"
       #Basically You Don't Need to assign this to the String Variable and 
       #then display the string variable...
    }
}

#endregion

$fragments = @()
$top = ""

$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 
$fragments+="</div>"
$fragments+= $html.InnerXml
$fragments+="</div>"


$fragments+= "<p class='footer'>$(get-date)</p>"
链接地址: http://www.djcxy.com/p/37885.html

上一篇: Object and HTML Output Issue

下一篇: WebRequest : Cannot bind parameter 'Headers'