Skip to content

Commit 82826ec

Browse files
authored
Update script ♾️
1 parent c42e857 commit 82826ec

1 file changed

Lines changed: 78 additions & 71 deletions

File tree

Powershell/SCOM-REST_API-Examples.ps1

Lines changed: 78 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,86 +10,91 @@ $MainURL = 'http://<WebConsoleAddress>/OperationsManager'
1010

1111
function Authenticate-SCOMAPI
1212
{
13-
param (
14-
[PSCredential]$Credential = $null
15-
)
16-
# Set SCOM Header and the Body
17-
$SCOMHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
18-
$SCOMHeaders.Add('Content-Type', 'application/json; charset=utf-8')
19-
$BodyRaw = "Windows"
20-
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($BodyRaw)
21-
$EncodedText = [Convert]::ToBase64String($Bytes)
22-
$JSONBody = $EncodedText | ConvertTo-Json
23-
24-
# Initiate the Cross-Site Request Forgery (CSRF) token, this is to prevent CSRF attacks
25-
$CSRFtoken = $WebSession.Cookies.GetCookies($MainURL) | ? { $_.Name -eq 'SCOM-CSRF-TOKEN' }
26-
$SCOMHeaders.Add('SCOM-CSRF-TOKEN', [System.Web.HttpUtility]::UrlDecode($CSRFtoken.Value))
27-
28-
# Authentication
29-
if ($Credential -ne $null) {
30-
Invoke-RestMethod -Method Post -Uri "$MainURL/authenticate" -Headers $SCOMHeaders -Body $JSONBody -Credential $Credential -SessionVariable WebSession
31-
} else {
32-
Invoke-RestMethod -Method Post -Uri "$MainURL/authenticate" -Headers $SCOMHeaders -Body $JSONBody -UseDefaultCredentials -SessionVariable WebSession
33-
}
34-
$script:WebSession = $WebSession
13+
param (
14+
[PSCredential]$Credential = $null
15+
)
16+
# Set SCOM Header and the Body
17+
$SCOMHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
18+
$SCOMHeaders.Add('Content-Type', 'application/json; charset=utf-8')
19+
$BodyRaw = "Windows"
20+
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($BodyRaw)
21+
$EncodedText = [Convert]::ToBase64String($Bytes)
22+
$JSONBody = $EncodedText | ConvertTo-Json
23+
24+
# Initiate the Cross-Site Request Forgery (CSRF) token, this is to prevent CSRF attacks
25+
$CSRFtoken = $WebSession.Cookies.GetCookies($MainURL) | ? { $_.Name -eq 'SCOM-CSRF-TOKEN' }
26+
$SCOMHeaders.Add('SCOM-CSRF-TOKEN', [System.Web.HttpUtility]::UrlDecode($CSRFtoken.Value))
27+
28+
# Authentication
29+
if ($Credential -ne $null)
30+
{
31+
Invoke-RestMethod -Method Post -Uri "$MainURL/authenticate" -Headers $SCOMHeaders -Body $JSONBody -Credential $Credential -SessionVariable WebSession
32+
}
33+
else
34+
{
35+
Invoke-RestMethod -Method Post -Uri "$MainURL/authenticate" -Headers $SCOMHeaders -Body $JSONBody -UseDefaultCredentials -SessionVariable WebSession
36+
}
37+
$script:WebSession = $WebSession
3538
}
3639

37-
Authenticate-SCOMAPI
38-
3940
# Function to fetch all installed SCOM Consoles
4041
function Get-SCOMConsoles
4142
{
42-
# Criteria: Enter the displayname of the SCOM object
43-
$Criteria = "DisplayName LIKE '%System Center Operations Manager Console%'"
44-
45-
# Convert our criteria to JSON format
46-
$JSONBody = $Criteria | ConvertTo-Json
47-
48-
$Response = Invoke-WebRequest -Uri "$MainURL/OperationsManager/data/scomObjects" -Method Post -Body $JSONBody -WebSession $script:WebSession
49-
50-
# Convert our response from JSON format to a custom object or hash table
51-
$Object = ConvertFrom-Json -InputObject $Response.Content
52-
53-
# Print out the object results
54-
$Object.scopeDatas
43+
# Criteria: Enter the displayname of the SCOM object
44+
$Criteria = "DisplayName LIKE '%System Center Operations Manager Console%'"
45+
46+
# Convert our criteria to JSON format
47+
$JSONBody = $Criteria | ConvertTo-Json
48+
49+
$Response = Invoke-WebRequest -Uri "$MainURL/OperationsManager/data/scomObjects" -Method Post -Body $JSONBody -WebSession $script:WebSession
50+
51+
# Convert our response from JSON format to a custom object or hash table
52+
$Object = ConvertFrom-Json -InputObject $Response.Content
53+
54+
# Print out the object results
55+
$Object.scopeDatas
5556
}
5657

