[download id=”35″]

My Collegue Michael Petersen asked me for at  script that would list the package structure of the packages in ConfigMgr 2007.

Sometimes we end up with a PackageID from a log file or report, and if there is a lot of packages/folders. Finding the correct package, can be quite difficult.

I decided to make this script without the ConfigMgr Community Cmdlets, since it is merely quite regular WMI queries.

Therefore it is supported in all versions of PowerShell, both 1.0 and 2.0.

I am sure that it can be done prettier than this, but I did not want to spend all day writing it. It works, but please do not hesitate to post suggestions/other solutions.

This overview can be made in SQL Reporting Services too, but it is not everyone who is running reporting services :).

# //***************************************************************************
# // ***** Script Header *****
# //
# // Solution:
# // File:     ListPackageStructure.ps1
# // Author:	Jakob Gottlieb Svendsen, Coretech A/S. https://blog.ctglobalservices.com
# // Purpose:   List All Package Structure.
# //
# //
# // Usage:     ListPackageStructure.ps1
# //
# //
# // CORETECH A/S History:
# // 1.0.0     JGS 11/04/2011  Created initial version.
# //
# // Customer History:
# //
# // ***** End Header *****
# //***************************************************************************
# //----------------------------------------------------------------------------
#//
#//  Global constant and variable declarations
#/
#//----------------------------------------------------------------------------

$ServerName = Read-Host -Prompt "Type ConfigMgr 2007 ServerName:"
$SiteName = Read-Host -Prompt "Type ConfigMgr 2007 SiteCode:"

cls

#//----------------------------------------------------------------------------
#//  Procedures
#//----------------------------------------------------------------------------

function ListFolderPackages([string]$strPath,[string]$folderID,[switch]$rootFolder)
{
	if ($rootfolder)
	{
		$Subfolders = Get-WmiObject -ComputerName $ServerName -Namespace "root\sms\site_$SiteName" -Query "SELECT Name,ContainerNodeID,ParentContainerNodeID FROM SMS_ObjectContainerNode WHERE SearchFolder = 0 AND ObjectType = 2 AND ParentContainerNodeID = 0 ORDER BY ContainerNodeID"
	}
	else
	{
		$Subfolders = Get-WmiObject -ComputerName $ServerName -Namespace "root\sms\site_$SiteName" -Query "SELECT Name,ContainerNodeID,ParentContainerNodeID FROM SMS_ObjectContainerNode WHERE SearchFolder = 0 AND ObjectType = 2 AND ParentContainerNodeID = $folderID ORDER BY ContainerNodeID"
	}
	if ($Subfolders -ne $null)
	{
		foreach ($folder in $Subfolders)
		{
			$strNewPath = $strPath + $folder.Name
			$strNewPath

			$packagesInFolder = $locationInfo | where {$_.ContainerNodeID -eq $folder.ContainerNodeID}

			if ($packagesInFolder -ne $null)
			{
				foreach ($packageinFolder in $packagesInFolder)
				{
					$package = $packagesInfo | where { $_.PackageID -eq $packageinFolder.InstanceKey }
					"`t{0} - ({1})" -f $package.Name, $package.PackageID
				}
			}
			else
			{
				"Contains no packages"
			}
			#ListSubFolders
			ListFolderPackages -strPath "$strNewPath\" -folderID  $folder.ContainerNodeID
		}
	}
}

#//----------------------------------------------------------------------------
#//  Main routines
#//----------------------------------------------------------------------------

#get package info (name etc.) once to keep load on server as low as possible.
$packagesInfo = Get-WmiObject -ComputerName $ServerName -Namespace "root\sms\site_$SiteName" -class SMS_Package

#get package and folder location info once to keep server load as low as possible
$locationInfo = Get-WmiObject -ComputerName $ServerName -Namespace "root\sms\site_$SiteName" -class SMS_ObjectContainerItem

ListFolderPackages -strPath "\" -folderID 0 -rootFolder

#//----------------------------------------------------------------------------
#//  End Script
#//----------------------------------------------------------------------------