Search code examples
powershellbrowserpowershell-cmdlet

PowerShell script opens browser by itself


The following code is a powerShell script that sends POST request and gets response and store it to $response. When I try to get the form value and try to store it to $wa , it automatically opens up the browser.

$destination="https://adfs.somedomain.com/adfs/ls/";
$response = '';
$username = 'some username';
$password = 'some password';
$AuthMethod = 'FormsAuthentication';
$postParams = @{
    destination="$destination";
    flags='0';
    forcedownlevel='0';
    trusted='0'
    UserName="$($username)";
    Password="$($password)";
    AuthMethod = "$($AuthMethod)";
    isUtf8='1'
}

$url = "$($destination)?wa=wsignin1.0&wtrealm=urn:federation:cas"
$response = Invoke-WebRequest -uri $url -Method POST -body $postParams;
$wa = $response.InputFields.value[0];

How do I make the script NOT to open up the browser?

Note: It sends post request which returns a redirection to same uri with get request and respond with some form value which I am trying to store in a variable.


Solution

  • So I found out that Powershell uses computer's default browser's parser to parse HTML. That is why it opens up the browser. Using -UseBasicParsing attribute make use of powershell's HTML parser. And turns out Powershell's parser is not good enough for above case at least!