The next change just uses 1 call to get the disk information instead of 2
Measure-Command -Expression {
$srvs = ‘W16TP5TGT01’, ‘W16TP5TGT02′
for ($i=1; $i -le 150; $i++){
foreach ($srv in $srvs) {
$cs = New-CimSession -ComputerName $srv
$bootupMemory = Get-CimInstance -Query “SELECT * FROM Win32_OperatingSystem” -CimSession $cs
$cpuLoad = Get-CimInstance -Query “SELECT * FROM Win32_Processor” -CimSession $cs
$tSessions = Get-CimInstance -Query “SELECT * FROM Win32_TerminalService” -CimSession $cs
$sfilt = “Name=’imaservice’ OR Name=’mfcom’ OR Name=’cpsvc’ OR Name=’msmq'”
$reqserv = Get-CimInstance -ClassName Win32_Service -Filter $sfilt -CimSession $cs
$ima = $reqserv | where Name -eq ‘imaservice’
$mfcom = $reqserv | where Name -eq ‘mfcom’
$ctxPrintMgr = $reqserv | where Name -eq ‘cpsvc’
$msmqstatus = $reqserv | where Name -eq ‘msmq’
$dfilt = “Deviceid=’c:’ OR Deviceid=’D:'”
$drives = Get-CimInstance -ClassName Win32_Logicaldisk -Filter $dfilt -CimSession $cs
$cDrive = $drives | where deviceid -eq ‘c:’
$dDrive = $drives | where deviceid -eq ‘d:’
Remove-CimSession -CimSession $cs
}
}
}
Time now becomes
Days : 0
Hours : 0
Minutes : 6
Seconds : 36
Milliseconds : 923
Ticks : 3969235528
TotalDays : 0.00459402260185185
TotalHours : 0.110256542444444
TotalMinutes : 6.61539254666667
TotalSeconds : 396.9235528
TotalMilliseconds : 396923.5528
Not such a dramatic change but overall we’re now taking 26.4% less time to run the code.
The post Optimising WMI calls–part 3 appeared first on PowerShell for Windows Admins.