0

I am trying to create statistics for a server using PowerShell. and also learning PowerShell.

This is the code I have

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>"

The Output I get in HTML is: SQL Server Ping Status *: 31 *: 20

Error Message ew-Object : Cannot bind parameter 'Property'. Cannot convert the "ServerName is REACHABLE" value of type "System.String" to type "System.Collections.IDictionary". At line:38 char:45 + New-Object -TypeName PSObject -Property $HostNameStatusInfo + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.NewObjectCommand

1
  • So basically you do not need that object . Commented Aug 29, 2017 at 6:20

1 Answer 1

0

Apologies... The issue has been resolved... The code is as below: -

#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>"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.