2021-06-22

Export All Teams Channels Owners, Members and Guests with JSON format by PowerShell

Connect To Teams 的段落搭配
https://blog.dino9021.com/2020/11/connect-azaccount-with-saved-encryped.html

$ShowID = $True;

##### Get Self-Path #####
if (($MyInvocation.MyCommand).Path -ne $Null) {
$SelfPath = (($MyInvocation.MyCommand).Path -replace ($MyInvocation.MyCommand),' ');
} else {
$SelfPath = ((Get-Location).Path + '\');
};
##### Get Self-Path #####

#$webclient=New-Object System.Net.WebClient
#$webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
#[Net.ServicePointManager]::SecurityProtocol = "tls12"
#Install-Module MicrosoftTeams
##### Connect To Teams #####
$CredentailFileName = "AzCredential.txt";

[string[]]$CredentialRead = Get-Content -Path $CredentailFileName

$UserName = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($CredentialRead[0]))
$Password = $CredentialRead[1] | ConvertTo-SecureString -Key (1..16)

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password

Connect-MicrosoftTeams -Credential $Credential
##### Connect To Teams #####

Import-Module MicrosoftTeams

##### Get All Teams Info #####
$Results = @();
$AllTeams = Get-Team
Foreach ($AllTeam in $AllTeams) {
if ($AllTeam.GroupId -eq $Null) {
Continue;
};
$TeamChannels = Get-TeamChannel -GroupId $AllTeam.GroupId
$TeamChannelInfo = @();
Foreach ($TeamChannel in $TeamChannels) {
write-host ('Processing Team Channel: ' + $AllTeam.DisplayName + ' - ' + $TeamChannel.DisplayName);
$TeamChannelUsers = @();
Foreach ($User in (Get-TeamChannelUser -GroupId $AllTeam.GroupId -DisplayName $TeamChannel.DisplayName)) {
$TeamChannelUsers += New-Object -TypeName PSObject -Property @{
Name = $User.Name
User = $User.User
Role = $User.Role
UserId = $User.UserId
};
};
$TeamChannelInfo += New-Object -TypeName PSObject -Property @{
DisplayName = $TeamChannel.DisplayName
Description = $TeamChannel.Description
Id = $AllTeam.GroupId
Users = $TeamChannelUsers
};
};
$Results += New-Object -TypeName PSObject -Property @{
DisplayName = $AllTeam.DisplayName
Description = $AllTeam.Description
Visibility = $AllTeam.Visibility
Archived = $AllTeam.Archived
MailNickName = $AllTeam.MailNickName
GroupId = $AllTeam.GroupId
Channel = $TeamChannelInfo;
};
};

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False;
[System.IO.File]::WriteAllLines(($SelfPath + 'TeamsChannel_' + (Get-Date -Format 'yyyyMMdd') + '.json'), ($Results | ConvertTo-Json -Depth 10 -Compress), $Utf8NoBomEncoding);
##### Get All Teams Info #####

##### Write All Teams Channels Users Readable File #####
$Results = (Get-Content ($SelfPath + 'TeamsChannel_' + (Get-Date -Format 'yyyyMMdd') + '.json') -Encoding UTF8) | ConvertFrom-Json
$Output = '';
Foreach ($Result in $Results) {
if ($Result.GroupId -eq $Null) {
Continue;
};
$Output += ("Team`t");
$Output += ("DisplayName:`t" + $Result.DisplayName + "`n");
$Output += ("`t`t" + "Description:`t" + $Result.Description + "`n");
$Output += ("`t`t" + "Visibility:`t`t" + $Result.Visibility + "`n");
$Output += ("`t`t" + "Archived:`t`t" + $Result.Archived + "`n");
$Output += ("`t`t" + "MailNickName:`t" + $Result.MailNickName + "`n");
if ($ShowID -eq $True) {
$Output += ("`t`t" + "GroupId:`t`t" + $Result.GroupId + "`n");
};
$Output += "`n";
Foreach ($ChannelInfo in $Result.Channel) {
$Output += ("`t`tChannel`t");
$Output += ("DisplayName:`t" + $ChannelInfo.DisplayName + "`n");
$Output += ("`t`t`t`t" + "Description:`t" + $ChannelInfo.Description + "`n");
if ($ShowID -eq $True) {
$Output += ("`t`t`t`t" + "Id:`t`t`t`t" + $ChannelInfo.Id + "`n");
};
$Output += "`n";
Foreach ($User in $ChannelInfo.Users) {
$Output += ("`t`t`t`t" + $User.Role + ":`t");

if ($User.Role -eq 'Guest') {
$Output += ($User.Name + " (" + ($User.User -Split '@')[0] + ")`t");
} else {
$Output += ($User.Name + " (" + $User.User + ")`t");
$Output += ((Get-ADUser -Identity ($User.User -Split '@')[0] -Properties Department).Department);
};
$Output += "`n"

if ($ShowID -eq $True) {
$Output += ("`t`t`t`t`t`t" + "UserId:`t" + $User.UserId + "`n");
};
};
$Output += "`n`n";
};
};

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False;
[System.IO.File]::WriteAllLines(($SelfPath + 'TeamsChannel_Readable_' + (Get-Date -Format 'yyyyMMdd') + '.txt'), $Output, $Utf8NoBomEncoding);
##### Write All Teams Channels Users Readable File #####

