Advertisement
stephanlinke

[PRTG] Monitor website login

Aug 4th, 2015
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ___ ___ _____ ___
  2. #| _ \ _ \_   _/ __|
  3. #|  _/   / | || (_ |
  4. #|_| |_|_\ |_| \___|
  5. #    NETWORK MONITOR
  6. #-------------------
  7. # Description: Monitors if a login on a website with a HTML login form works properly
  8. # Parameters:
  9. # -URL: The URL to the login form
  10. # -Username: A valid username for the page
  11. # -Password: The user's password
  12. # -FormId: The ID of the login form
  13. # -UsernameID: The CSS ID of the username field
  14. # -PasswordID: The CSS ID of the password field
  15. # -CheckString: A string element that only occurs if the login worked
  16.  
  17. # ------------------
  18. # (c) 2015 Stephan Linke | Paessler AG
  19. Param(
  20.     [string]$url = "",
  21.     [string]$logout = "",
  22.     [string]$username="",
  23.     [string]$password="",
  24.     [int]$formId = "0",
  25.     [string]$usernameId = "",
  26.     [string]$passwordId = "",
  27.     [string]$checkString = ""
  28. )
  29.  
  30.  
  31. $loginPage = (Invoke-WebRequest -Uri  "$url" -SessionVariable PRTG)
  32.  
  33. $LoginForm = $loginPage.Forms[$formId];
  34. $LoginForm.Fields[$usernameId] = $username;
  35. $LoginForm.Fields[$passwordId] = $password;
  36.  
  37. $result = Invoke-WebRequest -Uri ($url) -WebSession $PRTG -Method POST -Body $LoginForm.Fields
  38. $response_code = $result.StatusCode
  39. if($result.Content.Contains($checkString))
  40. {
  41.     Write-Host "$response_code:Login successful - '$($Checkstring)' found.";
  42.     # Logout
  43.     Invoke-WebRequest -Uri $logout -SessionVariable PRTG | Out-Null;
  44.     Remove-Variable PRTG;
  45.     exit 0;
  46.  
  47. }
  48. else
  49. {
  50.     Write-Host "$response_code:Login failed or checkstring not found. Please check if the username/password combination as well as the  checkstring are correct!";
  51.     # Logout
  52.     Invoke-WebRequest -Uri $logout -SessionVariable PRTG | Out-Null;
  53.     Remove-Variable PRTG;
  54.     exit 1;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement