如果使用PowerShell打开另一个URL,则在IE中打开一组URL [英] Open a set of URLs in IE if another URL is open using PowerShell
问题描述
在第二次运行此脚本时,如果我第一次关闭Bing,我可以打开Microsoft和Yahoo;如何在不先关闭Bing的情况下打开这些网站?错误:
On the second run of this script, I can open Microsoft and Yahoo if I first close Bing; how can I open those sites without first closing Bing? The error:
Method invocation failed because [System.Object[]] doesn't contain a method named 'Navigate2'.
+ $ie.Navigate2 <<<< ("www.microsoft.com", $navOpenInNewTab);
+ CategoryInfo : InvalidOperation: (Navigate2:String) [], Runtime Exception
+ FullyQualifiedErrorId : MethodNotFound
当我打开Bing和Google时,为什么会失败,但是当我打开Google时却没有?
Why does it fail when I have Bing and Google open, but not when I have Google open?
# Set BrowserNavConstants to open URL in new tab
# Full list of BrowserNavConstants: https://msdn.microsoft.com/en-us/library/aa768360.aspx
$navOpenInNewTab = 0x800;
$navOpenInBackgroundTab = 0x1000;
$ie = $null
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }
} else {
$ie = New-Object -COM "InternetExplorer.Application"
sleep -milliseconds 50
$ie.visible = $true
}
$today = (get-date).DayOfWeek
switch ($today) {
"Someday" {
$ie.Navigate("www.bing.com");
$ie.Navigate2("www.yahoo.com", $navOpenInBackgroundTab); break
}
default {
$google = $false
# Check if Google open
foreach ($tab in $ie) {
if ($tab.LocationURL.Contains("google"))
{ $google = $true; break }
}
# If Google open on second run, open Microsoft and Yahoo
if ($google) {
$ie.Navigate2("www.microsoft.com", $navOpenInNewTab);
$ie.Navigate2("www.yahoo.com", $navOpenInBackgroundTab);
} else {
# On first run, open Bing and Google
$ie.Navigate("www.bing.com");
$ie.Navigate2("www.google.com", $navOpenInBackgroundTab);
}
break
}
}
# Cleanup
'ie' | ForEach-Object {Remove-Variable $_ -Force}
推荐答案
您的问题似乎是,脚本无法在第二次运行时找到(右侧)打开的IE实例。在PowerShell ISE(或PowerShell PowerGUI)中调试脚本,并在 $ ie =(New-Object -COMShell.Application)中设置断点.Windows()| ? {$ _。名称-eqInternet Explorer}
Your problem seems to be, that the script fails to find (the right) open IE instance on the second run. Debug the Script in the PowerShell ISE (or the PowerShell PowerGUI) and set a breakpoint at $ie=(New-Object -COM "Shell.Application").Windows() | ? {$_.Name -eq "Internet Explorer"}
在下面的交互式PowerShell窗口中运行以下命令:(New-Object -COMShell.Application)。Windows()|选择对象{$ _。名称}
Run the following command in the interactive PowerShell Window below: (New-Object -COM "Shell.Application").Windows() | Select-Object { $_.Name }
你得到这样的输出吗?
File Explorer
Internet Explorer
Internet Explorer
Internet Explorer
Internet Explorer
当IE窗口有不同的名称时,尝试根据需要调整代码。
以下代码适用于我的机器:I选择第一个IE实例(否则你会因为打开多个网站而遇到问题)。
Try to adjust your code as needed when your IE windows have different Names.
The following code works on my machine: I select just the first IE instance (otherwise you get problems since you open multiple sites).
...
# If Google open on second run, open Microsoft and Yahoo
if ($google) {
sleep -milliseconds 50
$ie[0].Navigate2("www.microsoft.com", $navOpenInBackgroundTab);
sleep -milliseconds 50
$ie[0].Navigate2("www.yahoo.com", $navOpenInBackgroundTab);
} else { ...
这篇关于如果使用PowerShell打开另一个URL,则在IE中打开一组URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!