2

I can login to facebook, or google, or stackoverflow using this simple script (or a slight variation depending on the Ids). However, my company has a website and I can't login to that using powershell.

$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true

$ie.navigate("www.loginpage.com")

start-sleep 1; 
# I was using this before, but it would never leave the loop...
# while($ie.ReadyState -ne 4) {start-sleep -m 100} 

$ie.document.getElementById("username").value = "username" 
$ie.document.getElementById("password").value = "password" 
$ie.document.getElementById("loginform").submit()

The only difference I think is that the website is on our "Intranet", not the internet, and for some reason powershell distinguishes between the two. But I'm not sure, I can still normally open up IE and go to the url and view source and see the tag ID's and whatnot.

Does anyone know why I'm getting the following error when I use $ie.document.getElementById in this case.

  You cannot call a method on a null-valued expression.
 + CategoryInfo          : InvalidOperation: (getElementById:String) [], RuntimeException
 + FullyQualifiedErrorId : InvokeMethodOnNull

I know the tags are:

<input name="username" id="username" langcode="EN" type="text">
<input name="password" id="password" type="password">

Any ideas?

Charles Clayton
  • 17,005
  • 11
  • 87
  • 120
  • Possible duplicate http://stackoverflow.com/questions/15243990/powershell-website-login-not-working?rq=1 – Matt Aug 27 '14 at 18:27
  • That implies that there is no current document in $ie, maybe the page isn't loaded yet? I use `Do {Start-Sleep 1} Until (!($IE.Busy))` to wait for that. – TheMadTechnician Aug 27 '14 at 18:28
  • @TheMadTechnician It broke out of that loop pretty quickly and afterwards I still got the `null-valued expression` error. I commented out a previous wait loop in the above code, and that seems to run indefinitely though. – Charles Clayton Aug 27 '14 at 18:32
  • @Matt and `This question may already have an answer here:`, it doesn't though. I looked at those threads and tested it out with other websites, and it worked so I know it's not the issue with the protected mode or administrator settings, and I included the input tags to show that I looked at that too, and it's a more specific problem than that. – Charles Clayton Aug 27 '14 at 18:35
  • Thats fine. While the websites you have listed work i was considering that there might be another reason for your failure besides that it was intranet. – Matt Aug 27 '14 at 18:38
  • If you run to that point then do `$IE.Document` does it show anything? – TheMadTechnician Aug 27 '14 at 20:15
  • @TheMadTechnician If I call just document, it will run with no errors and if i write-host $ie.document it won't print anything. – Charles Clayton Aug 27 '14 at 21:35
  • It doesn't see the page. Did the page pop up in another window? Is it in a frame? `write-host $ie.document` should give you something like `mshtml.HTMLDocumentClass`. $ie.document.body.innertext should give you the text displayed on the page. If those don't then I'm not sure what to tell you. – TheMadTechnician Aug 27 '14 at 22:26
  • I'm not sure. The page opens up and I can see it and login using it, but powershell just doesn't see it for some reason. As I said, the only thing I think could be different is that it's not the public internet... – Charles Clayton Aug 27 '14 at 22:31
  • I'm having the exact same issue. I'm noticing that `$ie.Busy`will return `true` or `false` if I visit an external website, but will return nothing if I visit an internal website. – eh1160 Jan 15 '16 at 20:37

1 Answers1

2

The solution is to run PowerShell as an administrator. This SO answer helped: https://stackoverflow.com/a/14616609/347303

Community
  • 1
  • 1
eh1160
  • 674
  • 1
  • 6
  • 14