Skip to content

Commit 534334d

Browse files
authored
Update to newer version 👍 🍰
1 parent 4f7a14a commit 534334d

1 file changed

Lines changed: 58 additions & 47 deletions

File tree

Powershell/SCOM-REST_API-Examples.ps1

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,62 @@
22
# This script includes functions to interact with SCOM's REST API
33
# Author: Blake Drumm (blakedrumm@microsoft.com)
44
# Date Created: November 1st, 2023
5+
# Date Updated: January 4th, 2024
56
# Blog: https://blakedrumm.com/
67

7-
# Initialize SCOM API Base URL
8-
$URIBase = 'http://<WebConsoleURL>/OperationsManager'
8+
$MainURL = 'http://MS02-2022.contoso-2022.com/OperationsManager'
99

10-
# Function to initialize HTTP headers and CSRF token for SCOM API
11-
function Initialize-SCOMHeaders {
12-
$SCOMHeaders = @{
13-
'Content-Type' = 'application/json; charset=utf-8'
14-
}
15-
16-
$CSRFtoken = $WebSession.Cookies.GetCookies($URIBase) | Where-Object { $_.Name -eq 'SCOM-CSRF-TOKEN' }
17-
$SCOMHeaders['SCOM-CSRF-TOKEN'] = [System.Web.HttpUtility]::UrlDecode($CSRFtoken.Value)
18-
return $SCOMHeaders
19-
}
20-
21-
# Function to authenticate with the SCOM API
22-
function Authenticate-SCOM {
10+
function Authenticate-SCOMAPI
11+
{
2312
param (
2413
[PSCredential]$Credential = $null
2514
)
26-
27-
$bodyRaw = "Windows"
28-
$encodedText = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($bodyRaw))
29-
$JSONBody = $encodedText | ConvertTo-Json
30-
31-
$SCOMHeaders = Initialize-SCOMHeaders
32-
15+
# Set SCOM Header and the Body
16+
$SCOMHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
17+
$SCOMHeaders.Add('Content-Type', 'application/json; charset=utf-8')
18+
$BodyRaw = "Windows"
19+
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($BodyRaw)
20+
$EncodedText = [Convert]::ToBase64String($Bytes)
21+
$JSONBody = $EncodedText | ConvertTo-Json
22+
23+
# Authentication
3324
if ($Credential -ne $null) {
34-
Invoke-RestMethod -Method Post -Uri "$URIBase/authenticate" -Headers $SCOMHeaders -Body $JSONBody -Credential $Credential -SessionVariable WebSession
25+
Invoke-RestMethod -Method Post -Uri "$MainURL/authenticate" -Headers $SCOMHeaders -Body $JSONBody -Credential $Credential -SessionVariable WebSession
3526
} else {
36-
Invoke-RestMethod -Method Post -Uri "$URIBase/authenticate" -Headers $SCOMHeaders -Body $JSONBody -UseDefaultCredentials -SessionVariable WebSession
27+
Invoke-RestMethod -Method Post -Uri "$MainURL/authenticate" -Headers $SCOMHeaders -Body $JSONBody -UseDefaultCredentials -SessionVariable WebSession
3728
}
29+
$script:WebSession = $WebSession
30+
# Initiate the Cross-Site Request Forgery (CSRF) token, this is to prevent CSRF attacks
31+
$CSRFtoken = $WebSession.Cookies.GetCookies($MainURL) | ? { $_.Name -eq 'SCOM-CSRF-TOKEN' }
32+
$SCOMHeaders.Add('SCOM-CSRF-TOKEN', [System.Web.HttpUtility]::UrlDecode($CSRFtoken.Value))
3833
}
39-
40-
# Function to fetch Effective Monitoring Configuration by GUID
41-
function Get-EffectiveMonitoringConfiguration {
42-
param (
43-
[string]$guid
44-
)
45-
46-
$uri = "$URIBase/effectiveMonitoringConfiguration/$guid`?isRecursive=True"
47-
$response = Invoke-WebRequest -Uri $uri -Method GET -WebSession $WebSession
48-
return $response.Content | ConvertFrom-Json
34+
Authenticate-SCOMAPI
35+
36+
# Function to fetch all installed SCOM Consoles
37+
function Get-SCOMConsoles
38+
{
39+
# Criteria: Enter the displayname of the SCOM object
40+
$Criteria = "DisplayName LIKE '%System Center Operations Manager Console%'"
41+
42+
# Convert our criteria to JSON format
43+
$JSONBody = $Criteria | ConvertTo-Json
44+
45+
$Response = Invoke-WebRequest -Uri "$MainURL/OperationsManager/data/scomObjects" -Method Post -Body $JSONBody -WebSession $script:WebSession
46+
47+
# Convert our response from JSON format to a custom object or hash table
48+
$Object = ConvertFrom-Json -InputObject $Response.Content
49+
50+
# Print out the object results
51+
$Object.scopeDatas
4952
}
5053

51-
# Function to fetch Unsealed Management Packs
52-
function Get-UnsealedManagementPacks {
53-
$uri = "$URIBase/data/UnsealedManagementPacks"
54-
$response = Invoke-WebRequest -Uri $uri -Method GET -WebSession $WebSession
55-
return $response.Content | ConvertFrom-Json
54+
# Function to fetch all Windows Servers
55+
function Get-WindowsServers {
56+
$criteria = "DisplayName LIKE 'Microsoft Windows Server%'"
57+
$JSONBody = $criteria | ConvertTo-Json
58+
59+
$response = Invoke-WebRequest -Uri "$MainURL/data/scomObjects" -Method Post -Body $JSONBody -WebSession $script:WebSession
60+
return ($response.Content | ConvertFrom-Json).scopeDatas
5661
}
5762

5863
# Function to fetch the state of the Management Group
@@ -64,18 +69,24 @@ function Get-ManagementGroupState {
6469
})
6570

6671
$JSONQuery = $query | ConvertTo-Json
67-
$response = Invoke-RestMethod -Uri "$URIBase/data/state" -Method Post -Body $JSONQuery -ContentType "application/json" -WebSession $WebSession
72+
$response = Invoke-RestMethod -Uri "$MainURL/data/state" -Method Post -Body $JSONQuery -ContentType "application/json" -WebSession $script:WebSession
6873
return $response.Rows
6974
}
7075

71-
# Function to fetch all Windows Servers
72-
function Get-WindowsServers {
73-
$criteria = "DisplayName LIKE 'Microsoft Windows Server%'"
74-
$JSONBody = $criteria | ConvertTo-Json
76+
# Function to fetch Unsealed Management Packs
77+
function Get-UnsealedManagementPacks {
78+
$response = Invoke-WebRequest -Uri "$MainURL/data/UnsealedManagementPacks" -Method GET -WebSession $script:WebSession
79+
return $response.Content | ConvertFrom-Json
80+
}
7581

76-
$uri = "$URIBase/data/scomObjects"
77-
$response = Invoke-WebRequest -Uri $uri -Method Post -Body $JSONBody -WebSession $WebSession
78-
return ($response.Content | ConvertFrom-Json).scopeDatas
82+
# Function to fetch Effective Monitoring Configuration by GUID
83+
function Get-EffectiveMonitoringConfiguration {
84+
param (
85+
[string]$guid
86+
)
87+
$uri = "$MainURL/effectiveMonitoringConfiguration/$guid`?isRecursive=True"
88+
$response = Invoke-WebRequest -Uri $uri -Method GET -WebSession $script:WebSession
89+
return $response.Content | ConvertFrom-Json
7990
}
8091

8192
# ------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)