Internet explorer Powershell-无法自动将凭据传递到gmail.com
我正试图了解如何使用powershell来实现IE导航的自动化,我在 代码如下:Internet explorer Powershell-无法自动将凭据传递到gmail.com,internet-explorer,powershell,automation,Internet Explorer,Powershell,Automation,我正试图了解如何使用powershell来实现IE导航的自动化,我在 代码如下: function Login-GMail{ param( [string]$uname, [string]$url="http://www.gmail.com", [bool]$cookie=$false ) $creds = get-credential $uname $pass = $creds.GetNetworkCre
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
代码看起来非常好,但没有按预期工作
当我在命令行上运行它时,例如C:\sandbox\gmail.ps1me@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
然后我就搬走了
$creds=get-credential $uname
并得到错误
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
但包括
$creds = get-credential $uname
然后出现一个弹出窗口,其中用户名已预先填充me@gmail.com. 在我输入密码并按OK后,带有gmail Appers和用户名的网页将预先填充
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
第一个错误至少可以通过两种不同的方式解决:在调用
登录Gmail
函数时使用命名参数,或者通过使用位置信息装饰$uname
参数:
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
)
命名参数:
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
变量中的值。第一个错误至少可以通过两种不同的方式解决:在调用登录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
)
命名参数:
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
变量中的值