Close Specific Website with Stop-Process in PowerShell

瘦欲@ 提交于 2021-01-27 12:51:37

问题


I was wondering if anyone is aware of a way to close a specific website that the user has open in Google Chrome?

As of right now, I have Google Chrome opening up to a website that the user types in. The site is assigned to the $question variable that you will see below:

$question2 = Read-Host "Would you like to copy $question back to the server?"
if( ($question2 -eq 'yes') -or ($question2 -eq 'YES') -or ($question2 -eq 'Yes') -or ($question2 -eq 'Ye') -or ($question2 -eq 'Y') -or ($question2 -eq 'y') ) {
    start chrome.exe http://$question.website.org/
}

I am wondering if there is a way after Chrome opens to that website, that I can also close it as well.

I'm aware of

Stop-Process -processname chrome

but that will close Chrome all together. I am looking for a way to only close the website that I opened up for them.

Any suggestions are greatly appreciated.

Thanks in Advance!


回答1:


Very difficult I believe. It seems that when Chrome starts it spawns various other process that end up being the windows/tabs and the process that you actually start only facilitates this and then exits. I thought it might be possible to force it to open in a new window and track the PID of that program, but the PID that gets returned rapidly disappears.

I also thought it might be possible to look at the ProcessStartInfo information to see if you could pass any arguments through that you can later query, but again when you launch the process it does what it needs to then disappears and the arguments aren't passed to the spawned processes.

For what it's worth, this is how far I got:

[PS] 118# $a = new-object system.diagnostics.process
[PS] 119# $a.startinfo.filename = "C:\program files (x86)\Google\chrome\application\chrome.exe"
[PS] 120# $a.startinfo.arguments = "--new-window http://google.com"
[PS] 121# $a.startinfo.useshellexecute = $false
[PS] 122# $a.startinfo.verb = "test"
[PS] 123# $a.start()
True
[PS] 124# $a

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
              0        0          0     0     0.08   7332


[PS] 125# get-process | ? {$_.id -eq 7332}
[PS] 126# get-process | ? {$_.name -eq 'chrome'} | % {$_.startinfo.arguments}











[PS] 127#

Although this did create a new window with http://google.com, as you can see the process that was returned, 7332, doesn't exist and the StartInfo.Arguments of any other chrome processes are blank

As an aside to not getting this working, you can vastly simplify your answer logic for yes/no by using a simple regular expression:

($question2 -imatch "^y(es)?$")
($question2 -imatch "^no?$")

as proved:

[PS] 404> $("y" -imatch "y(es)?$")
True
[PS] 405> $("YES" -imatch "y(es)?$")
True
[PS] 406> $("N" -imatch "^no?$")
True
[PS] 407> $("nn" -imatch "^no?$")
False


来源:https://stackoverflow.com/questions/21858079/close-specific-website-with-stop-process-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!