Hello. i am trying to connect to icingaweb2 via powershell script but i can’t get csrfToken. What am I doing wrong please?
I created the script based on network traffic tracing in firefox.
We currently do not have an operational API and I don’t know if we ever will. Likewise, our ICT department does not want to allow login via certificate.
So I wanted to initiate a classic connection via https PUSH and GET queries in powershell and then parse the returned HTML code. But I can’t do that. I’m done when logging in.
How to proceed correctly (in powershell) connecting to the website? What am i doing wrong in the script?
The mentioned script only works if I create an active session in firefox and thus the corresponding token. After I log out in firefox, the powershell script also stops working.
I have to add a new token to it, possibly other information, so that everything starts working again. But again only until the session expires.
$dnsName = "icinga2.mySite.com"
$baseUri = "https://" + $dnsName
$logonUri = $baseURI + "/icingaweb2/authentication/login"
$dataUri = $baseURI + "/icingaweb2/monitoring/list/services?sort=service_state&dir=desc&service_state%3E0&limit=500&showCompact=1"
$uName = "myUserName"
$uPwd = "myPassword"
#$csrfToken = "myToken"
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.Add((New-Object System.Net.Cookie("_chc", "1", "/", "$($dnsName)")))
$session.Cookies.Add((New-Object System.Net.Cookie("icingaweb2-session", "1721822041", "/", "$($dnsName)")))
$session.Cookies.Add((New-Object System.Net.Cookie("Icingaweb2", "mmlmngu2ncr4u94ncbboe3hka9", "/", "$($dnsName)")))
$session.Cookies.Add((New-Object System.Net.Cookie("icingaweb2-tzo", "7200-1", "/", "$($dnsName)")))
Invoke-WebRequest -UseBasicParsing -Uri "$($logonUri)" `
-Method POST `
-WebSession $session `
-UserAgent "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0" `
-Headers @{
"Accept" = "*/*"
"Accept-Language" = "cs,sk;q=0.8,en-US;q=0.5,en;q=0.3"
"Accept-Encoding" = "gzip, deflate, br, zstd"
"X-Icinga-Accept" = "text/html"
"X-Icinga-Container" = "layout"
"X-Icinga-WindowId" = "qboktawylfrj"
"X-Requested-With" = "XMLHttpRequest"
"Origin" = "$($baseUri)"
"Referer" = "$($logonUri)"
"Sec-Fetch-Dest" = "empty"
"Sec-Fetch-Mode" = "cors"
"Sec-Fetch-Site" = "same-origin"
"Priority" = "u=0"
} `
-ContentType "application/x-www-form-urlencoded; charset=UTF-8" `
-Body "username=$($uName)&password=$($uPwd)&rememberme=0&redirect=&formUID=form_login&CSRFToken=655133125%7C52db29ddf2b7ec477253aa787e791b6da2163c4d8382e852988d1bcd40a482be&btn_submit=Login"
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.Add((New-Object System.Net.Cookie("icingaweb2-session", "1721823545", "/", "$($dnsName)")))
$session.Cookies.Add((New-Object System.Net.Cookie("Icingaweb2", "5s2f849efld37coa0fgbpcimms", "/", "$($dnsName)")))
$session.Cookies.Add((New-Object System.Net.Cookie("icingaweb2-tzo", "7200-1", "/", "$($dnsName)")))
$result = Invoke-WebRequest -UseBasicParsing -Uri "$($dataUri)" `
-WebSession $session `
-UserAgent "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0" `
-Headers @{
"Accept" = "*/*"
"Accept-Language" = "cs,sk;q=0.8,en-US;q=0.5,en;q=0.3"
"Accept-Encoding" = "gzip, deflate, br, zstd"
"X-Icinga-Accept" = "text/html"
"X-Icinga-Container" = "ciu_1"
"X-Icinga-WindowId" = "qboktawylfrj"
"X-Requested-With" = "XMLHttpRequest"
"Referer" = "$($baseUri)/icingaweb2/dashboard"
"Sec-Fetch-Dest" = "empty"
"Sec-Fetch-Mode" = "cors"
"Sec-Fetch-Site" = "same-origin"
} `
-ContentType "application/x-www-form-urlencoded; charset=UTF-8"
$result.rawcontent
$result.rawcontent | out-file K:\ZakInst\_InstPws\_upgradePws\icinga18.data.html