首页 > 解决方案 > 如何使用 PowerShell 自动登录 Chrome。我有一个使用 IE 运行但需要使用 Chrome 的脚本

问题描述

#设置网址。

$Url = "https://www.plexonline.com"; 
$Username="xxxx";
$Password="xxxx";
$CompanyCode="xxx";

为 IE 创建一个新的 COM 对象。

$IE = New-Object -ComObject internetexplorer.application;

将 IE 设置为全屏。

$IE.FullScreen = $true;

将 IE 设置为可见。

$IE.Visible = $true;

将 IE 导航到指定的 URL。

$IE.navigate($url); 

等到 IE 不再忙。

 while ($IE.Busy -eq $true) 
    { 
        Start-Sleep -Milliseconds 500; 
    } 

在各自的字段中插入指定的用户名、密码和公司代码。

$IE.Document.getElementById("txtUserID").value = $Username;
$IE.Document.getElementByID("txtPassword").value = $Password;
$IE.Document.getElementByID("txtCompanyCode").value = $CompanyCode;

开始登录过程。

$IE.Document.getElementById("btnLogin_Label").Click();

继续等到 IE 不再忙。

while ($IE.Busy -eq $true) 
{ 
Start-Sleep -Milliseconds 2000; 
}   

标签: powershellgoogle-chrome

解决方案


不幸的是,Google 不支持 COM 模型,因此没有提供在 PowerShell 中使用的 COM 对象。

但是,您可以使用Start-Process启动 Google Chrome 进程,然后您可以相应地继续执行其余步骤:

$Url = "https://www.plexonline.com"; 
$Username="xxxx";
$Password="xxxx";

$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList @($Username,(ConvertTo-SecureString -String $Password -AsPlainText -Force))

Start-Process "<path to chrome.exe>" $Url -Credential $Credential -WindowStyle Maximized

推荐阅读