Having trouble pulling a value out of a hash table

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.

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!

<span style=“font-size: medium;”><span style=“font-size: medium;”></span></span>

<span style=“font-size: medium;”><span style=“font-size: medium;”></span></span>

$S = “Success”
$TO = “Timed Out”

<span style=“font-size: medium;”><span style=“font-size: medium;”></span></span>
$Sites = @{
‘192.xxx.xxx.1’ = “Site1”;
‘192.xxx.xxx.2’ = “Site2”;
‘192.xxx.xxx.3’ = “Site3”;
}

<span style=“font-size: medium;”><span style=“font-size: medium;”></span></span>

 

<span style=“font-size: medium;”><span style=“font-size: medium;”></span></span>
$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

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

$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

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.

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.