diff options
Diffstat (limited to 'T2R.ps1')
| -rw-r--r-- | T2R.ps1 | 156 |
1 files changed, 117 insertions, 39 deletions
@@ -1,10 +1,17 @@ -$tenantId = "00000000-0000-0000-0000-000000000000" -$clientID = "00000000-0000-0000-0000-000000000000" -$clientSecret = (ConvertTo-SecureString "CLIENT_SECRET_VALUE" -AsPlainText -Force ) +param( + [Parameter(Mandatory = $true)] + [string[]]$Team, + [switch]$Exact +) + +$config = Get-Content -Path ./config.json -Raw | ConvertFrom-Json +$tenantId = $config.tenantId +$clientID = $config.clientID +$clientSecret = (ConvertTo-SecureString $config.clientSecret -AsPlainText -Force ) $Scope = "https://graph.microsoft.com/.default" $authToken = Get-MsalToken -ClientId $clientID -ClientSecret $clientSecret -TenantId $tenantId -Scopes $Scope -$TeamsTeam = Read-Host "Enter team name" +Write-Host "Processing $($Team.Count) teams: $($Team -join ', ')" $Headers = @{ "Authorization" = "Bearer $($authToken.AccessToken)" @@ -12,48 +19,119 @@ $Headers = @{ } $allTeams = @() -$aadTeams = (Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/teams/" -Headers $Headers -Method Get -ContentType "application/json") +$teamsResponse = Invoke-WebRequest -Uri "https://graph.microsoft.com/v1.0/teams/" -Headers $Headers -Method GET +$aadTeams = $teamsResponse.Content | ConvertFrom-Json $allTeams += $aadTeams.value if ($aadTeams.'@odata.nextLink') { do { - $aadTeams = (Invoke-RestMethod -Uri $aadTeams.'@odata.nextLink' -Headers $Headers -Method Get -ContentType "application/json") + $teamsResponse = Invoke-WebRequest -Uri $aadTeams.'@odata.nextLink' -Headers $Headers -Method GET + $aadTeams = $teamsResponse.Content | ConvertFrom-Json $allTeams += $aadTeams.value } until (!$aadTeams.'@odata.nextLink') } $aadTeams = $allTeams -$channels = [System.Collections.ArrayList]::New() - -foreach ($teams in $aadTeams) { - if ($teams.displayName -like "$($TeamsTeam)*") { - $apiUri = "https://graph.microsoft.com/v1.0/teams/$($teams.id)/channels" - $Team = Invoke-RestMethod -Headers $Headers -Uri $apiUri -Method GET - $channelList = $Team.Value - foreach ($channel in $channelList) { - $channelFolder = Invoke-RestMethod -Headers $Headers -Uri "https://graph.microsoft.com/v1.0/teams/$($teams.id)/channels/$($channel.id)/filesFolder" -Method GET - $DriveID = $channelFolder.parentReference.driveId - $displayName = $channelFolder.name -replace " ", "_" -replace ",", "" - $Result = [PSCustomObject]@{ - displayName = $displayName - driveID = $DriveID - membershipType = $channel.membershipType - teamName = $teams.displayName +foreach ($requestedTeam in $Team) { + Write-Host "`nProcessing team: $requestedTeam" + + $channels = [System.Collections.ArrayList]::New() + $foundTeam = $false + + foreach ($teams in $aadTeams) { + if ($foundTeam) { break } + + $teamMatches = if ($Exact) { + $teams.displayName -eq $requestedTeam + } else { + $teams.displayName -like "$($requestedTeam)*" + } + + if ($teamMatches) { + Write-Host "Found matching team: $($teams.displayName)" + $apiUri = "https://graph.microsoft.com/v1.0/teams/$($teams.id)/channels" + + $response = Invoke-WebRequest -Headers $Headers -Uri $apiUri -Method GET + + try { + $teamResponse = $response.Content | ConvertFrom-Json -ErrorAction Stop + } catch { + $teamResponse = [System.Web.HttpUtility]::JavaScriptStringDeserializer.Deserialize($response.Content, [System.Object]) + } + + $channelList = $null + + if ($teamResponse.value) { + $channelList = $teamResponse.value + } elseif ($teamResponse.PSObject.Properties['value']) { + $channelList = $teamResponse.PSObject.Properties['value'].Value + } else { + $rawContent = $response.Content + if ($rawContent -match '"value":\s*\[(.*?)\]') { + $jsonArray = "[$($matches[1])]" + $channelList = $jsonArray | ConvertFrom-Json + } + } + + if ($channelList) { + Write-Host "Processing $($channelList.Count) channels for team: $($teams.displayName)" + foreach ($channel in $channelList) { + Start-Sleep -Milliseconds 250 + $folderResponse = Invoke-WebRequest -Headers $Headers -Uri "https://graph.microsoft.com/v1.0/teams/$($teams.id)/channels/$($channel.id)/filesFolder" -Method GET + $channelFolder = $folderResponse.Content | ConvertFrom-Json + $DriveID = $channelFolder.parentReference.driveId + $displayName = $channelFolder.name -replace " ", "_" -replace ",", "" + $Result = [PSCustomObject]@{ + displayName = $displayName + driveID = $DriveID + membershipType = $channel.membershipType + teamName = $teams.displayName + } + [void]$channels.Add($Result) + Write-Host "Added channel: $displayName with DriveID: $DriveID" + } + } else { + Write-Host "WARNING: No channels found for team: $($teams.displayName)" } - [void]$channels.Add($Result) + $foundTeam = $true + } + } + + if ($channels.Count -eq 0) { + Write-Host "ERROR: No channels were processed for team: $requestedTeam" + continue + } + + Write-Host "Total channels processed for $requestedTeam`: $($channels.Count)" + + $uniqueDrives = $channels | Sort-Object -Property driveID -Unique | Select-Object displayName, driveID, membershipType, teamName + Write-Host "Unique drives found for $requestedTeam`: $($uniqueDrives.Count)" + + $uniqueDrives | ForEach-Object { + if ($_.membershipType -eq 'standard') { + $_.displayName = $_.teamName -replace " ", "_" } - } -} - -$uniqueDrives = $channels | Sort-Object -Property driveID -Unique | Select-Object displayName, driveID, membershipType, teamName -$uniqueDrives | ForEach-Object { if ($_.membershipType -eq 'standard') { $_.displayName = $_.teamName -replace " ", "_" } } -foreach ($drive in $uniqueDrives) { - $config = Get-Content -Path ./remote-template.conf -Raw - $remote = $config -f $drive.displayName, $drive.driveID - Add-Content -Value $remote -Path ./rclone.conf -} - -$teamNames = $($uniqueDrives.displayName -join [Environment]::NewLine) -replace "\n", ": " -$teamNames = $teamNames + ":" -$union = Get-Content -Path ./union-template.conf -Raw -$unionConf = $union -f "$($channels[0].teamName)_union", $teamNames -Add-Content -Value $unionConf -Path ./rclone.conf
\ No newline at end of file + } + + $UnionName = "$($channels[0].teamName)_union" -replace " ", "_" + $RcloneFileName = $UnionName -replace "_union", "" + + foreach ($drive in $uniqueDrives) { + $config = Get-Content -Path ./remote-template.conf -Raw + $remote = $config -f $drive.displayName, $drive.driveID + Add-Content -Value $remote -Path "./$RcloneFileName.conf" + Write-Host "Added remote config for: $($drive.displayName)" + } + + if ($uniqueDrives.Count -ge 2) { + $teamNames = $($uniqueDrives.displayName -join [Environment]::NewLine) -replace "\n", ": " + $teamNames = "$teamNames" + ":" + $union = Get-Content -Path ./union-template.conf -Raw + $unionConf = $union -f $UnionName, $teamNames + Add-Content -Value $unionConf -Path "./$RcloneFileName.conf" + Write-Host "Generated union config: $UnionName" + } else { + Write-Host "Skipping union creation - only $($uniqueDrives.Count) remote(s) found (union requires 2+)" + } + + Write-Host "Generated config file: $RcloneFileName.conf" +}
\ No newline at end of file |
