DeviceGroups
Assign devices from a query to a static group​
- Shell
- PowerShell (native)
- PowerShell (PSc8y)
c8y inventory find --query "agentVersion eq '1.*' and not(bygroupid(123456))" --includeAll |
c8y devicegroups assignDevice --group 123456 --workers 2 --progress
c8y inventory find --query "agentVersion eq '1.*' and not(bygroupid(123456))" --includeAll |
c8y devicegroups assignDevice --group 123456 --workers 2 --progress
Find-ManagedObjectCollection -Query "agentVersion eq '1.*' and not(bygroupid(123456))" -IncludeAll |
Add-DeviceToGroup -Group 123456 -Workers 2 -Progress
Assign devices from a file to a group​
Assuming that you get handed a list just containing a list of unique names or external ids called list.txt
:
UNIQUE_ID_1
UNIQUE_ID_2
UNIQUE_ID_3
UNIQUE_ID_4
Lookup the c8y id by using a text search (this is case insensitive)
- Shell
- PowerShell (native)
- PowerShell (PSc8y)
cat list.txt |
c8y inventory findByText \
--fragmentType c8y_IsDevice \
--select id,name,c8y_Hardware.serialNumber \
-o csv > list.ids.csvcat list.txt |
c8y inventory findByText `
--fragmentType c8y_IsDevice `
--select id,name,c8y_Hardware.serialNumber `
-o csv > list.ids.csvGet-Content list.txt |
Find-ManagedObjectCollectionByText `
-FragmentType c8y_IsDevice `
-Select id,name,c8y_Hardware.serialNumber `
-Output csv > list.ids.csvOutput file: list.ids.csv1111,MyActualName1,UNIQUE_ID_1
2222,MyActualName2,UNIQUE_ID_2
3333,MyActualName3,UNIQUE_ID_3
4444,MyActualName4,UNIQUE_ID_4Check the file to verify that everything looks ok
- Shell
- PowerShell (native)
- PowerShell (PSc8y)
cat list.ids.csv | more
cat list.ids.csv | more
Get-Content list.ids.csv | more
Assign the devices to a single static group
- Shell
- PowerShell (native)
- PowerShell (PSc8y)
cat list.ids.csv |
c8y devicegroups assignDevice --group 1234 --workers 2 --progress --silentStatusCodes 409cat list.ids.csv |
c8y devicegroups assignDevice --group 1234 --workers 2 --progress --silentStatusCodes 409Get-Content list.ids.csv |
Add-DeviceToGroup -Group 1234 -Workers 2 -Progress -SilentStatusCodes 409The
silentStatusCodes
parameter is used to silence 409 (duplicate) errors, as we don't care if the device is already assigned to the group.noteIf the piped input is in a csv format, then go-c8y-cli will automatically extract the first field/column and use it to lookup the device. If the first field contains an id (i.e. just numbers), then the value will be used directly as an ID, however if it contains non ID characters (i.e. any character does not match [0-9]) then it will be treated as a name, and an additional lookup by name will be performed in order to get the device id (this will take longer as it performs one request per name).
If store you data in csv format, it is best practice to store the id as the first field making the data more pipe friendly. However you can still select the ids using the linux command
cut
.# MyActualName1,1111,UNIQUE_ID_1
cat list.ids.csv | cut -d, -f2 | c8y ....Alternatively you can also store the data in a json line format (using
--output json
), then the column order does not matter as thego-c8y-cli
is smart enough to the pluck the id property if it is present.Output: Example JSON lines{"name": "MyActualName1", "id": "1111", "c8y_Hardware": {"serialNumber": "UNIQUE_ID_1"}}
{"name": "MyActualName2", "id": "2222", "c8y_Hardware": {"serialNumber": "UNIQUE_ID_2"}}
{"name": "MyActualName3", "id": "3333", "c8y_Hardware": {"serialNumber": "UNIQUE_ID_3"}}
{"name": "MyActualName4", "id": "4444", "c8y_Hardware": {"serialNumber": "UNIQUE_ID_4"}}
Assign devices where the operation was set to FAILED in a bulk operation​
- Shell
- PowerShell (native)
- PowerShell (PSc8y)
# Create a new group
group=$( c8y devicegroups create --name "my_custom_group" --output csv --select id )
# Assign the failed operations
c8y operations list --bulkOperationId 8 --status FAILED --includeAll |
c8y devicegroups assignDevice --group $group --workers 2 --progress --silentStatusCodes 409
# Create a new group
group=$( c8y devicegroups create --name "my_custom_group" --output csv --select id )
# Assign the failed operations
c8y operations list --bulkOperationId 8 --status FAILED --includeAll |
c8y devicegroups assignDevice --group $group --workers 2 --progress --silentStatusCodes 409
# Create a new group
$group = New-DeviceGroup -Name "my_custom_group" -Output csv -Select id
# Assign the failed operations
Get-OperationCollection -BulkOperationId 8 -Status FAILED -IncludeAll |
Add-DeviceToGroup -Group $group -Workers 2 -Progress -SilentStatusCodes 409
Unassign devices from a group which match a custom inventory query​
The Cumulocity inventory query language supports a the bygroupid(x)
operator which checks if a device belongs to a group. This allows a custom query to be run without needing to first get a list of devices that are already assigned to the group.
- Shell
- PowerShell (native)
- PowerShell (PSc8y)
c8y inventory find --query "agentVersion eq '1.*' and bygroupid(123456)" --includeAll |
c8y devicegroups unassignDevice --group 123456 --workers 2 --progress
c8y inventory find --query "agentVersion eq '1.*' and bygroupid(123456)" --includeAll |
c8y devicegroups unassignDevice --group 123456 --workers 2 --progress
Find-ManagedObjectCollection -Query "agentVersion eq '1.*' and bygroupid(123456)" -IncludeAll |
Remove-DeviceFromGroup -Group 123456 -Workers 2 -Progress
elapsed 01:34 (started: 2021-04-13T11:27:36+02:00) â ¸ total requests sent: 399
worker 1: â ¸ avg: 2.149 request/s
worker 2: â ¸ avg: 2.099 request/s