stephanlinke

Untitled

Jun 22nd, 2017
302
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) 2017 Stephan Linke | Paessler AG
  19. Param(
  20.     [string]$url = "www.acme.com",
  21.     [string]$logout = "www.acme.com/logout",
  22.     [string]$username="<login>",
  23.     [string]$password="<password>",
  24.     [int]$formId = 0,
  25.     [string]$usernameId = "username",
  26.     [string]$passwordId = "password",
  27.     [string]$checkString = "Abmelden"
  28. )
  29.  
  30. $loginPage = (Invoke-WebRequest -Uri  "$url" -SessionVariable PRTG)
  31. Start-Sleep 1
  32.  
  33.  
  34. $LoginForm = $loginPage.Forms[$formId];
  35. $LoginForm.Fields[$usernameId] = $username;
  36. $LoginForm.Fields[$passwordId] = $password;
  37. $LoginForm.Fields['body_0_content_0_sectioned_0_SignInButton'] = "Sign In";
  38.  
  39. $result = (Invoke-WebRequest -Uri ($url) -WebSession $PRTG -Method POST -Body $LoginForm.Fields)
  40.  
  41. if($result.Content.Contains($checkString))
  42. {
  43.     Write-Host "0:Login successful - '$($Checkstring)' found.";
  44.     # Logout
  45.     Invoke-WebRequest -Uri $logout -SessionVariable PRTG | Out-Null;
  46.     Remove-Variable PRTG;
  47.     exit 0;
  48.  
  49. }
  50. else
  51. {
  52.     Write-Host "0:Login failed or checkstring not found. Please check the check if the username/password combination and the checkstring is correct!";
  53.     # Logout
  54.     Invoke-WebRequest -Uri $logout -SessionVariable PRTG | Out-Null;
  55.     Remove-Variable PRTG;
  56.     exit 1;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment