简体   繁体   English

PowerShell 随机打开关闭浏览器

[英]PowerShell Open Close Browser Randomly

How Do I Open and close chrome browser randomly in an infinite loop basically I need a script to open 3 URLs on randomly the whole night.如何在无限循环中随机打开和关闭 chrome 浏览器基本上我需要一个脚本来在整个晚上随机打开 3 个 URL。

$URLs = @(
'http://site.tld/url1'
'http://site.tld/url2'
'http://site.tld/url3'
)

$minSeconds = 10 * 60
$maxSeconds = 15 * 600

# Loop forever
while($true){
# Send requests to all 3 urls
$URLs |ForEach-Object {
Invoke-WebRequest -Uri $_
}

# Sleep for a random duration between 10 and 15 minutes
Start-Sleep -Seconds (Get-Random -Min $minSeconds -Max $maxSeconds)
}

Invoke-* does not open a browser. Invoke-* 不打开浏览器。 You use those to interact with web site info without the need for a browser.您可以使用它们与 web 站点信息进行交互,而无需浏览器。 You'd want to use Start-Process, which will open the default browser.您想使用 Start-Process,它将打开默认浏览器。 You can of course instead start Chrome.您当然可以改为启动 Chrome。

$URLs = @(
'http://site.tld/url1'
'http://site.tld/url2'
'http://site.tld/url3'
)

$minSeconds = 10 * 60
$maxSeconds = 15 * 600

# Loop forever
while($true)
{
    # Send requests to all 3 urls
    $URLs | ForEach-Object {Start-Process $PSitem}

    # Sleep for a random duration between 10 and 15 minutes
    Start-Sleep -Seconds (Get-Random -Min $minSeconds -Max $maxSeconds)
}

After that sleep line, you need to add code to close the browser.在该 sleep 行之后,您需要添加代码以关闭浏览器。

$URLs | 
ForEach-Object {Start-Process -FilePath chrome.exe -ArgumentList $PSitem}

Stop-Process -Name chrome -Force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM