Skip to content

Instantly share code, notes, and snippets.

@jworkmanjc
Created December 29, 2021 16:12
Show Gist options
  • Save jworkmanjc/ef3f764fd012312df5b0bf3cbb271703 to your computer and use it in GitHub Desktop.
Save jworkmanjc/ef3f764fd012312df5b0bf3cbb271703 to your computer and use it in GitHub Desktop.
Remove inactive systems from system groups
# Get Devices from Group
$groupID = '61842933b54406680ae461aa'
$groupMembers = Get-JcSdkSystemGroupMember -GroupId $groupID
# Inactivity Threshold in days
$inactivityPeriod = 30
$currentTime = [Xml.XmlConvert]::ToString((get-date), [Xml.XmlDateTimeSerializationMode]::Utc)
# If system is inactive greater than the inactivity period, remove from group
foreach ($system in $groupMembers) {
$targetSystem = get-jcsdksystem -Id $system.ToId
$TargetRFCTime = [Xml.XmlConvert]::ToString($($targetSystem.LastContact), [Xml.XmlDateTimeSerializationMode]::Utc)
$timespan = New-TimeSpan -Start $TargetRFCTime -End $currentTime
$days = [math]::Round($($timespan.totaldays), 2)
if ($days -gt $inactivityPeriod) {
Write-Host "$($targetSystem.displayName) last check in was $days days ago"
Set-JcSdkSystemGroupMember -Id $targetSystem.Id -GroupId $groupID -Op 'remove'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment