How do I write this Powershell Function More Efficently? -


i.ve come @ many different ways , looked on alot of sites efficient powershell scripts still use making script more efficient...it uses 3-4% of cpu. main drag on processor mainlogic function. nested in while($true) loop @ start of body of script , runs time...when parameters in mainlogic function change function breaks , if statements tested in body of script. i'm trying learn how write more efficient code ...im practicing lisp developer , think lesson on powershell efficiency . i've rewritten script 3 times , still need making better...i see windows process use no cpu , run time , smart code. i'd love good. need in script\i have test things tested in script. commented alot aid in helping me

ive tried these things:

  • am using right construct situation? i've come @ 1 many way's...i think there might better mathematical approach....i'm learning linear algebra me this.
  • am doing many or unnecessary operations? that's main question....could use new ideas
  • am working many objects? not sure if there way ....i think used minimum ojects getwmiobject better get-process? etc.
  • is pipeline best approach? think pipelined well
  • is there command model may work better? googled can't find command model pertaining powershell
  • am thinking objects properly? can't seem find on google powershell object lesson have read 1 1\2 powershell books though there no similar code mine in them
  • am using -filter (if available)? not available get-process
  • am reinitializing variables unnecessarily? no reinitialization
  • are loops designed efficiently? think designed them best way - while($true) nested inside while($true) i'm not running whole script every loop....i need stuff in mainlogic function (my loop runs time)
  • am using foreach-object when work well? not applicable
  • do retrieve values once, , save in variable if needed again later?...i this
  • are passing large collections pipeline instead of storing in object?...not doing this
  • am testing condition unnecessarily? i?

is big script or not seeing big picture far coding....pls cite online resource me write more efficient code, if see holes in logic.

here script: mainlogic function processor hog , needs looked at....i added rest aid in assistance. in advance

#this function ran in while($true) loop , main place in script information  firefox.skype , vpn processes gathered. function global:mainlogic() {  #monitor skype workingset changes - if change detected change processed under #"skype logic" section find out if skype call in progress.  $skypeprocess1 = get-process | where-object {$_.processname -eq "skype"} ss -s 1 $skypeprocess2 = get-process | where-object {$_.processname -eq "skype"}  $skypewsdiff = $skypeprocess2.workingset - $skypeprocess1.workingset  ###skype logic###  #test skype call status , if changed break function in ###skype break### sectiom below ,  #check parameters in body of script.  $y = $x wo "`$x `= $x" wo "`$y `= $y"  #testing information gathered in above section see if skype call in progress.  if($skypewsdiff -gt 8000000){$x = 1} elseif($skypewsdiff -lt -11000000){$x = 2} wo "function2 `$x `= $x" if ($x -eq 1){wo "skype telephone call in progress" } if ($x -eq 2){wo "skype telephone call not in progress" } wo "`$skypewsdiff `= $skypewsdiff"  ###skype break### if($y -ne $x){wo "skype changed - break" break} wo "`$x `= $x" wo "`$y `= $y" #skype logic end  #firefox status gathered below , saved variable $a saved variable $b here , stored compare later  #with variable $a see if firefox status changed.  $b = $a wo "`$a `= $a" wo "`$b `= $b"   #vpn status gathered below , saved variable $c saved variable $d here , stored compare later  #with variable $c see if vpn status changed.  $d = $c wo "`$c `= $c" wo "`$d `= $d"  #firefox , vpn status saved here variables comparison later , above section in script. if(ps firefox -ea 0){$a = 3 ; wo "firefox open" } if(-not(ps firefox -ea 0)){$a = 4 ; wo "firefox closed" } if(ipconfig | select-string 'ppp adapter'){$c = 5 ; wo "vpn connected" } if((ipconfig | select-string 'ppp adapter') -eq $null){$c = 6 ; wo "vpn not connected" }  #this firefox variable comparison mentioned above happens. if change detected script #breaks function , subsequently if\then logic tested in body of script. if($b -ne $a){wo "firefox changed - break" break} wo "`$a `= $a" wo "`$b `= $b"  #this vpn variable comparison mentioned above happens. if change detected script #breaks function , subsequently if\then logic tested in body of script. if($d -ne $c){wo "vpn changed - break" ss -s 2 break} wo "`$c `= $c" wo "`$d `= $d"  }  #functions   #if firefox open , vpn connected - disconnect vpn  if ((ps firefox -ea 0) -and (ipconfig | select-string 'ppp adapter')) { rasdial "btguard vpn" /disconnect ss -s 5 }  #if firefox not open , vpn not connected - connect vpn if ((-not(ps firefox -ea 0)) -and ((ipconfig | select-string 'ppp adapter') -eq $null)) { rasdial "btguard vpn" joe 11223344 }  while($true){  while($true){. mainlogic} #if skype call not in progress(variable $x) , firefox open , vpn not connected - nothing if($x -eq 2 -and (ps firefox -ea 0) -and (ipconfig | select-string 'ppp adapter') -eq $null) {  } #elseif skype call not in progress(variable $x) , firefox not open , vpn connected - nothing elseif($x -eq 2 -and (-not(ps firefox -ea 0)) -and (ipconfig | select-string 'ppp adapter')){  } #elseif skype call not in progress(variable $x) , firefox open , vpn connected - disconnect vpn elseif($x -eq 2 -and (ps firefox -ea 0) -and (ipconfig | select-string 'ppp adapter')){  ss -s 1 rasdial "btguard vpn" /disconnect  } #elseif skype call not in progress(variable $x) , firefox not open , vpn not connected - connect vpn elseif($x -eq 2 -and (-not(ps firefox -ea 0)) -and (ipconfig | select-string 'ppp adapter') -eq $null){  rasdial "btguard vpn" joe 11223344 } #elseif skype call in progress(variable $x) - nothing elseif($x -eq 1){ }} 

cpu usage of 3-4% isn't much. being said, use measure-command see statements consume time , not.

in general sense, try , avoid unecessary work. is, commandlets provide built-in filtering. such, piping output ? wasteful. consider get-process:

measure-command -expression { get-process | ? { $_.processname -eq "iexplore" } } measure-command -expression { get-process -name "iexplore"  } 

in system first 1 takes 13 ms on average. second 1 takes 1.5 ms. improvement decade.

in addition, consider storing command results variables instead of re-running commands time. example, code littered statements ipconfig | select-string. instead of those, store output variable , update variable when necessary.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -