patternshellMinor
Recycling Internet Explorer
Viewed 0 times
internetrecyclingexplorer
Problem
Is there anything I can do to this code to optimize it to use less resources?
write-host (get-date -format s) " Beginning script..."
Add-Type -AssemblyName System.Windows.Forms
do{
$len = [System.Windows.Forms.Screen]::AllScreens.Length;
if($len-eq1){
$ie = Get-Process -Name iexplore -ErrorAction SilentlyContinue
if ($ie -ne $null){
Write-Host (get-date -format s) "Stopping IE..."
$ie | Stop-Process
}
}elseif($len-eq2){
$ie = Get-Process -Name iexplore -ErrorAction SilentlyContinue
if ($ie -eq $null){
write-Host (get-date -format s) " Starting IE..."
Start-Sleep -s 5 #5
$ie = Open-InternetExplorer -Url http://localhost/ -Left 1366 -FullScreen -InForeground #This is an external function that works well.
write-Host (get-date -format s) " IE Started"
}
}
start-sleep -s 10
}while(1-eq1)Solution
You should have included
A few of optimizations...
-
You will not need to
-
Unless the number of screens changes per iteration of the loop, getting this value could be taken outside the loop. Otherwise, consider a
-
The condition for the infinite loop
-
When IE is started, the script does not use the return value so it isn't necessary to assign it to
-
The use of
The resulting script could look like:
Open-InternetExplorer - without it, this code is incomplete and does not run.A few of optimizations...
-
You will not need to
Add-Type if all you need is the screen property. It might also make more sense to use Count rather than Length, afterall this code relies on the number of screens.-
Unless the number of screens changes per iteration of the loop, getting this value could be taken outside the loop. Otherwise, consider a
switch and remove $len altogether:Switch ([System.Windows.Forms.Screen]::AllScreens.Count) {
1 {
...
}
2 {
...
}
}-
The condition for the infinite loop
while (1-eq1) can be simplified to avoid having to calculate a result. Use while ($true) or while (1).-
When IE is started, the script does not use the return value so it isn't necessary to assign it to
$ie:$null = Open-InternetExplorer -Url http://...-
The use of
$ieis actually unnecessary in both cases.The resulting script could look like:
write-host (get-date -format s) " Beginning script..."
do {
Switch ([System.Windows.Forms.Screen]::AllScreens.Count) {
1 {
Get-Process -Name iexplore -ErrorAction SilentlyContinue | Stop-Process -Force
If ($?) {
Write-Host (get-date -format s) "Stopping IE..."
}
}
2 {
if ((Get-Process -Name iexplore -ErrorAction SilentlyContinue) -eq $null){
write-Host (get-date -format s) " Starting IE..."
Start-Sleep -s 5 #5
$null = Open-InternetExplorer -Url http://localhost/ -Left 1366 -FullScreen -InForeground
write-Host (get-date -format s) " IE Started"
}
}
}
start-sleep -s 10
} while(1)Code Snippets
Switch ([System.Windows.Forms.Screen]::AllScreens.Count) {
1 {
...
}
2 {
...
}
}$null = Open-InternetExplorer -Url http://...write-host (get-date -format s) " Beginning script..."
do {
Switch ([System.Windows.Forms.Screen]::AllScreens.Count) {
1 {
Get-Process -Name iexplore -ErrorAction SilentlyContinue | Stop-Process -Force
If ($?) {
Write-Host (get-date -format s) "Stopping IE..."
}
}
2 {
if ((Get-Process -Name iexplore -ErrorAction SilentlyContinue) -eq $null){
write-Host (get-date -format s) " Starting IE..."
Start-Sleep -s 5 #5
$null = Open-InternetExplorer -Url http://localhost/ -Left 1366 -FullScreen -InForeground
write-Host (get-date -format s) " IE Started"
}
}
}
start-sleep -s 10
} while(1)Context
StackExchange Code Review Q#71656, answer score: 2
Revisions (0)
No revisions yet.