I do a lot of application packaging and deployment at work, so recently I’ve been experimenting with using PowerShell to speed up some of my testing procedures. To that end, I have some scripts that do the following:
- Create a collection in Configuration Manager
- Add a computer to that collection
- Create an application deployment targeted at the collection I created
I’ve tried to combine the three scripts into one. But in doing so, I now get an error that I can’t explain and can’t figure out how to fix. Code for the one script combining the three smaller scripts is below. The error occurs at the following line:
[pre]Add-CMDeviceCollectionDirectMembershipRule -CollectionName “$CollectionName” -ResourceID $(get-cmdevice -name “$DeviceName”).ResourceID[/pre]
The error message is:
Add-CMDeviceCollectionDirectMembershipRule : One or more errors occurred.
Add-CMDeviceCollectionDirectMembershipRule -CollectionName "$Collecti …
CategoryInfo : NotSpecified: ( [Add-CMDeviceCol…tMembershipRule], AggregateException
FullyQualifiedErrorId : System.AggregateException,Microsoft.ConfigurationManagement.Cmdlets.Collections.Commands.AddDeviceCollectionDirectM
embershipRuleCommand
Here’s the full script:
[pre]
change the drive to our Config Mgr site
$SiteCode = Get-PSDrive -PSProvider CMSITE
Set-Location "$($SiteCode.Name):"
user prompts
$DeviceName = Read-host(“Enter the device name”)
$CollectionName = Read-host(“Enter the collection name”)
$ApplicationName = Read-host(“Enter the application name”)
Create the collection
Write-Host “Creating collection…”
New-CMDeviceCollection -Name “$CollectionName” -LimitingCollectionName ‘All Workstations’
Add device to collection
Write-Host “Adding device to collection…”
Add-CMDeviceCollectionDirectMembershipRule -CollectionName “$CollectionName” -ResourceID $(get-cmdevice -name “$DeviceName”).ResourceID
Move the collection to the Development folder
Write-Host “Moving collection…”
$Collection1 = Get-CMDeviceCollection -Name “$CollectionName”
Move-CMObject -InputObject $Collection1 -FolderPath ‘SMS:\DeviceCollection\Engineering\Development’
Create the deployment.
Write-Host “Creating deployment…”
New-CMApplicationDeployment -CollectionName “$CollectionName” -name “$ApplicationName” `
-DeployAction Install -DeployPurpose Required -UserNotification DisplayAll -AvailableDateTime (get-date) `
-DeadlineDateTime (get-date) -TimeBaseOn LocalTime -SendWakeupPacket $true -Verbose
[/pre]