Powershell网站登录无法正常工作

时间:2013-03-06 09:54:29

标签: powershell

我正在尝试编写一个基本的PowerShell脚本来登录gmail但是有一些问题可以解决。代码如下。我已经评论了一些内容以缩小范围。

$url = "http://gmail.com" 
$username="myusername" 
$password= "123456" 
$ie = New-Object -com InternetExplorer.Application


$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy) 
{ 
Start-Sleep -m 10000; 
} 

$counter = 0
while (($counter -lt 10) -and ($ie.document -eq $null)) {Start-Sleep 1; $counter++}

$ie.document -eq $null
$ie.Document.getElementByID('email').value=$username
#$ie.Document.getElementByID("Password").value=$password 
#$ie.Document.getElementById("signin").Click();

当我运行powershell脚本时,我收到以下错误。

You cannot call a method on a null-valued expression.
At J:\scripts\gmail.ps1:21 char:1
+ $ie.Document.getElementByID('email').value=$username
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

奇怪的是,昨天工作得非常好,今天根本没用。我正在使用IE9。

任何帮助都非常感激。

2 个答案:

答案 0 :(得分:4)

PowerShell IE9 ComObject has all null properties after navigating to webpage给了我答案。由于IE保护模式阻止创建对象,因此必须以管理员身份运行我的脚本。关闭IE保护模式或以管理员身份运行脚本以使其正常工作。

答案 1 :(得分:0)

我认为你在这里犯了一个错误:

$ie.Document.getElementByID("Password").value=$password 

ID应该是passwd,而不是密码。所以用以下方式替换该行:

$ie.Document.getElementByID("Passwd").value=$password 

适合我。