<# ==============================================================================================
NAME: Export-SCCMSoftwareCollections.ps1
COMMENT: Export SCCM Windows 10 Clients Software Collections
============================================================================================== #>
# requires -version x
# change window title
$host.ui.RawUI.WindowTitle = "Export-SCCMSoftwareCollections.ps1"
## load CM CommandLets (Standard by Microsoft)
# Site configuration
$SiteCode = "001" # Site code
$ProviderMachineName = "SCCM.XXXXX.YY" # SMS Provider machine name
# Customizations
$initParams = @{}
#$initParams.Add("Verbose", $true) # Uncomment this line to enable verbose logging
#$initParams.Add("ErrorAction", "Stop") # Uncomment this line to stop the script on any errors
# Do not change anything below this line
# Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams
}
# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
# Set the current location to be the site code.
Set-Location "$($SiteCode):\" @initParams
## Script part
$DeviceNames = (Get-CMCollectionMember -CollectionName "Windows 10 Clients").Name
foreach ($DeviceName in $DeviceNames) {
$Applications = $Null
$Packages = $Null
# get Collections of device with direct rule
$Collections = (Get-WmiObject -Namespace root/SMS/site_"$SiteCode" -Class sms_fullcollectionmembership -Filter "Name = '$DeviceName' and isDirect = '$true'")
# get Software collections (application or package)
$SoftwareCollections = $Collections | foreach {
# get Name of collection
$CollectionName = (Get-CMDeviceCollection -Id $_.CollectionID).Name
# get deployments from collection if application=1 or package=2 / shrink to unique because some collection include multiple deployments
Get-CMDeployment -CollectionName $CollectionName | where {$_.FeatureType -eq 1 -or $_.FeatureType -eq 2} | select ApplicationName,CollectionName,CollectionID,FeatureType
}
# help table to convert deployment config type
$ApplicationDeploymentHashtable = @{1="Install";2="Uninstall"}
# get all applications from software collections
$Applications = $SoftwareCollections | foreach {
if ($_.FeatureType -eq 1) {
# set var for use in comandlet
$CollectionID = $_.CollectionID
$CollectionName = $_.CollectionName
Get-CMApplication -Name $_.ApplicationName | select @{Name="Collection";Expression={$CollectionName}},@{Name="Name";Expression={$_.LocalizedDisplayName}},@{Name="Version";Expression={$_.SoftwareVersion}},Manufacturer,@{Name="CollectionID";Expression={$CollectionID}}
}
}
foreach ($Application in $Applications) {
# set var for use in comandlet
$CollectionID = $Application.CollectionID
$ApplicationDeployment = Get-CMApplicationDeployment -Name $Application.Name | where {$_.TargetCollectionID -eq $CollectionID}
$Application | Add-Member -MemberType NoteProperty -Name "Action" -Value ($ApplicationDeploymentHashtable[$ApplicationDeployment.DesiredConfigType]) -Force
}
# get all packages from software collections
$Packages = $SoftwareCollections | foreach {
if ($_.FeatureType -eq 2) {
$_ | select @{Name="Collection";Expression={$_.CollectionName}},@{Name="Name";Expression={$_.ApplicationName}},@{Name="CollectionID";Expression={$_.CollectionID}},@{Name="Action";Expression={"Package"}}
}
}
$Software = $Applications + $Packages
foreach ($S in $Software) { $S | Add-Member -MemberType NoteProperty -Name "Computer" -Value $DeviceName }
$AllSoftware += $Software
}