gpt4 book ai didi

windows - 使用 Windows Powershell 打开浏览器

转载 作者:行者123 更新时间:2023-12-03 11:10:23 26 4
gpt4 key购买 nike

我正在学习 Powershell。我在网上找到了几个脚本,修改它们以使文件在 Chrome 和 IE 中打开。该脚本可以正常工作,但 IE 打开时会显示空白页面,但不会像在 Chrome 中那样打开其他选项卡中的所有其他链接。此外,我收到以下错误消息。我希望看到的行为是在 Chrome 和 IE 中打开每个链接,然后在一定时间后关闭每个浏览器。

收到错误:

You cannot call a method on a null-valued expression.
At line:92 char:13
+ $ie.navigate($site );
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Remove-Variable : Cannot find a variable with the name 'ie'.
At line:95 char:13
+ Remove-Variable ie;
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (ie:String) [Remove-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.RemoveVariableCommand

下面是代码,下面列出了文本文件中的链接。

# How long to wait between open site commands
$waitBetweenSitesS = 7;

# How long to wait after a browser's last site before closing its window
$waitBeforeBrowserClose = 7;

# How long to wait between browsers
$waitBetweenBrowsers = 7;

# Name of the file containing the sites to open
$siteUrlFile = "C:\Scripts\URLs.txt";


# Browsers to start
$browsers = @("chrome", "iexplore")

# Start of the script

Function Open-IETabs {
param (
[string[]]$sites )
begin {
$Ie = New-Object -ComObject InternetExplorer.Application }
process {

foreach ($Link in $sites)
{
$Ie.Navigate2($Link, 0x1000)
}
}
end
{
$Ie.Visible = $true }
}
#$path = read-host 'Enter the path of ppm list file'
$sites = gc "C:\Scripts\URLs.txt"
Open-IETabs -Url $sites

# Browsers
foreach ($browser in $browsers)
{

# Sites
$siteCount = 0;
foreach ($site in $sites)
{
$siteCount++;

if ($siteCount -eq 1)
{
if ($browser -eq "chrome" -or $browser -eq "iexplore")
{
# Start the browser with an empty tab because the first page load is currently not captured by uberAgent
$process = Start-Process -PassThru $browser "about:blank"
}
else
{
# Start the browser with the first site
$process = Start-Process -PassThru $browser $site
}

# Store the browser's main process (the first one started)
$browserProcess = $process;

# Wait for the window to open
while ($process.MainWindowHandle -eq 0)
{
Start-Sleep 1
}

if ($browser -eq "chrome" -or $browser -eq "firefox")
{
# Open the first site in a new tab
Start-Process $browser $site
}
}
elseif ($browser -eq "iexplore")
{
# Additional IE tabs need to be opened differently, or new windows will be created instead

$navOpenInNewTab = 0x800;

# Get running Internet Explorer instances
$app = New-Object -ComObject shell.application;

# Grab the last opened tab
$ie = $app.Windows() | Select-Object -Last 1;

# Open the site in a new tab
$ie.navigate($site, $navOpenInNewTab);

# Release the COM objects
Remove-Variable ie;
Remove-Variable app;
}
else
{
# Addition tabs in Chrome/Firefox
Start-Process $site #$browser
}

Start-Sleep $waitBetweenSitesS;
}

Start-Sleep $waitBeforeBrowserClose;

# Close the browser
$browserProcess.CloseMainWindow();
$browserProcess = $null;

Start-Sleep $waitBetweenBrowsers;

}

https://mail.google.com/mail/u/0/#inbox
https://docs.google.com/document/d/1hOc4bdEQ1-KJ5wOsiCt4kVQB-xaHuciQY6Y4X_I7dYA/edit
https://www.google.com/maps/
https://twitter.com/
https://onedrive.live.com/edit.aspx?cid=740de493111072ca&page=view&resid=740DE493111072CA!108&parId=740DE493111072CA!106&app=PowerPoint
https://outlook.live.com/mail/inbox
https://www.dropbox.com/h
https://www.nytimes.com/
https://www.nbcnews.com/
https://foxnews.com/

最佳答案

我无法重现您的错误。不管怎样,你的代码对于你想做的事情来说似乎过于复杂了。我没有得到 Open-IETabs 函数(因为它似乎应该做你在浏览器迭代中的代码无论如何都会做的事情。也许这可能是一个开始:

# How long to wait between open site commands
$waitBetweenSitesS = 7;

# How long to wait after a browser's last site before closing its window
$waitBeforeBrowserClose = 7;

# How long to wait between browsers
$waitBetweenBrowsers = 7;

$sites =
"https://www.google.com/maps/",
"https://www.nytimes.com/",
"https://www.nbcnews.com/"

# Start of the script

#IEXPLORE
$Ie = New-Object -ComObject InternetExplorer.Application
$ie.visible = $true
foreach ($site in $sites){
$Ie.Navigate2($site, 0x1000)
start-sleep -seconds $waitBetweenSitesS
}

Start-Sleep -seconds $waitBetweenBrowsers

#CHROME
$process = Start-Process -PassThru "chrome" "about:blank"
foreach ($site in $sites){
Start-Process "chrome" $site
start-sleep -seconds $waitBetweenSitesS
}

关于windows - 使用 Windows Powershell 打开浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59327346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com