List WSUS groups and add content to HTML file

Sebastian Jesior 166 Reputation points
2023-04-02T16:12:10.2033333+00:00

Hello Team,

I'm preparing powershell script which lists WSUS groups and counts members. Now I need to add this content to HTML file. Unfortunatelly it is not working and I can see results in powershell console:

foreach( $group in $wsus.GetComputerTargetGroups() ) 
{ 
    Write-Host "Group: " $group.Name " - " $group.GetComputerTargets().Count " member(s)" | ConvertTo-Html | Add-Content "C:\Scripts\WSUS.htm"
} 

obraz

May I kindl ask you to help me and tell what is wrong in this code?

Regards, Sebastian

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2023-04-02T16:48:53.6766667+00:00

    Write-Host is sending the output to the console and not to the pipeline and on to ConvertTo-Html.

    Try this.

    "Group: " + $group.Name + " - " + $group.GetComputerTargets().Count + " member(s)" | ConvertTo-Html | Add-Content "C:\Scripts\WSUS.htm"
    

    Personally, I prefer this method for formatting.

    "Group: {0} - {1} member(s)" -f $group.Name, $group.GetComputerTargets().Count | ConvertTo-Html | Add-Content "C:\Scripts\WSUS.htm"
    
    

    I don't think that ConvertTo-Html is going to do what you want it to do. Let me see if I can get that working too.


1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-04-02T18:44:16.62+00:00

    You can use Select-Object with "calculated properties", but I like this better:

    $wsus.GetComputerTargetGroups() |
        ForEach-Object{ 
            [pscustomobject]@{
                Group = $_.Name
                "Member Count" = $_.GetComputerTargets().Count
            }
        } | 
            ConvertTo-HTML | 
                Add-Content "C:\Scripts\WSUS.htm"
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.