SCOM Report using Powershell

Hello All,

I’ve been working on a script to retrieve the following information from scom:

from a server name or a list of servers, I need to extract the following

  1. rules and monitors
  2. Management pack related to those rules and monitors
  3. configuration settings and thresholds for those rules and monitors
  4. Targets where the rules and monitors were applied

and lastly, export all these to get the information on a CSV file, it’s been extremly complicated to do it, because when I get some information is complicated to understand or even explained, so I will post the last script what I’m using and hopefully some of you guys can provide guidence, I’ll really appreciate it.

Thanks in advance to everyone.

$ErrorActionPreference = "Stop"

try {
    # Cargar la lista de servidores
    $servers = Get-Content -Path "servers.txt"
    if (-not $servers) {
        throw "the file is empty"
    }
} catch {
    Write-Error "error loading the server list: $_"
    exit 1
}


foreach ($server in $servers) {
    try {
        # Obtener los grupos a los que pertenece el servidor
        $groups = Get-SCOMGroup | Where-Object { $_.Name -like "*$server*" }
        if (-not $groups) {
            throw "there are not groups for the following server $server."
        }
    } catch {
        Write-Warning "Warning: $_"
        continue
    }

    try {
        # Obtener las reglas aplicadas
        $rules = Get-SCOMRule | Where-Object { $_.Target -like "*$server*" }
        if (-not $rules) {
            throw "there are no rules applied to this server $server."
        }
    } catch {
        Write-Warning "Warning: $_"
        continue
    }

    foreach ($rule in $rules) {
        try {
            # checking properties of the rule
            $thresholds = $rule.Configuration -or "no configuration"
            $monitoringTarget = $rule.Target.Name -or "no available"
            $managementPack = $rule.ManagementPack.Name -or "no available"
        } catch {
            Write-Warning "Error to get configuration details for the rule: $_"
            continue
        }

        # adding information to the array
        $results += [PSCustomObject]@{
            Server          = $server
            Groups          = ($groups | Select-Object -ExpandProperty DisplayName -join ", ")
            RuleName        = $rule.DisplayName
            Thresholds      = $thresholds
            MonitoringTarget = $monitoringTarget
            ManagementPack  = $managementPack
        }
    }
}

try {
    $results | Export-Csv -Path "ServerMonitoringDetails.csv" -NoTypeInformation -Delimiter ";"
    Write-Host "The information has been exported successfully to 'ServerMonitoringDetails.csv'."
} catch {
    Write-Error "Error to export the results to the CSV file: $_"
    exit 1
}

Write-Error "Error Detected: $_" | Out-File -Append -FilePath "ErrorLog.txt"

if (-not (Get-Module -Name OperationsManager)) {
    Import-Module -Name OperationsManager -ErrorAction Stop
}






This is the error that I’m getting when I ran the script I pasted.

Select-Object : A parameter cannot be found that matches parameter name 'join'.

At C:\XXXXX\RportingServers-v2EH.ps1:52 char:84

+ ...    = ($groups | Select-Object -ExpandProperty DisplayName -join ", ")

+                                                               ~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

I already edid the post hope I done correctly… my apologies :slight_smile:

Daniel,
Welcome to the forum. :wave:t3:

Before we proceed … when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

Guide to Posting Code - Redux <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

And you don’t have to re-post your question - simply edit your existing one! :point_up:

Regardless of that … what’s your actual question?

The error is pretty clear, it is telling you that Select-Object does not have a -Join parameter. You need to enclose that portion so -join is called on the resulting objects, and not mistakenly considered part of the Select-Object arguments.

(($groups | Select-Object -ExpandProperty DisplayName) -join ", ")

It looks like that original parenthesis may not be needed, so just changing to this would work as well.

($groups | Select-Object -ExpandProperty DisplayName) -join ", "
1 Like

Hi Krzydoug,

Thanks for your help. I made the correction and now I got the following, can you please take a look?

X:\XXXX\RportingServers-v2EH.ps1 : Error Detected:

At X:\XXXX\RportingServers-v2EH.ps1:69 char:1

 

RportingServers-v2EH.ps1:69 char:1

+ Write-Error "Error Detected: $_" | Out-File -Append -FilePath "ErrorL ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException

    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,RportingServers-v2EH.ps1


I’m not sure if I’m missing a (.) after $_

Thanks in advance… I’m new to doing scripting with PowerShell.

Hello everyone,

After a slow revision of the script, I found the error. But unfortunately is not showing the information as I expected. I will continue reviewing the code, and I’ll keep updating in my journey :slight_smile:

[quote="Daniel_Zumaya, post:1, topic:25699"]

#the path wasn’t completed and I dind’t have rights to create the file where I was creating the CSV file,I change that and finally works.

try {
$results | Export-Csv -Path ***ServerMonitoringDetails.csv" -NoTy**peInformation -Delimiter “;”
Write-Host “The information has been exported successfully to ‘ServerMonitoringDetails.csv’.”
} catch {
Write-Error “Error to export the results to the CSV file: $_”
exit 1
}

Write-Error “Error Detected: $_” | Out-File -Append -FilePath “ErrorLog.txt”

if (-not (Get-Module -Name OperationsManager)) {
Import-Module -Name OperationsManager -ErrorAction Stop
}

[/quote]


Thanks
Daniel

Why don’t you share it with the world? It will help future readers of this thread.

Thanks in advance.