Get-Item or Test-Path leads to a redundant cascade of queries. Any way to avoid it?

Joe Emenaker 1 Reputation point
2021-10-02T01:29:57.797+00:00

I'm writing a script which aims to automatically create device collections and, if necessary, create the folders where they're supposed to be.

My issue comes with checking to see if those folders exist. I can use Get-Item, Get-ChildItem, or Test-Path, to query the SITECODE:\DeviceCollection folder, but, when I do, the command takes about a full minute or more. I used -Verbose to get an idea of what it's doing, and this is what I saw (sorry for using code formatting... it was the only way I could find to keep the stars and underscores from triggering typeface changes):

PS ABC:\> Test-Path ABC:\DeviceCollection\Delegated\Dept1\Labs -Verbose
VERBOSE: Getting FolderType for path REDACTED\DeviceCollection\Delegated\Dept1\Labs.
VERBOSE: Start: Execution of WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ... ParentContainerNodeId = 0 ...AND Name = 'Delegated'
VERBOSE: Start: Execution of WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ... ParentContainerNodeId = 16777351 ...AND Name = 'Dept1'
VERBOSE: Start: Execution of WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ... ParentContainerNodeId = 16777352 ...AND Name = 'Labs'
VERBOSE: Item exists - Path REDACTED\DeviceCollection\Delegated\Dept1\Labs is a valid path.

VERBOSE: Getting FolderType for path REDACTED\DeviceCollection\Delegated\Dept1.
VERBOSE: Start: Execution of WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ... ParentContainerNodeId = 0 ...AND Name = 'Delegated'
VERBOSE: Start: Execution of WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ... ParentContainerNodeId = 16777351 ...AND Name = 'Dept1'
VERBOSE: Item exists - Path REDACTED\DeviceCollection\Delegated\Dept1 is a valid path.

VERBOSE: Getting FolderType for path REDACTED\DeviceCollection\Delegated.
VERBOSE: Start: Execution of WQL query: SELECT * FROM SMS_ObjectContainerNode WHERE ... ParentContainerNodeId = 0 ...AND Name = 'Delegated'
VERBOSE: Item exists - Path REDACTED\DeviceCollection\Delegated is a valid path.
VERBOSE: Getting FolderType for path REDACTED\DeviceCollection.
VERBOSE: FolderType is Category for path REDACTED\DeviceCollection.

So, notice that it performs a query to find the top-level folder (to get that NodeId), and then does a query to find a child with the appropriate name with that NodeId as its parent, etc. Then, it performs the same queries for one folder level higher (even though it just performed those two queries in the first set of three queries). This isn't the worst part, though. Ignoring, for a moment, that this causes the number of queries to go up as the square of the number of levels deep of the folder you're trying to test for, Test-Path does this exact same set of six queries seven more times! That's right... I've only posted 1/8 of the output, as the rest is just exact duplicates of what you see here.

Does anybody know of a way to disable/suspend this redundant behavior or a different command I can use to test for CM folder existence?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-10-02T14:35:14.563+00:00

    What about Get-CMDeviceCollection?


  2. Rich Matheisen 44,776 Reputation points
    2021-10-02T18:21:36.68+00:00

    I don't know SharePoint, so I'm not qualified to say more than I found a few things that look like they'd help. But adding a SharePoint tag to your original question might attract more knowledgeable people!

    powershell-configurations-manager-2012-list-collections-inside-a-folder
    list-all-collections-under-a-specific-folder-with-powershell