##### Write All Teams Channels Users Tab Separate File #####

$Results = (Get-Content ('TeamsChannel_' + (Get-Date -Format 'yyyyMMdd') + '.json') -Encoding UTF8) | ConvertFrom-Json

$Output = '';

$Output += "Team DisplayName`tTeam Description`tTeam Visibility`tTeam Archived`tTeam MailNickName`t";
if ($ShowID -eq $True) {
$Output += "Team GroupId`t";
};

$Output += "Channel DisplayName`tChannel Description`t";
if ($ShowID -eq $True) {
$Output += "Channel Id`t";
};

$Output += "User Role`tUser Name`tUser Account`tUser BU`tUser Section`tUser Department";
if ($ShowID -eq $True) {
$Output += "`tMember UserId";
};

$Output += "`n";

Foreach ($Result in $Results) {
if ($Result.GroupId -eq $Null) {
Continue;
};
Foreach ($ChannelInfo in $Result.Channel) {
$ChannelUserExist = $False;
Foreach ($User in $ChannelInfo.Users) {
$ChannelUserExist = $True;

$Output += ($Result.DisplayName + "`t");
$Output += (($Result.Description -replace "`n","#") + "`t");
$Output += ($Result.Visibility + "`t");
if (($Result.Archived -eq $False) -or ($Result.Archived -eq 'false') -or ($Result.Archived -eq 0) ) {
$Output += ("Not Archived`t");
} else {
$Output += ("Archived`t");
};
$Output += ($Result.MailNickName + "`t");
if ($ShowID -eq $True) {
$Output += ($Result.GroupId + "`t");
};

$Output += ($ChannelInfo.DisplayName + "`t");
$Output += (($ChannelInfo.Description -replace "`n","#") + "`t");
if ($ShowID -eq $True) {
$Output += ($ChannelInfo.Id + "`t");
};

$Output += ($User.Role + "`t");
$Output += ($User.Name + "`t");
if ($User.Role -eq 'Guest') {
$Output += (($User.User -Split '@')[0] + "`t");
$Output += ("Guest`t");
$Output += ("Guest`t");
$Output += ("Guest");
} else {
$Output += ($User.User + "`t");
$UserDepartment = (((Get-ADUser -Identity ($User.User -Split '@')[0] -Properties Department).Department) -Split ' - ');
if ($UserDepartment.Count -eq 3) {
$Output += ($UserDepartment[0] + "`t");
$Output += ($UserDepartment[1] + "`t");
$Output += ($UserDepartment[2]);
} elseif ($UserDepartment.Count -eq 2) {
$Output += ($UserDepartment[0] + "`t");
$Output += ($UserDepartment[0] + "`t");
$Output += ($UserDepartment[1]);
} elseif ($UserDepartment.Count -eq 1) {
$Output += ($UserDepartment[0] + "`t");
$Output += ($UserDepartment[0] + "`t");
$Output += ($UserDepartment[0]);
};
};
if ($ShowID -eq $True) {
$Output += ("`t" + $User.UserId);
};
$Output += "`n";
};

if ($ChannelUserExist -eq $False) {

$Output += ($Result.DisplayName + "`t");
$Output += (($Result.Description -replace "`n","#") + "`t");
$Output += ($Result.Visibility + "`t");
if (($Result.Archived -eq $False) -or ($Result.Archived -eq 'false') -or ($Result.Archived -eq 0) ) {
$Output += ("Not Archived`t");
} else {
$Output += ("Archived`t");
};
$Output += ($Result.MailNickName + "`t");
if ($ShowID -eq $True) {
$Output += ($Result.GroupId + "`t");
};

$Output += ($ChannelInfo.DisplayName + "`t");
$Output += (($ChannelInfo.Description -replace "`n","#") + "`t");
if ($ShowID -eq $True) {
$Output += ($ChannelInfo.Id + "`t");
};

$Output += ("No User`t");
$Output += ("No User`t");
$Output += ("No User`t");
$Output += ("No User`t");
$Output += ("No User");
if ($ShowID -eq $True) {
$Output += ("`tNo User");
};
$Output += "`n";
};
};
};

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False;
[System.IO.File]::WriteAllLines(($SelfPath + 'TeamsChannel_TabSeparate_' + (Get-Date -Format 'yyyyMMdd') + '.txt'), $Output, $Utf8NoBomEncoding);
##### Write All Teams Channels Users Tab Separate File #####

沒有留言:

張貼留言