Hi Lokesh,
Keith Garner has written an WMI function to add machines to a collection in SCCM.
Function Add-ResourceToCollection {
[CmdLetBinding()]
Param(
[string] $SiteCode ,
[string] $SiteServer,
[string] $CollectionName,
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
$System
)
begin {
$WmiArgs = @{ NameSpace = "Root\SMS\Site_$SiteCode"; ComputerName = $SiteServer }
$CollectionQuery = Get-WmiObject @WMIArgs -Class SMS_Collection -Filter "Name = '$CollectionName' and CollectionType='2'"
$InParams = $CollectionQuery.PSBase.GetMethodParameters('AddMembershipRules')
$Cls = [WMIClass]"Root\SMS\Site_$($SiteCode):SMS_CollectionRuleDirect"
$Rules = @()
}
process {
foreach ( $sys in $System ) {
$NewRule = $cls.CreateInstance()
$NewRule.ResourceClassName = "SMS_R_System"
$NewRule.ResourceID = $sys.ResourceID
$NewRule.Rulename = $sys.Name
$Rules += $NewRule.psobject.BaseObject
}
}
end {
$InParams.CollectionRules += $Rules.psobject.BaseOBject
$CollectionQuery.PSBase.InvokeMethod('AddMembershipRules',$InParams,$null) | Out-null
$CollectionQuery.RequestRefresh() | out-null
}
}
https://keithga.wordpress.com/2018/01/25/a-replacement-for-sccm-add-cmdevicecollectiondirectmembershiprule-powershell-cmdlet/
Please try to load the function before use the command below:
1. Use "get-cmdevice" cmdlet to get device:
get-cmdevice -name test* -fast | Add-ResourceToCollection -SiteCode "Site_APF" -SiteServer "APFLABSCCM01" -CollectionName "YourColloectionName"
2. Try to save the device infor in the csv file,then import it.
csv file example:
"ResourceID","Name"
"16777253","DESKTOP-2RTC761"
"16777254","DESKTOP-2RTC762"
"16777255","DESKTOP-2RTC763"
Import-Csv -Path c:\test.csv | Add-ResourceToCollection -SiteCode "Site_APF" -SiteServer "APFLABSCCM01" -CollectionName "YourColloectionName"
Best regards,
Lee
Just do it.