Problems with Invoke-WebRequest

Mikhail Firsov 1,876 Reputation points
2020-11-19T14:54:24.243+00:00

Hello,

I tried to connect to my Exchange OWA site by using the code from this page:

$Body = @{  
    User = 'jdoe'  
    password = 'P@S$w0rd!'  
}  
$LoginResponse = Invoke-WebRequest 'https://www.contoso.com/login/' -SessionVariable 'Session' -Body $Body -Method 'POST'  

$Session  

$ProfileResponse = Invoke-WebRequest 'https://www.contoso.com/profile/' -WebSession $Session  

$ProfileResponse  

...and got confusing results:
41098-1.png

1) my $LoginResponse variable was always null
2) while executing this string
$LoginResponse = Invoke-WebRequest 'https://mail.domain.net/owa/' -SessionVariable 'Session' -Body $Body -Method 'POST'

...the IE's window would always pop up:
41099-2.png

3) in spite of the fact that the $LoginResponse was null (and with any random password supplied) the $ProfileResponse.Status returned was always 200 (OK) - I thought inability to log in to the OWA site will lead to the $ProfileResponse.Status !=200 ...

Maybe these results are by design and I don't understand anything???

Thank you in advance,
Michael

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Rich Matheisen 45,091 Reputation points
    2020-11-19T15:16:26.707+00:00

    What happens if you use If ($null -eq $LoginResponse) instead? If $LoginResponse is null you'll see the error if you do the comparison your way because the variable isn't defined.

    0 comments No comments

  2. MotoX80 31,816 Reputation points
    2020-11-19T16:31:34.707+00:00

    I think that this is more of a "how to programmatically connect to Exchange" question, rather than a Powershell question.

    Where did you find sample code that showed that using the $Body variable as you did is the correct way to connect to OWA?

    Invoke-WebRequest accepts a -Credential variable, so why not use that?

    https://learn.microsoft.com/en-us/powershell/module/exchange/connect-mailbox?view=exchange-ps

    https://blog.kloud.com.au/2016/04/19/programmatically-read-email-from-an-exchange-sever-mailbox/

    https://www.nakivo.com/blog/how-to-connect-office-365-exchange-online-powershell/


  3. Mikhail Firsov 1,876 Reputation points
    2020-11-20T15:12:11.863+00:00

    Hello evereone,

    Thank you for your replies!

    1) "What happens if you use If ($null -eq $LoginResponse) instead? " - it returns False: sorry, I was not accurate saying $LoginResponse is null because I do define the $LoginResponse as ="" (I just didn't print-screen it) so it's not null but the empty string.

    2) "Where did you find sample code that showed that using the $Body variable as you did is the correct way to connect to OWA? " - why not? OWA is just one of examples of the forms-based authentication for IIS - at least I had no reasons to think that's impossible. My goal is just to check the OWA is alive so I thought it should working... Will read your articles, thanks!

    3) -ErrorAction STOP

    41503-9.png

    Seems $LoginResponse = Invoke-WebRequest 'https://www.contoso.com/login/' -SessionVariable 'Session' -Body $Body -Method 'POST' does not work with OWA ...strange enough providing that the $Session variable gets created (without STOP of course) ... :(

    41521-10.png

    0 comments No comments

  4. Mikhail Firsov 1,876 Reputation points
    2020-11-20T15:17:59.013+00:00

    P.S. With the -ErrorAction STOP IE does not pop up!


  5. Mikhail Firsov 1,876 Reputation points
    2020-11-23T11:31:29.247+00:00

    "Have you retrieved the response code to see why the Invoke-WebRequest failed?" - "The remote server returned an error: (440) Login Timeout." That's the bad thing :(

    The "odinary" web requests work exactly as they should: connecting to the existing and alive sites return $LR.StatusCode=200, connecting to (for example) non-existing sites return nothing ($LR is null or the empty string)

    $LR=Invoke-WebRequest 'https://google.ru/' - Code=200

    $LR=Invoke-WebRequest 'https://gooEgle.ru/' - $LR =""

    And I was wrong: IE pops up even with the -ErrorAction Stop (I made a mistake in the word Error so PS just threw syntax error but did not run IE).

    Sounds strange to me that MS's own example does not work :(