MyPSFunctions.Teams.ps1

<#
    ===========================================================================
     Created with: SAPIEN Technologies, Inc., PowerShell Studio 2021 v5.8.196
     Created on: 10/26/2023 7:16 PM
     Created by: John@MyPSFunctions.com
     Organization: MyPSFunctions
     Filename: MyPSFunctions.Teams.psm1
    -------------------------------------------------------------------------
     Module Name: MyPSFunctions.Teams
    ===========================================================================
#>


Function Enable-Office365GroupForTeams
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true,
                   Position = 1)]
        [String]$EmailAddress
    )
    
    Try
    {
        $Office365Group = Get-UnifiedGroup -Identity $EmailAddress
        $Office365GroupExternalID = $Office365Group.ExternalDirectoryObjectId
        $Office365GroupDisplayName = $Office365Group.DisplayName
        Try
        {
            New-Team -GroupId $Office365GroupExternalID
            Write-Log -Level Info -Message "Office 365 Group $Office365GroupDisplayName ($EmailAddress) is enabled for Teams"    
            
        }
        Catch
        {
            $ErrorMessage = $Error[0].Exception.Message
            Write-Log -Level Error -Message "Failed to enable the Office 365 Group $Office365GroupDisplayName ($EmailAddress) for Teams with Error:$ErrorMessage"
        }
    }
    Catch
    {
        $ErrorMessage = $Error[0].Exception.Message
        $CMDLet = $Error[0].InvocationInfo.Line
        $FailedItem = $Error[0].Exception.ItemName
        Write-Log -Level Error -Message "Failed to Find the Office 365 Group with error: $ErrorMessage"
    }
    
    
    
}

Function Migrate-SkypeToTeamsMigrationBatch
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true,
                   Position = 1)]
        [string]$TxtInputFile
    )
    $users_ids = Get-Content $TxtInputFile
    $DateFull = Get-Date -Format "ddMMyyyy_HH-mm-ss"
    $OperationName = "MigrationBatch_" + $DateFull
    
    New-CsBatchPolicyAssignmentOperation -PolicyType TeamsUpgradePolicy -PolicyName UpgradeToTeams -Identity $users_ids -OperationName $OperationName
    sleep 5
    Get-CsBatchPolicyAssignmentOperation | where { $_.OperationName -eq $OperationName }
    
}

Function Check-SkypeToTeamsMigrationBatchStatus
{
    param
    (
        [Parameter(Mandatory = $true,
                   Position = 1)]
        [String]$MigrationBatchName
    )
    
    $MigrationBatches = Get-CsBatchPolicyAssignmentOperation | where { $_.OperationName -eq $OperationName }
    $MigrationBatchID = $MigrationBatches[0].OperationID
    $MigratedUsers = Get-CsBatchPolicyAssignmentOperation -Identity $MigrationBatchID | select -ExpandProperty UserState
    $MigratedUsers | group state | ft Count, Name
    $Response = Read-Host "Do you want to display the full List of Users? Press <Y>"
    If ($Response -eq "Y")
    {
        $MigratedUsers | ft
    }
}

Function Clear-TeamsCacheFolder
{
    [CmdletBinding()]
    param ()
    
    #Source:
    # From <https://commsverse.blog/2018/09/28/clear-the-microsoft-teams-client-cache/>
    # https://answers.microsoft.com/en-us/msoffice/forum/msoffice_other-mso_win10-mso_o365b/microsoft-teams-wont-open/24fcab1e-7c75-4f3e-9fd6-42c1ccb6004d#:~:text=the%20following%20steps.-, 1., and%20fully%20kill%20the%20process.&text=Once%20finally%20done%20clearing%2C%20you, check%20if%20the%20issue%20disappears.
    # Kill Teams Process
    $TeamsProcesses = get-process | where { $_.ProcessName -like "*Teams*" }
    $TeamsProcesses | foreach { Stop-Process $_.Id -ErrorAction SilentlyContinue | Out-Null }
    # Go to %appdata%\Microsoft\teams
    $TeamsAppData = $env:APPDATA + "\Microsoft\teams\"
    
    # Clear Folder: %appdata%\Microsoft\teams\application cache\cache"
    $TeamsAppDataAppCache = $TeamsAppData + "application cache\cache"
    Get-ChildItem -Path $TeamsAppDataAppCache -Recurse -ErrorAction SilentlyContinue | Remove-Item -Confirm:$false -Recurse | Out-Null
    
    # Clear Folder: %appdata%\Microsoft\teams\blob_storage
    $TeamsAppDataBlob_storage = $TeamsAppData + "blob_storage"
    Get-ChildItem -Path $TeamsAppDataBlob_storage -Recurse -ErrorAction SilentlyContinue | Remove-Item -Confirm:$false -Recurse | Out-Null
    
    # Clear Folder: %appdata%\Microsoft\teams\Cache
    $TeamsAppDataCache = $TeamsAppData + "Cache"
    Get-ChildItem -Path $TeamsAppDataCache -Recurse -ErrorAction SilentlyContinue | Remove-Item -Confirm:$false -Recurse | Out-Null
    # Clear Folder: %appdata%\Microsoft\teams\databases
    $TeamsAppDataDatabases = $TeamsAppData + "databases"
    Get-ChildItem -Path $TeamsAppDataDatabases -Recurse -ErrorAction SilentlyContinue | Remove-Item -Confirm:$false -Recurse | Out-Null
    # Clear Folder: %appdata%\Microsoft\teams\GPUcache
    $TeamsAppDataGPUcache = $TeamsAppData + "GPUcache"
    Get-ChildItem -Path $TeamsAppDataGPUcache -Recurse -ErrorAction SilentlyContinue | Remove-Item -Confirm:$false -Recurse | Out-Null
    # Clear Folder: %appdata%\Microsoft\teams\IndexedDB
    $TeamsAppDataIndexedDB = $TeamsAppData + "IndexedDB"
    Get-ChildItem -Path $TeamsAppDataIndexedDB -Recurse -ErrorAction SilentlyContinue | Remove-Item -Confirm:$false -Recurse | Out-Null
    # Clear Folder: %appdata%\Microsoft\teams\Local Storage
    $TeamsAppDataLocalStorage = $TeamsAppData + "Local Storage"
    Get-ChildItem -Path $TeamsAppDataLocalStorage -Recurse -ErrorAction SilentlyContinue | Remove-Item -Confirm:$false -Recurse | Out-Null
    # Clear Folder: %appdata%\Microsoft\teams\tmp
    $TeamsAppDataTmp = $TeamsAppData + "tmp"
    Get-ChildItem -Path $TeamsAppDataTmp -Recurse -ErrorAction SilentlyContinue | Remove-Item -Confirm:$false -Recurse | Out-Null
}

