Each Result in Table <TD>

1

I have a PowerShell Script, which contain $frcnt Variable, it will gives a tow condition array:
1. Values: Name of process (without duplicates).
2. Keys: How many duplicates process. i want to show each above array in HTML table such as:

Keys         Values
chrome       10
explorer     5
firefox      7

here's my wrong syntax:

    $GetCon = Get-NetTCPConnection


$GName = foreach ($process in $GetCon) {

    $processName = (Get-Process -Id $process.OwningProcess).ProcessName
    $process | Add-Member -NotePropertyName ProcessName -NotePropertyValue $processName -PassThru
}

$gnmcnt = $GetCon | select -ExpandProperty ProcessName
$frcnt=@{}
$gnmcnt | % {if (!($frcnt.ContainsKey($_))) {$frcnt.Add($_,1)} else {$frcnt.Item($_)=$frcnt.Item($_) +1}}




$TableBody,$StrBody=""


$frcnt | ForEach-Object {

$TableBody+="<tr><td>$($_.Keys)</td> <td>$($_.Values)</td></tr>"

}


$Body =@"
<table>
<th>Process</th><th>Count</th>
$TableBody
</table>
"@

$Head = "<Style> body {Background-color: lightblue; } table {background-color: white; margin: 5px; float: left; top: 0px; display: inline-block; padding:5px; border: 1px solid black} tr:nth-child(odd) {background-color: lightgray} </style>"


$HTML = ConvertTo-Html -Body $Body -Head $Head


$HTML | Out-File -filepath "C:\temp\test.html" -Force

Invoke-Item "C:\temp\test.html"

Her's a result: enter image description here

Again: I want to each result show's in with count values. thanks.

SchoolforDesign

Posted 2017-01-20T21:56:38.337

Reputation: 145

I don't have time to look where in the code it goes wrong and how to fix it, but I can tell you, you are not working with an array, there's only one result. – LPChip – 2017-01-20T22:31:53.500

@LPChip you'r right, in fact as you see in the image result, it show's all process in one table row <tr>. why? i don't know why! please could you show me an simple example to show all results in each tr? thanks – SchoolforDesign – 2017-01-20T22:37:08.367

Answers

2

To convert the hashtable $frcnt to an array you can use the .GetEnumerator method.
.Keys changes to .Name then. and
.Values to singular .Value

Change the lines

$frcnt | ForEach-Object {
  $TableBody+="<tr><td>$($_.Keys)</td> <td>$($_.Values)</td></tr>"
}

to

$frcntArr = $frcnt.GetEnumerator() |%{$_}
ForEach ($Row in $frcntArr) {
  $TableBody+="<tr><td>$($Row.Name)</td> <td>$($Row.Value)</td></tr>`r`n"
}

Edit

or still better this, with only one Foreach

$frcnt.GetEnumerator() | ForEach {
  $TableBody+="<tr><td>$($_.Name)</td> <td>$($_.Value)</td></tr>`r`n"
}

LotPings

Posted 2017-01-20T21:56:38.337

Reputation: 6 150

can i ask something else, please? – SchoolforDesign – 2017-01-21T17:24:07.967

1@SchoolforDesign Yes of course. But question of public interrest are better put into a new question. – LotPings – 2017-01-21T17:25:33.200

Thanks @LotPings, I want to add CopyRight Member to Get-Process, Here is my wrong syntax, please fix it: Link of My Script

– SchoolforDesign – 2017-01-21T18:11:21.820

1@SchoolforDesign Sorry but comments aren't adequate for new questions. I dont' know what you expect vs what you get, which errors occur. So please open a new questions and others can participate. – LotPings – 2017-01-21T18:32:39.757

@ LoiPngs it say (question limit) for me, but you know I will ask something else, which is get or create a new member for Get-Process, which is CopyRight for each process, I have an script to do that, but it doesn't show any thing, here is my script in google drive: https://drive.google.com/open?id=0B3L7ZNJaTcIhN3d5RjMwSjdrcUE

– SchoolforDesign – 2017-01-21T18:44:51.063

her's my new question, as you say: http://stackoverflow.com/questions/41791316/add-copyright-as-a-new-member-to-get-process

– SchoolforDesign – 2017-01-22T13:17:58.047

@SchoolforDesign remembered a simpler version with only one Foreach. Hmm, a new user to circumvent new question restrictions... – LotPings – 2017-01-22T16:50:00.673