Tuesday, August 11, 2015

SCCM 2012:Powershell: Find the name of the collections in which the device is member or listed.

Here is a script to find collection names which a device is member of .
We will collect all collection names and then we will run the command get-cmdevice on each collection to check if the device is member of that collection. Collection name will be returned if the device is member of the collection.


#Get all collections you can enter collection name filters if needed ex: -name "*windows 7*"
$collections=Get-CMDeviceCollection | select Name
# Import the CSV which has the list of machines. Heading of CSV should be HostName
#or you can use a text file and use the command get-content
$devices=Import-Csv -Path $env:USERPROFILE\desktop\Device_List1.csv

foreach($device in $devices)
{
$hostname=$device.HostName
$break=$false
foreach($collection in $collections)
{
   $colName=$collection.Name
   $colObj=Get-CMDevice -CollectionName $colName -Name $hostname
   if($colObj -ne $null)
   {
    Write-Output "$hostname found in $colName"
    $break=$true;
   }
}
if(-not($break))
{
  Write-Host "$hostname not found in any collection"
}
}

No comments:

Post a Comment