function p4maintain-clients {param([switch]$doCleanUp,[switch]$force) $taskConfig = $null $clientsToRemove = $null $taskConfig = p4get-maintenanceTasks if (!$taskConfig) { throw "No task configuration found for this task" } $now = get-date write-progress -id 5 -activity ("Perforce Clientspec maintenance script.") -status ("Retrieving clientspecs from server.") $tombstonedClients = p4get-client | ?{$_.access -le $now.adddays(-$taskConfig.clientSpecTombstoneDays)} | %{$_.client} $untouchableClients = (p4get-maintenanceTasks).immortalClientSpecs.client if ($tombstonedClients) { $clientsToRemove = compare-object $tombstonedClients $untouchableClients | %{$_.inputobject} | ?{$_ -ne ""} } if ($clientsToRemove) { if ($doCleanUp) { $i=0 foreach ($client in $clientsToRemove) { if ($client) { try { write-progress -id 5 -activity ("Removing clients older than " + $taskConfig.clientSpecTombstoneDays.tostring() + " days.") -status ("Removing client " + $client) -percentcomplete ($i/$clientsToRemove.count*100) $i++ if ($force) { p4remove-client $client -unshelve -revert } else { p4remove-client } } catch { if ($error[0].Exception.Message -match "because it has open files") { write-warning ("Clientspec " + $client + " has open files. Not reverting because -force was not specified.") } } } } } else { $clientsToRemove ColorConsole -FGColor yellow -message ($clientsToRemove.count.tostring() + " clients are older than " + $taskConfig.clientSpecTombstoneDays.tostring() + " days old.`r`nInvoke with -doCleanUp to remove clientspecs.`r`nAddionally, invoke -force to revert clients and delete shelves.") } } else { ColorConsole -FGColor yellow -message ("No clients with an access date older than " + $taskConfig.clientSpecTombstoneDays.tostring() + " were found to remove.") } }