question

JoeEmenaker-7909 avatar image
0 Votes"
JoeEmenaker-7909 asked RichMatheisen-8856 commented

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered JoeEmenaker-7909 commented

What about Get-CMDeviceCollection?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Get-CMDeviceCollection does return all device collections (and the returned objects might include the path to them… haven’t checked), but that doesn’t help for empty folders.

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 commented

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


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 - I don't think Sharepoint is involved with this at all. This is just running Powershell commands (which seem to launch WQL queries) against SCCM.
2 - I have no trouble getting lists of device collections. What I need to know is if a folder exists for me to move a device collection into. Even if it were possible to get each collection's path, that doesn't tell me about folders which, currently, contain no collections.


0 Votes 0 ·

Well, the thing is that it probably does involve SharePoint. The product teams are usually the ones that write the code for "providers". PowerShell comes with a this set of built-in providers:

  • Registry

  • Alias

  • Environment

  • FileSystem

  • Function

  • Variable

  • Certificate

  • WSMan

I don't have anything installed on my machine to do with SharePoint. See if there's additional providers installed on yours by running the Get-PSProvider cmdlet.

0 Votes 0 ·