Very Slow Output Occuring - Help! RRS feed

  • Question

  • Hi Folks,

    The script below is running very, very, very slow. it is taking about 10 minutes per object to be written to a CSV file. I have thousands of objects to enumerate so need to speed up the script. Can anyone point out a way to do that?

    The script is looking at an SCCM collection, getting the membership of the collection, looking up the user\machine relationship, and finally writing to a CSV file. 

    Thanks for the input and help!

    Import-Module "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.PSD1"
    $CollName = "Windows 10 Ready"
    $Collection = Get-WmiObject -ComputerName $SiteServer -NS $ns -Class 'SMS_Collection'
    $MyCollection = $Collection | Where-Object { $_.Name -eq $CollName }
    $MyCollectionMembers = Get-WmiObject  -ComputerName $SiteServer -NS `
    $ns -Query "select * from SMS_CM_RES_COLL_$($MyCollection.CollectionID)"
    $ListofMachines = @()
    Foreach ($member in $MyCollectionMembers) {
      $oldErrCount = $error.Count
     $Name = $member.Name.ToString()
     $ListofMachines += $Name
     ForEach ($Machine in  $ListofMachines){
    $ResourceNames = Get-WmiObject SMS_UserMachineRelationship -NS $ns -ComputerName $SiteServer |Where {$_.types -contains 1 -and $_.sources -contains 4 -and $_.resourcename -like $Machine}
    $ResourceNames | Select ResourceName, UniqueUserName |Export-CSV D:\ScorchFileOutput\Win10Ready1.csv -NoTypeInformation -Append

    Friday, December 1, 2017 6:38 PM


  • My main suggestion is not to use Where-Object and instead use Get-WmiObject's -Filter parameter.

    There's also no need for the repeated array concatenation ($ListOfMachines += Name). This should be equivalent:

    $ListOfMachines = $MyCollectionMembers | Select-Object -ExpandProperty $member.Name

    I also recommend not to use Export-Csv's -Append parameter, as it will become increasingly slower as the output file size increases.

    -- Bill Stewart [Bill_Stewart]

    Friday, December 1, 2017 8:50 PM