I'm trying to understand how to use powershell to automate IE navigation in general, and I found a sample script at http://scriptolog.blogspot.com/2007/01/automated-web-forms.html
Here is the code:
function Login-GMail{
param(
[string]$uname,
[string]$url="http://www.gmail.com",
[bool]$cookie=$false
)
$creds = get-credential $uname
$pass = $creds.GetNetworkCredential().Password
$ie=new-object -com internetexplorer.application
$ie.visible=$true
$ie.navigate($url)
while($ie.ReadyState -ne 4){start-sleep -m 100}
$ie.document.getElementbyId("Email").value=$creds
$ie.document.getElementbyId("Passwd").value=$pass
$ie.document.getElementbyName("PersistentCookie") | foreach {$_.checked=$cookie}
$ie.document.getElementbyId("gaia_loginform").submit()
}
Login-Gmail
The code looks perfectly fine but it does not work as expected
When I run this on a command line, i.e. C:\sandbox\gmail.ps1 me@gmail.com I get an error,
Set-Location : A positional parameter cannot be found that accepts argument 'me@gmail.com'.
At line:1 char:3
+ cd <<<< c:\sandbox\gmail.ps1 p...@gmail.com
+ CategoryInfo : InvalidArgument: (:) [Set-Location], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
When I run this in Powershell GUI and press the green arrow, a pop-up box asks for my username and password. However, when it opens gmail.com, it pre-populates the Username textbox with "System.Management.Automation.PSCredential" and I get the following error
Method invocation failed because [mshtml.HTMLDocumentClass] doesn't contain a method named 'getElementbyName'.
At C:\sandbox\gmail.ps1:18 char:34
+ $ie.document.getElementbyName <<<< ("PersistentCookie") | foreach {$_.checked=$cookie}
+ CategoryInfo : InvalidOperation: (getElementbyName:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
How to troubleshoot this?
EDIT:
@ Goyuix
I made changes to the parameter list, added
Login-Gmail -uname me@gmail.com
and I removed
$creds=get-credential $uname
and get error
You cannot call a method on a null-valued expression.
At C:\sandbox\gmail.ps1:9 char:40
+ $pass = $creds.GetNetworkCredential <<<< ().Password
+ CategoryInfo : InvalidOperation: (GetNetworkCredential:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
and
Method invocation failed because [mshtml.HTMLDocumentClass] doesn't contain a method named 'getElementbyName'.
At C:\sandbox\gmail.ps1:18 char:34
+ $ie.document.getElementbyName <<<< ("PersistentCookie") | foreach {$_.checked=$cookie}
+ CategoryInfo : InvalidOperation: (getElementbyName:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
If I remove
$ie.document.getElementbyName("PersistentCookie") | foreach {$_.checked=$cookie}
But include
$creds = get-credential $uname
Then a pop-up appears with the User name prepopulated with me@gmail.com. After I enter in the Password and press OK, the webpage with gmail appaers and the username is pre-populated with
System.Management.Automation.PSCredential
But here are no errors.
How to fix the code such that it automatically opens gmail and logs me into my account. Here is code so far:
function Login-GMail{
param(
[Parameter(Mandatory=$True,Position=1)][string]$uname,
[Parameter(Mandatory=$False,Position=2)][string]$url="http://www.gmail.com",
[Parameter(Mandatory=$False,Position=3)][bool]$cookie=$false
)
$creds = get-credential $uname
$pass = $creds.GetNetworkCredential().Password
$ie=new-object -com internetexplorer.application
$ie.visible=$true
$ie.navigate($url)
while($ie.ReadyState -ne 4){start-sleep -m 100}
$ie.document.getElementbyId("Email").value=$creds
$ie.document.getElementbyId("Passwd").value=$pass
$ie.document.getElementbyId("gaia_loginform").submit()
}
Login-Gmail -uname me@gmail.com