powershell - 如何使用 PowerShell 自动登录 Chrome。我有一个使用 IE 运行的脚本,但需要使用 Chrome

标签 powershell google-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; 
}   

最佳答案

很遗憾,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

关于powershell - 如何使用 PowerShell 自动登录 Chrome。我有一个使用 IE 运行的脚本,但需要使用 Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66143911/

相关文章:

css - 浏览器放大时如何防止CSS内联样式?

css - 在 chrome 中更改滤镜时图像在悬停时移动

powershell - 在 powershell 中写入 machine.config 文件

powershell - 如何测试一个对象是否具有特定属性?

windows - 如何在 CMD 中运行 PowerShell

google-chrome - Chrome 扩展程序 : Custom Protocol?

powershell - 输出单行多行CimInstance

powershell - 使用键盘热键启动 powershell 脚本

php - 使用谷歌浏览器的 php 包含奇怪的 javascript 行为

jquery - Google Chrome 上的 jquery 加载问题