Powershell - 无法自动将凭据传递给gmail.com

Glo*_*wie 0 powershell internet-explorer automation

我试图了解如何使用powershell来自动化IE导航,我在http://scriptolog.blogspot.com/2007/01/automated-web-forms.html找到了一个示例脚本

这是代码:

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
Run Code Online (Sandbox Code Playgroud)

代码看起来非常好,但它不能按预期工作

当我在命令行上运行它时,即C:\ sandbox\gmail.ps1 me@gmail.com我收到错误,

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
Run Code Online (Sandbox Code Playgroud)

当我在Powershell GUI中运行它并按下绿色箭头时,弹出框会询问我的用户名和密码.但是,当它打开gmail.com时,它使用"System.Management.Automation.PSCredential"预填充用户名文本框,我收到以下错误

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
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

编辑:

@ Goyuix

我添加了参数列表的更改

Login-Gmail -uname me@gmail.com
Run Code Online (Sandbox Code Playgroud)

我删除了

$creds=get-credential $uname
Run Code Online (Sandbox Code Playgroud)

并得到错误

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

如果我删除

$ie.document.getElementbyName("PersistentCookie") | foreach {$_.checked=$cookie}
Run Code Online (Sandbox Code Playgroud)

但包括

$creds = get-credential $uname
Run Code Online (Sandbox Code Playgroud)

然后会出现一个弹出窗口,其中用户名已预先填入me@gmail.com.输入密码并按确定后,带有gmail appaers和用户名的网页已预先填充

System.Management.Automation.PSCredential

但这里没有错误.

如何修复代码,使其自动打开Gmail并将我登录到我的帐户.这是到目前为止的代码:

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
Run Code Online (Sandbox Code Playgroud)

Goy*_*uix 5

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

命名参数:

Login-Gmail -uname me@gmail.com
Run Code Online (Sandbox Code Playgroud)

添加位置信息:

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 
  )
Run Code Online (Sandbox Code Playgroud)

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