5758
# Function to fetch all Windows Servers
58-
function Get-WindowsServers {
59-
$criteria = "DisplayName LIKE 'Microsoft Windows Server%'"
60-
$JSONBody = $criteria | ConvertTo-Json
61-
62-
$response = Invoke-WebRequest -Uri "$MainURL/data/scomObjects" -Method Post -Body $JSONBody -WebSession $script:WebSession
63-
return ($response.Content | ConvertFrom-Json).scopeDatas
59+
function Get-WindowsServers
60+
{
61+
$criteria = "DisplayName LIKE 'Microsoft Windows Server%'"
62+
$JSONBody = $criteria | ConvertTo-Json
63+
64+
$response = Invoke-WebRequest -Uri "$MainURL/data/scomObjects" -Method Post -Body $JSONBody -WebSession $script:WebSession
65+
return ($response.Content | ConvertFrom-Json).scopeDatas
6466
}
6567

6668
# Function to fetch the state of the Management Group
67-
function Get-ManagementGroupState {
68-
$query = @(@{
69-
"classId" = ""
70-
"criteria" = "DisplayName = 'Operations Manager Management Group'"
71-
"displayColumns" = "displayname", "healthstate", "name", "path"
72-
})
73-
74-
$JSONQuery = $query | ConvertTo-Json
75-
$response = Invoke-RestMethod -Uri "$MainURL/data/state" -Method Post -Body $JSONQuery -ContentType "application/json" -WebSession $script:WebSession
76-
return $response.Rows
69+
function Get-ManagementGroupState
70+
{
71+
$query = @(@{
72+
"classId" = ""
73+
"criteria" = "DisplayName = 'Operations Manager Management Group'"
74+
"displayColumns" = "displayname", "healthstate", "name", "path"
75+
})
76+
77+
$JSONQuery = $query | ConvertTo-Json
78+
$response = Invoke-RestMethod -Uri "$MainURL/data/state" -Method Post -Body $JSONQuery -ContentType "application/json" -WebSession $script:WebSession
79+
return $response.Rows
7780
}
7881

7982
# Function to fetch Unsealed Management Packs
80-
function Get-UnsealedManagementPacks {
81-
$response = Invoke-WebRequest -Uri "$MainURL/data/UnsealedManagementPacks" -Method GET -WebSession $script:WebSession
82-
return $response.Content | ConvertFrom-Json
83+
function Get-UnsealedManagementPacks
84+
{
85+
$response = Invoke-WebRequest -Uri "$MainURL/data/UnsealedManagementPacks" -Method GET -WebSession $script:WebSession
86+
return $response.Content | ConvertFrom-Json
8387
}
8488

8589
# Function to fetch Effective Monitoring Configuration by GUID
86-
function Get-EffectiveMonitoringConfiguration {
87-
param (
88-
[string]$guid
89-
)
90-
$uri = "$MainURL/effectiveMonitoringConfiguration/$guid`?isRecursive=True"
91-
$response = Invoke-WebRequest -Uri $uri -Method GET -WebSession $script:WebSession
92-
return $response.Content | ConvertFrom-Json
90+
function Get-EffectiveMonitoringConfiguration
91+
{
92+
param (
93+
[string]$guid
94+
)
95+
$uri = "$MainURL/effectiveMonitoringConfiguration/$guid`?isRecursive=True"
96+
$response = Invoke-WebRequest -Uri $uri -Method GET -WebSession $script:WebSession
97+
return $response.Content | ConvertFrom-Json
9398
}
9499

95100
# ------------------------------------------------------------------------------------------
@@ -100,10 +105,12 @@ function Get-EffectiveMonitoringConfiguration {
100105

101106
# Uncomment the below lines if you want to authenticate using specific credentials
102107
# $cred = Get-Credential
103-
# Authenticate-SCOM -Credential $cred
108+
# Authenticate-SCOMAPI -Credential $cred
109+
110+
# OR
104111

105112
# Uncomment the below line if you want to authenticate using the current user's credentials
106-
# Authenticate-SCOM
113+
# Authenticate-SCOMAPI
107114

108115
#endregion
109116
# ------------------------------------------------------------------------------------------
@@ -122,19 +129,19 @@ function Get-EffectiveMonitoringConfiguration {
122129
Write-Output "-----------------------------------------"
123130

124131
# Fetch all Windows Servers
125-
$WindowsServers = Get-WindowsServers
126-
Write-Output "Windows Servers:`n$($WindowsServers | ConvertTo-Json)"
132+
Write-Output "Windows Servers:`n"
133+
Get-WindowsServers | ConvertTo-Json
127134

128135
Write-Output "-----------------------------------------"
129136

130137
# Get Unsealed Management Packs
131-
$unsealedMPs = Get-UnsealedManagementPacks
132-
Write-Output "Unsealed MPs:`n$($unsealedMPs | ConvertTo-Json)"
138+
Write-Output "Unsealed MPs:`n"
139+
Get-UnsealedManagementPacks | ConvertTo-Json
133140

134141
Write-Output "-----------------------------------------"
135142

136143
# Get Management Group Health Status
137-
$state = Get-ManagementGroupState
138-
Write-Output "Management Group State:`n$($state | ConvertTo-Json)"
144+
Write-Output "Management Group State:`n"
145+
Get-ManagementGroupState | ConvertTo-Json
139146

140147
Write-Output "-----------------------------------------"

0 commit comments

Comments
 (0)