Function Get-Detailed_CSOnlineUsers_Report
{
    
    [Int]$i = 1
    $Table = $Null
    $Table = @()
    
    $CSOnlineUsers = Get-CsOnlineUser -ResultSize unlimited
    $Count = ($CSOnlineUsers | Measure).count
    ForEach ($CSOnlineUser in $CSOnlineUsers)
    {
        $CS_UserPrincipalName = $Null
        $CS_UserPrincipalName = $CSOnlineUser.UserPrincipalName
        $SIPAddress = $Null
        $SIPAddress = $CSOnlineUser.SIPAddress
        $SIPAddress = $SIPAddress.SubString(4)
        $DisplayName = $Null
        $DisplayName = $CSOnlineUser.DisplayName
        $LineURI = $Null
        $LineURI = $CSOnlineUser.LineURI
        $HostingProvider = $Null
        $HostingProvider = $CSOnlineUser.HostingProvider
        $TeamsUpgradeEffectiveMode = $Null
        $TeamsUpgradeEffectiveMode = $CSOnlineUser.TeamsUpgradeEffectiveMode
        Write-Log -Level Warning -message "The script is analyzing $DisplayName ($CS_UserPrincipalName)….. --- $i/$Count"
        $Recipient = Get-Recipient $CS_UserPrincipalName
        $UserPrincipalName = $Null
        $UserPrincipalName = $Recipient.WindowsLiveID
        $PrimarySmtpAddress = $Null
        $PrimarySmtpAddress = $Recipient.PrimarySmtpAddress
        $City = $Null
        $City = $Recipient.City
        $Department = $Null
        $Department = $Recipient.Department
        $FirstName = $Null
        $FirstName = $Recipient.FirstName
        $LastName = $Null
        $LastName = $Recipient.LastName
        $Office = $Null
        $Office = $Recipient.Office
        $Title = $Null
        $Title = $Recipient.Title
        $CustomAttribute5 = $Null
        $CustomAttribute5 = $Recipient.CustomAttribute5
        $CustomAttribute7 = $Null
        $CustomAttribute7 = $Recipient.CustomAttribute7
        $CustomAttribute13 = $Null
        $CustomAttribute13 = $Recipient.CustomAttribute13
        $CustomAttribute14 = $Null
        $CustomAttribute14 = $Recipient.CustomAttribute14
        $CustomAttribute15 = $Null
        $CustomAttribute15 = $Recipient.CustomAttribute15
        $Table += New-object PSobject -Property ([Ordered] @{
            FirstName = $FirstName;
            LastName = $LastName;
            DisplayName = $DisplayName;
            CS_UserPrincipalName = $CS_UserPrincipalName;
            UserPrincipalName = $UserPrincipalName;
            PrimarySmtpAddress = $PrimarySmtpAddress;
            SIPAddress = $SIPAddress;
            LineURI = $LineURI;
            HostingProvider = $HostingProvider;
            TeamsUpgradeEffectiveMode = $TeamsUpgradeEffectiveMode;
            Department = $Department;
            City = $City;
            Office = $Office;
            Title = $Title;
            CustomAttribute5 = $CustomAttribute5;
            CustomAttribute7 = $CustomAttribute7;
            CustomAttribute13 = $CustomAttribute13;
            CustomAttribute14 = $CustomAttribute14;
            CustomAttribute15 = $CustomAttribute15;
        })
        $i++
    }
    
    $DateFull = Get-Date -Format "ddMMyyyy_HH-mm-ss"
    $ReportFile = ".\Report_Detailed_CSOnlineUsers_" + $DateFull + ".xlsx"
    $Table | Export-Excel $ReportFile -TableName "CSOnlineUsers" -Title "CSOnlineUsers" -TitleBold -WorksheetName "CSOnlineUsers" -TableStyle Medium9 -AutoSize -AutoFilter
    Write-log INFO -Message "Generate the following Report: $ReportFile"
}