powershell - 如何使用 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;
}
解决方案
不幸的是,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
推荐阅读
- matrix - 从八度音阶中的三列矩阵的两列中减去
- android - 在 DrawerLayout 中更改进度条大小
- javascript - 重新调整窗口大小
- mysql - 迭代数据集并插入 MySQL 表
- java - 为什么我的 Java 程序只返回 ArrayList 中的最后一个字符串
- azure-devops - 如何从持续部署中排除测试项目
- sql - sql server 2017 - 如何创建唯一值并将列与这些相关联
- php - 提取外部网站的 Content-Type 并将 JSON 文件中的值保存为“Null”
- spring-boot - 是否有将 Kafka 数据发送到 AMQP/MQTT 的桥接器?
- google-sheets - 汇总 Google 表格中的合并单元格