我有一个简单的脚本,可以在多个服务器上查找文件。问题是,每台服务器上都挂有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
呼叫的方式上,应String
ScriptBlock
-Filter
在这些cmdlet的参数上使用a而不是a。从那个答案:
-Filter
不支持ScriptBlocks
,但它们有时会有点工作,因为它们在获得评估之前就已获得ToString。如果您使用简单的变量扩展(例如$_
或)$emailAddress
,它们在技术上会起作用,但最终将使您头疼,尤其是当您尝试访问对象属性(如上)时,因为它根本不起作用。每次都使用字符串过滤器,如果需要将对象属性用作过滤器参数,请使用Variable Substitution或Command Substitution。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句