答案1
您的要求没有明确定义,但据我对您问题的理解,我可以为您提供以下解决方案:如果不是星期天,我们搜索所有 IE 实例(如果它们在 google.com 上),如果是,则打开 yahoo.com。
这并不完美,因为如果再次运行脚本,您会得到更多的 yahoo.com 标签(但这种情况不是由您指定的),并且还有其他东西,另外您应该添加更多检查。
但无论如何,这是我的方法:
# Set BrowserNavConstants to open URL in new tab
$navOpenInBackgroundTab = 0x1000; # Full list of BrowserNavConstants: https://msdn.microsoft.com/en-us/library/aa768360.aspx
$ie = $null
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
#Write-Output "IE is running"
$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
# Depending on the day of the week discovered, assign the right day's array into the sitesToOpen array.
switch ($today) {
"Sunday" {
$ie.navigate("http://bing.com"); break
}
default {
$google = $false
foreach($tab in $ie) {
if($tab.LocationURL.Contains("www.google.com"))
{$google = $true; break}
}
if($google) {
$ie.Navigate2("http://yahoo.com", $navOpenInBackgroundTab);
} else {
$ie.navigate("http://google.com")
}
break
}
}
# Cleanup
'ie' | ForEach-Object {Remove-Variable $_ -Force}