Created
September 12, 2025 19:31
-
-
Save ahpooch/7990b1b6ed5b1e7d6213bde48dcb0eaa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-xCMObjectLocation { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory = $true)] | |
| [string] $InstanceKey, | |
| [Parameter(Mandatory = $false)] | |
| [string] $SiteCode = (Get-CMSite).SiteCode, | |
| [Parameter(Mandatory = $false)] | |
| [string] $SiteServer = ((Get-CMSiteSystemServer -SiteCode $SiteCode | Where-Object { $_.Type -eq "2" }) | Select-Object -ExpandProperty NetworkOSPath).TrimStart("\") | |
| ) | |
| # Powered by: | |
| # https://petervanderwoude.nl/post/get-the-folder-location-of-an-object-in-configmgr-2012-via-powershell/ | |
| # https://github.com/pvanderwoude/blog/blob/main/Get-ObjectLocation.ps1 | |
| $ContainerNode = Get-WmiObject -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServer -Query "SELECT ocn.* FROM SMS_ObjectContainerNode AS ocn JOIN SMS_ObjectContainerItem AS oci ON ocn.ContainerNodeID=oci.ContainerNodeID WHERE oci.InstanceKey='$InstanceKey'" | |
| if ($null -ne $ContainerNode) { | |
| $ObjectFolder = $ContainerNode.Name | |
| if ($ContainerNode.ParentContainerNodeID -eq 0) { | |
| $ParentFolder = $false | |
| } | |
| else { | |
| $ParentFolder = $true | |
| $ParentContainerNodeID = $ContainerNode.ParentContainerNodeID | |
| } | |
| while ($ParentFolder -eq $true) { | |
| $ParentContainerNode = Get-WmiObject -Namespace root/SMS/site_$($SiteCode) -ComputerName $SiteServer -Query "SELECT * FROM SMS_ObjectContainerNode WHERE ContainerNodeID = '$ParentContainerNodeID'" | |
| $ObjectFolder = $ParentContainerNode.Name + "\" + $ObjectFolder | |
| if ($ParentContainerNode.ParentContainerNodeID -eq 0) { | |
| $ParentFolder = $false | |
| } | |
| else { | |
| $ParentContainerNodeID = $ParentContainerNode.ParentContainerNodeID | |
| } | |
| } | |
| $ObjectFolder = "\" + $ObjectFolder | |
| return $ObjectFolder | |
| } | |
| else { | |
| $ObjectFolder = "\" | |
| return $ObjectFolder | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment