4
\$\begingroup\$

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)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Can you give us a little more context about the script's purpose? \$\endgroup\$
    – Thriggle
    Dec 20 '16 at 4:17
2
\$\begingroup\$

You should have included 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)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.