我以这么多不同的方式来看看很多关于Efficient Powershell Scripts的网站 但我仍然可以使用帮助使这个脚本更有效...它使用3-4%的CPU。处理器上的Main Drag是MainLogic功能。它嵌套在我脚本正文开头的While($ true)循环中并且一直运行...当MainLogic函数中的某些参数改变函数断点时,如果那么语句在那里被测试剧本。我正在努力学习如何编写更高效的代码......我正在练习成为一名Lisp开发人员,我认为关于Powershell效率的好教训对我们所有人都有好处。我已经重写了这个脚本3次,我仍然需要帮助使它变得更好......我看到大多数Windows进程几乎不使用CPU,它们一直运行并且是非常智能的代码。我很乐意那么好。我需要脚本中的所有内容\我必须测试脚本中测试的所有内容。我评论它很多,以帮助有人帮助我
我尝试过这些东西:
这只是一个很大的脚本,还是我没有看到编码的全局......请引用在线资源来帮助我编写更有效的代码,如果你在我的逻辑中看到了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){
}}
答案 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
之类的语句。而不是那些,将输出存储到变量中,并仅在必要时更新变量。