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.

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
}

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax