[contact-form][contact-field label=”Name” type=”name” required=”true” /][contact-field label=”Email” type=”email” required=”true” /][contact-field label=”Website” type=”url” /][contact-field label=”Message” type=”textarea” /][/contact-form]So at my place of employment, I am responsible for quite a few task and one of them being the Pharmacy Maintainance window. This window control’s its security patches from Microsoft through a custom web interface with a Login. By using the powershell code below I was able to automate logging into the interface ready to invoke the window. I hope the code below helps someone else post past a ASP.NET page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host $Headers = @{ "accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" "Host"="vm716" "Cache-Control"="max-age=0" "Origin"="http://ADMServer" "Upgrade-Insecure-Requests"="1" "Referer"="http://ADMServer/ADMConsole/Login.aspx?ReturnUrl=%2fADMConsole%2fdefault.aspx" "Accept-Encoding"="gzip, deflate" "Accept-Language"="en-US,en;q=0.9"} $FormatEnumerationLimit=-1 $userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome $LoginResponse = Invoke-WebRequest 'http://ADMServer/ADMConsole/Login.aspx?ReturnUrl=%2fADMConsole%2fDevices%2fdefault.aspx' -SessionVariable 'Session' -UserAgent $userAgent -Headers $Headers #$LoginResponse.Forms[0] | Format-List -Property * $addUserForm = $LoginResponse.Forms[0] #Invoke-WebRequest does a lot of auto processing. #Doing it this will will also send the stateview and other important ASP.NET variables. $addUserForm.Fields['loginUser$UserName'] = "MyUserName" $addUserForm.Fields['loginUser$Password'] = "MyPassword" $addUserForm.Fields['loginUser$LoginButton'] = "Login" #$LoginResponse.Forms[0] | Format-List -Property * $LoginResponse = Invoke-WebRequest ('http://ADMServer/ADMConsole/' + $addUserForm.Action) -WebSession $session -Body $addUserForm.Fields -Method 'POST' -UserAgent $userAgent -Headers $Headers $LoginResponse write-Host 'http://ADMServer/ADMConsole/' + $addUserForm.Action $CheckBoxes = $LoginResponse.ParsedHtml.getElementsByTagName("input") #$LoginResponse = Invoke-WebRequest ('http://ADMServer/ADMConsole/ADMConsole%2fDevices%2fdefault.aspx') -WebSession $session -UserAgent $userAgent -Headers $Headers #$LoginResponse foreach ($element in $CheckBoxes) { Write-Host $element.Name $element.value } |