Created
December 29, 2021 16:12
-
-
Save jworkmanjc/ef3f764fd012312df5b0bf3cbb271703 to your computer and use it in GitHub Desktop.
Remove inactive systems from system groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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