Powershell脚本CPU使用率高

内森

我有一个简单的脚本,可以在多个服务器上查找文件。问题是,每台服务器上都挂有CPU并导致生产工作负载出现问题。如何使该脚本不对我的机器进行DDoS?

Start-transcript C:\tools\Querylog.txt

$Servers = Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem

Invoke-command -ComputerName $Server-ScriptBlock {Get-ChildItem -Path $_.Root -Recurse -Force -File -ErrorAction SilentlyContinue | Where-Object { $_.Name -like '*somefile-readme*' } |Out-File -FilePath <filePath>\results.txt -Append}

Stop-Transcript
班德最大

与使用-Filter选项相比,与Get-ChildItem返回所有对象并使用过滤相比,它应该具有更高的性能Where-Object


另外,与您的CPU问题无关,但在编写Get-ADComputer呼叫的方式上,StringScriptBlock-Filter在这些cmdlet参数上使用a而不是a从那个答案:

-Filter不支持ScriptBlocks,但它们有时有点工作,因为它们在获得评估之前就已获得ToString。如果您使用简单的变量扩展(例如$_或)$emailAddress它们在技术上会起作用,但最终将使您头疼,尤其是当您尝试访问对象属性(如上)时,因为它根本不起作用。每次都使用字符串过滤器,如果需要将对象属性用作过滤器参数,请使用Variable SubstitutionCommand Substitution

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章