0

So I'm pulling a SCOM class to get Management Pack names, then using those MP names to get different monitors. I'm wanting to output the GUID, Monitor Display Name, and Management Pack Name. The problem I'm running into is how to output it into a CSV because I don't know how to separate them into columns. There are hundreds of Monitors, so I believe it needs to be put into an array also.

Currently, I can get it to write to the console in the columns and with the information I need, but when I try to use the Export-CSV cmdlet, it gives me all sorts of errors. Here is what I have so far:

$OMMgmtSrv = "SERVER"
$Classes = Get-SCOMClass -ComputerName $OMMgmtSrv
Foreach ($Class in $Classes)
{
$MP = $Class.ManagementPackName

$monitors = Get-SCOMMonitor -ManagementPack $MP  | sort-object displayname

Foreach($monitor in $monitors)
{

Write-Host $monitor.ID $monitor.displayname $MP

}
}
user3704471
  • 35
  • 1
  • 5

1 Answers1

0

PowerShell 3.0 or later, you can use a PSCustomObject.

foreach ($monitor in $monitors) {
    [PSCustomObject]@{
        ID = $monitor.ID
        DisplayName = $monitor.displayname
        ManagementPackName = $MP
    } | Export-Csv -Path "C:\monitors.csv" -Append
}

This custom object will have custom properties ID, DisplayName, and ManagementPackName, and it will export to a CSV easily.

Benjamin Hubbard
  • 2,797
  • 22
  • 28
  • You can do it in older versions too, you just have to replace `[PSCustomObject]` with `New-Object PSObject -Properties ` followed by the hashtable. – TheMadTechnician Sep 09 '14 at 17:14