Having trouble pulling a value out of a hash table

This topic contains 6 replies, has 2 voices, and was last updated by  nboardman 4 years, 5 months ago.

  • Author
    Posts
  • #7684

    nboardman
    Participant

    First time poster, so please be gentle...

    I have a script that does a background ping on a list of routers.  I am able to successfully gather the 3 fields I need: .Address, .StatusCode, and .ResponseTime.  My problem is that I want to use the .Address field again and run it through a hash table that contains the Sitenames associate with the individual IP addresses and output those names but everything I am trying does not work.  I have "Googled" for almost two days and tried everything I have found to no avail.  Any help would be greatly appreciated.

  • #7689

    Poshoholic
    Participant

    Can you share your script? It would be much easier to identify the specific problem if we can see where you are currently with your script.

    Thanks!

  • #7690

    nboardman
    Participant

    ....

    $S = "Success"
    $TO = "Timed Out"


    $Sites = @{
    '192.xxx.xxx.1' = "Site1";
    '192.xxx.xxx.2' = "Site2";
    '192.xxx.xxx.3' = "Site3";
    }

     


    $job1 = Get-job | Receive-Job | select-object @{N='Site';E={$Sites.Address switch( $Sites.Value )}},Address,@{N='Status';E={if ($_.StatusCode -eq 0) {$S} else {$TO}}},@{N='Time (ms)';E={$_.ResponseTime}}| ConvertTo-HTML -Fragment -As Table | Out-String

  • #7696

    Poshoholic
    Participant

    Could you try copying and pasting again, going through notepad if necessary to remove the fonts and whatnot?

  • #7697

    nboardman
    Participant

    ...

    $S = "Success"
    $TO = "Timed Out"

    $Sites = @{
    '192.xxx.xxx.1' = "Site1";
    '192.xxx.xxx.2' = "Site2";
    '192.xxx.xxx.3' = "Site3";
    }

    $job1 = Get-job | Receive-Job | select-object @{N='Site';E={$Sites.Address switch( $Sites.Value )}},Address,@{N='Status';E={if ($_.StatusCode -eq 0) {$S} else {$TO}}},@{N='Time (ms)';E={$_.ResponseTime}}| ConvertTo-HTML -Fragment -As Table | Out-String

  • #7698

    Poshoholic
    Participant

    Ok, I think I see it. The trouble is addressing the property of the object that came down the pipeline from the new object at the moment you are creating it. Here is one way you could fix this:

    Don't use Select-Object; use ForEach-Object instead and create a new object that way. That could look like this (using the PowerShell 3.0 custom object accelerator):

    $job1 = Get-Job | Receive-Job | ForEach-Object {
    [pscustomobject]@{
    Site = $Sites[$_.Address]
    Address = $_.Address
    Status = $(if ($_.StatusCode -eq 0) {$S} else {$TO})
    'Time (ms)' = $_.ResponseTime
    }
    } | ConvertTo-HTML ...

    There are other ways as well (using Add-Member with ScriptProperty members to calculate the dynamic values, using ForEach-Object with New-Object to create the new object with the values you need, etc.). I just picked one I think is particularly well suited for your task.

  • #7699

    nboardman
    Participant

    Thanks for your help. I will give that a trying going forward. I actually discovered an open tab in my browser from yesterday an low and behold it had a solution that worked as well.

    Specifically below, where $VMState = the hash table

    @{Label =”VM State”; expression = {$VmState[$_.EnabledState]}}

    Thanks again Poshoholic for your prompt response and providing the more correct approach.

You must be logged in to reply to this topic.