如何更有效地编写此Powershell函数?

时间:2013-05-14 12:18:48

标签: function powershell cpu-usage

我以这么多不同的方式来看看很多关于Efficient Powershell Scripts的网站 但我仍然可以使用帮助使这个脚本更有效...它使用3-4%的CPU。处理器上的Main Drag是MainLogic功能。它嵌套在我脚本正文开头的While($ true)循环中并且一直运行...当MainLogic函数中的某些参数改变函数断点时,如果那么语句在那里被测试剧本。我正在努力学习如何编写更高效的代码......我正在练习成为一名Lisp开发人员,我认为关于Powershell效率的好教训对我们所有人都有好处。我已经重写了这个脚本3次,我仍然需要帮助使它变得更好......我看到大多数Windows进程几乎不使用CPU,它们一直运行并且是非常智能的代码。我很乐意那么好。我需要脚本中的所有内容\我必须测试脚本中测试的所有内容。我评论它很多,以帮助有人帮助我

我尝试过这些东西:

  • 我是否在这种情况下使用正确的构造?我已经走过很多方了......我想我可能会有更好的数学方法......我正在学习线性代数来帮助我。
  • 我做了太多或不必要的操作吗?这是我的主要问题....可以使用新想法的帮助
  • 我是否处理过多物体?不确定是否有另一种方式....我认为我使用了最小的项目,但Getwmiobject比Get-Process更好?等
  • 管道是最好的方法吗?我想我流畅了
  • 是否有其他可能更好的命令模型?我用谷歌搜索但找不到与Powershell有关的命令模型
  • 我是否正确地考虑了物体?似乎无法在Google上找到一个很好的PowerShell对象课程我已经阅读了1 1 \ 2 Powershell书籍但是没有类似的代码可以使用它们
  • 我使用-Filter(如果有的话)?不适用于Get-Process
  • 我是否不必要地重新初始化变量?没有重新初始化
  • 我的循环是否有效设计?我认为我设计了最好的方法 - 一段时间($ true)嵌套在另一个($ true)所以我没有在每个循环中运行我的整个脚本....我需要MainLogic函数中的所有东西(我的循环)这一直在运行)
  • 我使用Foreach-Object的时候还能用吗?不适用
  • 我是否只检索一次值,如果以后需要再次保存在变量中?...我这样做
  • 您是否将大型集合传递给管道而不是存储在对象中?...不执行此操作
  • 我是否在不必要地测试病情?我呢?

这只是一个很大的脚本,还是我没有看到编码的全局......请引用在线资源来帮助我编写更有效的代码,如果你在我的逻辑中看到了Holes。

这是我的脚本:MainLogic功能是处理器的猪,所有需要查看....我添加了其余的以帮助我的帮助。在此先感谢

#this function is ran by itself in a while($true) loop and is the main place in my script where information 
about Firefox.Skype and VPN Processes is gathered.
function global:MainLogic()
{

#Monitor Skype WorkingSet for any changes - If change is detected the change it is processed under the
#"Skype Logic" Section to find out if a Skype call is 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 and if changed break from this function in the ###SKype Break### sectiom below and 
#check all parameters in body of script.

$y = $x
wo "`$x `= $x"
wo "`$y `= $y"

#Testing Information gathered in above section to 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 and saved to variable $a is saved to variable $b here and stored to compare later 
#with variable $a to see if Firefox Status changed.

$b = $a
wo "`$a `= $a"
wo "`$b `= $b"


#VPN Status gathered below and saved to variable $c is saved to variable $d here and stored to compare later 
#with variable $c to see if VPN Status changed.

$d = $c
wo "`$c `= $c"
wo "`$d `= $d"

#Firefox and VPN status are saved here to variables for comparison later and above this section in the script.
if(ps firefox -ea 0){$a = 3 ; wo "Firefox is Open"
}
if(-not(ps firefox -ea 0)){$a = 4 ; wo "Firefox is Closed"
}
if(ipconfig | Select-String 'PPP adapter'){$c = 5 ; wo "VPN is Connected"
}
if((ipconfig | Select-String 'PPP adapter') -eq $null){$c = 6 ; wo "VPN is not connected"
}

#This is where the Firefox Variable comparison mentioned above actually happens. If change is detected the script
#breaks from this function and subsequently if\then logic is tested in body of script.
if($b -ne $a){wo "Firefox changed - break"
break}
wo "`$a `= $a"
wo "`$b `= $b"

#This is where the VPN Variable comparison mentioned above actually happens. If change is detected the script
#breaks from this function and subsequently if\then logic is 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 is Open and VPN is Connected - Disconnect VPN

if ((ps firefox -ea 0) -and (ipconfig | Select-String 'PPP adapter')) {
rasdial "BTGuard VPN" /DISCONNECT
ss -s 5
}

#If Firefox is not Open and VPN is 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) and Firefox is open and VPN is not connected - do 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) and Firefox is not open and VPN is connected - do 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) and Firefox is open and VPN is 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) and Firefox is not open and VPN is not connected - Connect to 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) - Do Nothing
elseif($x -eq 1){
}}

1 个答案:

答案 0 :(得分:4)

CPU使用率为3-4%并不是那么多。话虽如此,使用Measure-Command来查看哪些语句消耗时间而哪些语句不消耗。

从一般意义上讲,尽量避免不必要的工作。也就是说,一些命令行开关提供内置过滤。因此,输出到?的管道是浪费的。考虑Get-Process

Measure-Command -Expression { get-process | ? { $_.processname -eq "iexplore" } }
Measure-Command -Expression { get-process -name "iexplore"  }

在我的系统中,第一个平均需要大约13毫秒。第二个需要大约1.5毫秒。改善大约十年。

此外,考虑将命令结果存储到变量中,而不是一直重新运行命令。例如,代码中充斥着ipconfig | Select-String之类的语句。而不是那些,将输出存储到变量中,并仅在必要时更新变量。

相关问题