Reputation: 319
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!
Upvotes: 3
Views: 4552
Reputation: 22821
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
Upvotes: 2