Exclude Uninstall Collections from csv

#

# Press 'F5' to run this script. Running this script will load the ConfigurationManager

# module for Windows PowerShell and will connect to the site.

#

# This script was auto-generated at '2021-02-19 16:27:13'.

# Uncomment the line below if running in an environment where script signing is

# required.

#Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

# Site configuration

# Site code

$SiteCode = "%SiteCode%"

# SMS Provider machine name

$ProviderMachineName = "%CM_ServerFQDN"

# 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 to create exclution rules on SCCM Collections to prevent install / uninstall loops on collections with required install

$csv = Import-Csv -Path "%Path%\UserCollections.csv"

$csv2 = Import-Csv -Path "%Path%\@Labb\DeviceCollections.csv"

ForEach ($item In $csv)

{

$create_UserExclusion = Add-CMUserCollectionExcludeMembershipRule -CollectionName $item.Install -ExcludeCollectionName $item.Uninstall

Write-Host -ForegroundColor Green "Exclusion $($item.Uninstall) created!"

}

ForEach ($item In $csv2)

{

$create_DeviceExclusion = Add-CMDeviceCollectionExcludeMembershipRule -CollectionName $item.Install -ExcludeCollectionName $item.Uninstall

Write-Host -ForegroundColor Green "Exclusion $($item.Uninstall) created!"

}

DeviceCollections.csv

Install,Uninstall


UserCollections.csv

Install,Uninstall