0

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
4

1 回答 1

5

第一个错误至少可以通过两种不同的方式解决:在调用函数时使用命名参数或使用位置信息Login-Gmail装饰参数:$uname

命名参数:

Login-Gmail -uname me@gmail.com

添加位置信息:

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 
  )

您的第二个错误System.Management.Automation.PSCredential是当 IE DOM 需要一个字符串时,您试图分配对象的值(PSCredential)。您可以get-credential完全省略对的调用,而只分配变量中存在的$uname值。

于 2013-08-26T20:06:27.783 回答