$ErrorActionPreference = "Stop" # This sample script is provided as-is! I take no responsibility for the damage it might cause to your server! Please test this before using it in production! # Filling in the following four values and running this script should iterate through all public channels and delete messages older than $daysToClean days old. # The following values are used below $server = "http://server:3000" $user = "uername" $pass = "password" $daysToClean = 30 # Deletion will occur on all messages between these times: # From 10 years ago up until $daysToClean days ago. $endDate = (get-date).AddDays(-$daysToClean).tostring() $startDate = (get-date).AddYears(-10).tostring() try { $loginResult = invoke-restmethod -UseBasicParsing -uri $server/api/v1/login -method post -body "username=$user&password=$pass" if ($loginResult.status -ne "success") { write-output "Error authenticating to server!" exit } } catch { write-output "Error connecting to server $server - please be sure this contains a full url, including port (ex: http://server:3000)." exit } # Function that actually cleans the message history function cleanHistory($channelId, $authToken, $userId, $endDate, $startDate) { $body = @{ "roomId" = $channelId; "latest" = $endDate; "oldest" = $startDate } | ConvertTo-Json try { $result = invoke-restmethod -UseBasicParsing -uri $server/api/v1/channels.cleanHistory -method post -headers @{ "X-User-Id" = $userId; "X-Auth-Token" = $authToken } -ContentType "application/json" -body $body if (!$result.success) { write-output "There was an error processing $channelId - Unable to clean history!" } } catch { write-output "There was an error processing $channelId - the server gave a bad response, the channel may not exist or the server is having issues." } } $userId = $loginResult.data.userId $authToken = $loginResult.data.authToken # Pulling a list of all channels $channels = invoke-restmethod -UseBasicParsing -uri $server/api/v1/channels.list -headers @{ "X-User-Id" = $userId; "X-Auth-Token" = $authToken } # Loop through channels foreach ($channel in $channels.channels) { write-output "Clearing history for: $($channel.name)" cleanHistory $channel._id $authToken $userId $endDate $startDate }