Skip to content

Commit c42e857

Browse files
authored
Small change ⚙️
1 parent dc5bd51 commit c42e857

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

Powershell/SCOM-REST_API-Examples.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Blog: https://blakedrumm.com/
77

88
# You can modify the below to use HTTP or HTTPS. Replace <WebConsoleAddress> with the address for your Web Console.
9-
$MainURL = 'http://MS02-2022.contoso-2022.com/OperationsManager'
9+
$MainURL = 'http://<WebConsoleAddress>/OperationsManager'
1010

1111
function Authenticate-SCOMAPI
1212
{
@@ -21,17 +21,19 @@ function Authenticate-SCOMAPI
2121
$EncodedText = [Convert]::ToBase64String($Bytes)
2222
$JSONBody = $EncodedText | ConvertTo-Json
2323

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+
2428
# Authentication
2529
if ($Credential -ne $null) {
2630
Invoke-RestMethod -Method Post -Uri "$MainURL/authenticate" -Headers $SCOMHeaders -Body $JSONBody -Credential $Credential -SessionVariable WebSession
2731
} else {
2832
Invoke-RestMethod -Method Post -Uri "$MainURL/authenticate" -Headers $SCOMHeaders -Body $JSONBody -UseDefaultCredentials -SessionVariable WebSession
2933
}
3034
$script:WebSession = $WebSession
31-
# Initiate the Cross-Site Request Forgery (CSRF) token, this is to prevent CSRF attacks
32-
$CSRFtoken = $WebSession.Cookies.GetCookies($MainURL) | ? { $_.Name -eq 'SCOM-CSRF-TOKEN' }
33-
$SCOMHeaders.Add('SCOM-CSRF-TOKEN', [System.Web.HttpUtility]::UrlDecode($CSRFtoken.Value))
3435
}
36+
3537
Authenticate-SCOMAPI
3638

3739
# Function to fetch all installed SCOM Consoles

0 commit comments

Comments
 (0)