From f4b2a05f68dd687b7bcce304cdeaedb8633dfe82 Mon Sep 17 00:00:00 2001 From: AdminDroid <49208841+admindroid-community@users.noreply.github.com> Date: Mon, 23 Dec 2019 14:45:00 +0530 Subject: [PATCH] . --- .../ConnectExchangeOnlinePowerShell.ps1 | 170 ++--- .../PrivateChannelManagement.ps1 | 638 +++++++++--------- .../O365UserLoginHistory.ps1 | 216 ++++++ 3 files changed, 620 insertions(+), 404 deletions(-) rename ConnectExchangeOnlinePowerShell.ps1 => Connect to Exchange Online PowerShell script/ConnectExchangeOnlinePowerShell.ps1 (97%) rename PrivateChannelManagement.ps1 => Microsoft Teams-Private Channel Management/PrivateChannelManagement.ps1 (97%) create mode 100644 Office 365 User Logon History Report/O365UserLoginHistory.ps1 diff --git a/ConnectExchangeOnlinePowerShell.ps1 b/Connect to Exchange Online PowerShell script/ConnectExchangeOnlinePowerShell.ps1 similarity index 97% rename from ConnectExchangeOnlinePowerShell.ps1 rename to Connect to Exchange Online PowerShell script/ConnectExchangeOnlinePowerShell.ps1 index 08a33ab..1767be2 100644 --- a/ConnectExchangeOnlinePowerShell.ps1 +++ b/Connect to Exchange Online PowerShell script/ConnectExchangeOnlinePowerShell.ps1 @@ -1,85 +1,85 @@ -#Connect to Exchange Online PowerShell - Param - ( - [Parameter(Mandatory = $false)] - [switch]$Disconnect, - [switch]$MFA, - [string]$UserName, - [string]$Password - ) - #Disconnect existing sessions - if($Disconnect.IsPresent) - { - Get-PSSession | Remove-PSSession - Write-Host All sessions in the current window has been removed. -ForegroundColor Yellow - } - #Connect Exchnage Online with MFA - elseif($MFA.IsPresent) - { - #Check for MFA mosule - $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) - If ($MFAExchangeModule -eq $null) - { - Write-Host `nPlease install Exchange Online MFA Module. -ForegroundColor yellow - Write-Host You can install module using below blog : `nLink `nOR you can install module directly by entering "Y"`n - $Confirm= Read-Host Are you sure you want to install module directly? [Y] Yes [N] No - if($Confirm -match "[yY]") - { - Write-Host Yes - Start-Process "iexplore.exe" "https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.application" - } - else - { - Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/' - Exit - } - $Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No - if($Confirmation -match "[yY]") - { - $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) - If ($MFAExchangeModule -eq $null) - { - Write-Host Exchange Online MFA module is not available -ForegroundColor red - Exit - } - } - else - { - Write-Host Exchange Online PowerShell Module is required - Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/' - Exit - } - } - - #Importing Exchange MFA Module - write-host aaaa - . "$MFAExchangeModule" - Connect-EXOPSSession -WarningAction SilentlyContinue | Out-Null - } - #Connect Exchnage Online with Non-MFA - else - { - if(($UserName -ne "") -and ($Password -ne "")) - { - $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force - $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword - } - else - { - $Credential=Get-Credential -Credential $null - } - - $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection - Import-PSSession $Session -DisableNameChecking -AllowClobber -WarningAction SilentlyContinue | Out-Null - } - - #Check for connectivity - if(!($Disconnect.IsPresent)){ - If ((Get-PSSession | Where-Object { $_.ConfigurationName -like "*Exchange*" }) -ne $null) - { - Write-Host `nSuccessfully connected to Exchange Online - } - else - { - Write-Host `nUnable to connect to Exchange Online. Error occurred -ForegroundColor Red - }} +#Connect to Exchange Online PowerShell + Param + ( + [Parameter(Mandatory = $false)] + [switch]$Disconnect, + [switch]$MFA, + [string]$UserName, + [string]$Password + ) + #Disconnect existing sessions + if($Disconnect.IsPresent) + { + Get-PSSession | Remove-PSSession + Write-Host All sessions in the current window has been removed. -ForegroundColor Yellow + } + #Connect Exchnage Online with MFA + elseif($MFA.IsPresent) + { + #Check for MFA mosule + $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) + If ($MFAExchangeModule -eq $null) + { + Write-Host `nPlease install Exchange Online MFA Module. -ForegroundColor yellow + Write-Host You can install module using below blog : `nLink `nOR you can install module directly by entering "Y"`n + $Confirm= Read-Host Are you sure you want to install module directly? [Y] Yes [N] No + if($Confirm -match "[yY]") + { + Write-Host Yes + Start-Process "iexplore.exe" "https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.application" + } + else + { + Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/' + Exit + } + $Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No + if($Confirmation -match "[yY]") + { + $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) + If ($MFAExchangeModule -eq $null) + { + Write-Host Exchange Online MFA module is not available -ForegroundColor red + Exit + } + } + else + { + Write-Host Exchange Online PowerShell Module is required + Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/' + Exit + } + } + + #Importing Exchange MFA Module + write-host aaaa + . "$MFAExchangeModule" + Connect-EXOPSSession -WarningAction SilentlyContinue | Out-Null + } + #Connect Exchnage Online with Non-MFA + else + { + if(($UserName -ne "") -and ($Password -ne "")) + { + $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force + $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword + } + else + { + $Credential=Get-Credential -Credential $null + } + + $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection + Import-PSSession $Session -DisableNameChecking -AllowClobber -WarningAction SilentlyContinue | Out-Null + } + + #Check for connectivity + if(!($Disconnect.IsPresent)){ + If ((Get-PSSession | Where-Object { $_.ConfigurationName -like "*Exchange*" }) -ne $null) + { + Write-Host `nSuccessfully connected to Exchange Online + } + else + { + Write-Host `nUnable to connect to Exchange Online. Error occurred -ForegroundColor Red + }} diff --git a/PrivateChannelManagement.ps1 b/Microsoft Teams-Private Channel Management/PrivateChannelManagement.ps1 similarity index 97% rename from PrivateChannelManagement.ps1 rename to Microsoft Teams-Private Channel Management/PrivateChannelManagement.ps1 index 1fe3734..465affe 100644 --- a/PrivateChannelManagement.ps1 +++ b/Microsoft Teams-Private Channel Management/PrivateChannelManagement.ps1 @@ -1,319 +1,319 @@ -<# -============================================================================================= -Name: PrivateChannelManagement -Description: This script performs Private Channel related management actions and reporting -Version: 1.0 -Released date: 18/11/2019 -website: blog.admindroid.com -Script by: AdminDroid Team (Proud Creators of AdminDroid Office 365 Reporting Tool) - - -To run the script -./PrivateChannelManagement.ps1 - -To schdeule/run the script by explicitly mentioning credential -./PrivateChannelManagement.ps1 -UserName -Password - -To run the script with MFA enabled account -./PrivateChannelManagement.ps1 -MFA -============================================================================================ -#> - -#Accept input paramenters -param( -[string]$UserName, -[string]$Password, -[switch]$MFA, -[int]$Action -) - - -#install latest Microsoft Teams module from PowerShell Test Gallery -$Module=Get-Module -Name MicrosoftTeams -ListAvailable -if($Module.count -eq 0) -{ - $Confirm= Read-Host Are you sure you want to install Microsoft Teams module? [Y] Yes [N] No - if($Confirm -match "[y]") - { - Register-PSRepository -Name PSGalleryInt -SourceLocation https://www.poshtestgallery.com/ -InstallationPolicy Trusted - Install-Module -Name MicrosoftTeams -Repository PSGalleryInt -Force - Write-Host Installing Microsoft Teams Module... - } - else - { - Write-Host `nNeed Microsoft Teams PowerShell module. Please install the latest module from PowerShell Test Gallery -ForegroundColor Yellow - exit - } -} -#Check for latest Microsoft Teams PowerShell Module -elseif((Get-module -Name MicrosoftTeams -ListAvailable).version -lt "1.0.18") -{ - Write-Host `nTo manage Private Channel, you must install lastest version of MicrosoftTeams PowerShell module from PowerShell Test Gallery - $Confirm= Read-Host `nAre you sure you want to uninstall old version ? [Y] Yes [N] No - if($Confirm -match "[y]") - { - Uninstall-Module -Name MicrosoftTeams - } - else - { - Write-Host Please install latest version of Microsoft Teams PowerShell module from PowerShell Test Gallery -ForegroundColor Yellow - exit - } - $Confirm= Read-Host `nAre you sure you want to install latest version of Microsoft Teams module? [Y] Yes [N] No - if($Confirm -match "[y]") - { - Register-PSRepository -Name PSGalleryInt -SourceLocation https://www.poshtestgallery.com/ -InstallationPolicy Trusted - Install-Module -Name MicrosoftTeams -Repository PSGalleryInt -Force - Write-Host Installing Microsoft Teams Module... - } - else - { - Write-Host `nPlease install latest version of Microsoft Teams PowerShell module from PowerShell Test Gallery -ForegroundColor Yellow - exit - } -} - - -#Check Skype for Business Online module -$Module=Get-Module -Name SkypeOnlineConnector -ListAvailable -if($Module.count -eq 0) -{ - Write-Host `nPlease install Skype for Business Online PowerShell Module -ForegroundColor yellow - Write-Host `nYou can download the Skype Online PowerShell module directly using below url: https://download.microsoft.com/download/2/0/5/2050B39B-4DA5-48E0-B768-583533B42C3B/SkypeOnlinePowerShell.Exe - Write-Host `nAfter installing module, Please close all existing PowerShell sessions. Start new PowerShell console and rerun this script. - exit -} -Write-Host Preparing required PowerShell Modules... -Get-PSSession | Remove-PSSession -#Authentication using MFA -if($MFA.IsPresent) -{ - Write-Host Importing Skype for Business Online PowerShell Module... - $sfbSession = New-CsOnlineSession - Import-PSSession $sfbSession -AllowClobber | Out-Null - Write-Host Importing Microsoft Teams PowerShell Module... - Connect-MicrosoftTeams | Out-Null -} - -#Authentication using non-MFA -else -{ - #Storing credential in script for scheduling purpose/ Passing credential as parameter - if(($UserName -ne "") -and ($Password -ne "")) - { - $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force - $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword - } - else - { - $Credential=Get-Credential -Credential $null - } - Write-Host Importing Skype for Business Online PowerShell Module... - $sfbSession = New-CsOnlineSession -Credential $Credential - Import-PSSession $sfbSession -AllowClobber -WarningAction SilentlyContinue | Out-Null - Write-Host Importing Microsoft Teams PowerShell Module... - Connect-MicrosoftTeams -Credential $Credential | Out-Null -} -[boolean]$Delay=$false -Do { - if($Action -eq "") - { - if($Delay -eq $true) - { - Start-Sleep -Seconds 2 - } - $Delay=$true - Write-Host "" - Write-host `nPrivate Channel Management -ForegroundColor Yellow - Write-Host " 1.Allow for Organization" -ForegroundColor Cyan - Write-Host " 2.Disable for Organization" -ForegroundColor Cyan - Write-Host " 3.Allow for a User" -ForegroundColor Cyan - Write-Host " 4.Disable for a User" -ForegroundColor Cyan - Write-Host " 5.Allow User in bulk using CSV import" -ForegroundColor Cyan - Write-Host " 6.Disable User in bulk using CSV import" -ForegroundColor Cyan - Write-Host `nPrivate Channel Reporting -ForegroundColor Yellow - Write-Host " 7.All Private Channels in Organization" -ForegroundColor Cyan - Write-Host " 8.All Private Channels in Teams" -ForegroundColor Cyan - Write-Host " 9.Members and Owners Report of All Private Channels" -ForegroundColor Cyan - Write-Host " 10.Members and Owners Report of Single Private Channel" -ForegroundColor Cyan - Write-Host " 0.Exit" -ForegroundColor Cyan - Write-Host "" - $i = Read-Host 'Please choose the action to continue' - } - else - { - $i=$Action - } - Switch ($i) { - 1 { - Set-CsTeamsChannelsPolicy -Identity Global –AllowPrivateChannelCreation $True - Write-Host Private Channel creation allowed for Organization wide -ForegroundColor Green - } - - 2 { - Set-CsTeamsChannelsPolicy -Identity Global –AllowPrivateChannelCreation $False - Write-Host Blocked Private Channel creation Organization wide -ForegroundColor Green - } - - 3 { - if((Get-CsTeamsChannelsPolicy -Identity "Allow Private Channel Creation" -ErrorAction silent) -eq $null) - { - New-CsTeamsChannelsPolicy -Identity "Allow Private Channel Creation" -AllowPrivateChannelCreation $True | Out-Null - } - $User=Read-Host Enter User name"(UPN format)" to grant the right to create Private Channel - Grant-CsTeamsChannelsPolicy -PolicyName "Allow Private Channel Creation" -Identity $User - if($?) - { - Write-host `nNow $User can create Private Channel -ForegroundColor Green - } - } - - 4 { - if((Get-CsTeamsChannelsPolicy -Identity "Disable Private Channel Creation" -ErrorAction silent) -eq $null) - { - New-CsTeamsChannelsPolicy -Identity "Disable Private Channel Creation" -AllowPrivateChannelCreation $False | Out-Null - } - $User=Read-Host Enter User name"(UPN format)" to disable Private Channel creation - Grant-CsTeamsChannelsPolicy -PolicyName "Disable Private Channel Creation" -Identity $User - if($?) - { - $? - Write-host `nPrivate Channel creation blocked for $User -ForegroundColor green - } - } - - 5 { - if((Get-CsTeamsChannelsPolicy -Identity "Allow Private Channel Creation" -ErrorAction silent) -eq $null) - { - New-CsTeamsChannelsPolicy -Identity "Allow Private Channel Creation" -AllowPrivateChannelCreation $True - } - Write-Host `nThe file must follow the format: Users"'" UPN separated by new line without header -ForegroundColor Magenta - $UserNamesFile=Read-Host Enter CSV/txt file path"(Eg:C:\Users\Desktop\UserNames.txt)" - $Users=@() - $Users=Import-Csv -Header "UserPrincipalName" $UserNamesFile - foreach($User in $Users) - { - Grant-CsTeamsChannelsPolicy -PolicyName "Allow Private Channel Creation" -Identity $User.UserPrincipalName - if($?) - { - Write-host Now $User.UserPrincipalName can create Private Channel -ForegroundColor Green - } - } - } - - 6 { - if((Get-CsTeamsChannelsPolicy -Identity "Disable Private Channel Creation" -ErrorAction silent) -eq $null) - { - New-CsTeamsChannelsPolicy -Identity "Disable Private Channel Creation" -AllowPrivateChannelCreation $False - } - Write-Host `nThe file must follow the format: Users"'" UPN separated by new line without header -ForegroundColor Magenta - $UserNamesFile=Read-Host Enter CSV/txt file path"(Eg:C:\Users\Desktop\UserNames.txt)" - $Users=@() - $Users=Import-Csv -Header "UserPrincipalName" $UserNamesFile - foreach($User in $Users) - { - Grant-CsTeamsChannelsPolicy -PolicyName "Disable Private Channel Creation" -Identity $User.UserPrincipalName - if($?) - { - Write-host Private Channel creation blocked for $User.UserPrincipalName -ForegroundColor Green - } - } - } - - 7 { - $Result="" - $Results=@() - $Path="./AllPrivateChannels_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" - Write-Host Exporting Private Channels report... - $Count=0 - Get-Team | foreach { - $TeamName=$_.DisplayName - Write-Progress -Activity "`n Processed Teams count: $Count "`n" Currently Processing: $TeamName" - $Count++ - $GroupId=$_.GroupId - $PrivateChannels=(Get-TeamChannel -GroupId $GroupId -MembershipType Private).DisplayName - foreach($PrivateChannel in $PrivateChannels) - { - $Result=@{'Teams Name'=$TeamName;'Private Channel'=$PrivateChannel} - $Results= New-Object psobject -Property $Result - $Results | select 'Teams Name','Private Channel' | Export-Csv $Path -NoTypeInformation -Append - } - } - Write-Progress -Activity "`n Processed Teams count: $Count "`n" Currently Processing: $TeamName" -Completed - if((Test-Path -Path $Path) -eq "True") - { - Write-Host `nReport available in $Path -ForegroundColor Green - } - } - - 8 { - $TeamName=Read-Host Enter Teams name "(Case Sensitive)": - Write-Host Exporting Private Channel report... - $GroupId=(Get-Team -DisplayName $TeamName).GroupId - $Path=".\Private Channels available in $TeamName$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" - Get-TeamChannel -GroupId $GroupId -MembershipType Private | select DisplayName | Export-Csv $Path -NoTypeInformation - if((Test-Path -Path $Path) -eq "True") - { - Write-Host `nReport available in $Path -ForegroundColor Green - } - } - - 9{ - $Result="" - $Results=@() - Write-Host Exporting all Private Channel"'s" Members and Owners report... - $Count=0 - $Path="./AllPrivateChannels Members and Owners Report_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" - Get-Team | foreach { - $TeamName=$_.DisplayName - $GroupId=$_.GroupId - $PrivateChannels=(Get-TeamChannel -GroupId $GroupId -MembershipType Private).DisplayName - foreach($PrivateChannel in $PrivateChannels) - { - Write-Progress -Activity "`n Processed Private Channel count: $Count "`n" Currently Processing: $PrivateChannel" - $Count++ - Get-TeamChannelUser -GroupId $GroupId -DisplayName $PrivateChannel | foreach { - $Name=$_.Name - $UPN=$_.User - $Role=$_.Role - $Result=@{'Teams Name'=$TeamName;'Private Channel Name'=$PrivateChannel;'UPN'=$UPN;'User Display Name'=$Name;'Role'=$Role} - $Results= New-Object psobject -Property $Result - $Results | select 'Teams Name','Private Channel Name',UPN,'User Display Name',Role | Export-Csv $Path -NoTypeInformation -Append - } - } - } - Write-Progress -Activity "`n Processed Private Channel count: $Count "`n" Currently Processing: $PrivateChannel" -Completed - if((Test-Path -Path $Path) -eq "True") - { - Write-Host `nReport available in $Path -ForegroundColor Green - } - } - - 10 { - $Result="" - $Results=@() - $TeamName=Read-Host Enter Teams name in which Private Channel resides "(Case sensitive)": - $ChannelName=Read-Host Enter Private Channel name - $GroupId=(Get-Team -DisplayName $TeamName).GroupId - Write-Host Exporting $ChannelName"'s" Members and Owners report... - $Path=".\MembersOf $ChannelName$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" - Get-TeamChannelUser -GroupId $GroupId -DisplayName $ChannelName | foreach { - $Name=$_.Name - $UPN=$_.User - $Role=$_.Role - $Result=@{'Teams Name'=$TeamName;'Private Channel Name'=$ChannelName;'UPN'=$UPN;'User Display Name'=$Name;'Role'=$Role} - $Results= New-Object psobject -Property $Result - $Results | select 'Teams Name','Private Channel Name',UPN,'User Display Name',Role | Export-Csv $Path -NoTypeInformation -Append - } - if((Test-Path -Path $Path) -eq "True") - { - Write-Host `nReport available in $Path -ForegroundColor Green - } - } - } - if($Action -ne "") - {exit} - } - While ($i -ne 0) - Clear-Host - +<# +============================================================================================= +Name: PrivateChannelManagement +Description: This script performs Private Channel related management actions and reporting +Version: 1.0 +Released date: 18/11/2019 +website: blog.admindroid.com +Script by: AdminDroid Team (Proud Creators of AdminDroid Office 365 Reporting Tool) + + +To run the script +./PrivateChannelManagement.ps1 + +To schdeule/run the script by explicitly mentioning credential +./PrivateChannelManagement.ps1 -UserName -Password + +To run the script with MFA enabled account +./PrivateChannelManagement.ps1 -MFA +============================================================================================ +#> + +#Accept input paramenters +param( +[string]$UserName, +[string]$Password, +[switch]$MFA, +[int]$Action +) + + +#install latest Microsoft Teams module from PowerShell Test Gallery +$Module=Get-Module -Name MicrosoftTeams -ListAvailable +if($Module.count -eq 0) +{ + $Confirm= Read-Host Are you sure you want to install Microsoft Teams module? [Y] Yes [N] No + if($Confirm -match "[y]") + { + Register-PSRepository -Name PSGalleryInt -SourceLocation https://www.poshtestgallery.com/ -InstallationPolicy Trusted + Install-Module -Name MicrosoftTeams -Repository PSGalleryInt -Force + Write-Host Installing Microsoft Teams Module... + } + else + { + Write-Host `nNeed Microsoft Teams PowerShell module. Please install the latest module from PowerShell Test Gallery -ForegroundColor Yellow + exit + } +} +#Check for latest Microsoft Teams PowerShell Module +elseif((Get-module -Name MicrosoftTeams -ListAvailable).version -lt "1.0.18") +{ + Write-Host `nTo manage Private Channel, you must install lastest version of MicrosoftTeams PowerShell module from PowerShell Test Gallery + $Confirm= Read-Host `nAre you sure you want to uninstall old version ? [Y] Yes [N] No + if($Confirm -match "[y]") + { + Uninstall-Module -Name MicrosoftTeams + } + else + { + Write-Host Please install latest version of Microsoft Teams PowerShell module from PowerShell Test Gallery -ForegroundColor Yellow + exit + } + $Confirm= Read-Host `nAre you sure you want to install latest version of Microsoft Teams module? [Y] Yes [N] No + if($Confirm -match "[y]") + { + Register-PSRepository -Name PSGalleryInt -SourceLocation https://www.poshtestgallery.com/ -InstallationPolicy Trusted + Install-Module -Name MicrosoftTeams -Repository PSGalleryInt -Force + Write-Host Installing Microsoft Teams Module... + } + else + { + Write-Host `nPlease install latest version of Microsoft Teams PowerShell module from PowerShell Test Gallery -ForegroundColor Yellow + exit + } +} + + +#Check Skype for Business Online module +$Module=Get-Module -Name SkypeOnlineConnector -ListAvailable +if($Module.count -eq 0) +{ + Write-Host `nPlease install Skype for Business Online PowerShell Module -ForegroundColor yellow + Write-Host `nYou can download the Skype Online PowerShell module directly using below url: https://download.microsoft.com/download/2/0/5/2050B39B-4DA5-48E0-B768-583533B42C3B/SkypeOnlinePowerShell.Exe + Write-Host `nAfter installing module, Please close all existing PowerShell sessions. Start new PowerShell console and rerun this script. + exit +} +Write-Host Preparing required PowerShell Modules... +Get-PSSession | Remove-PSSession +#Authentication using MFA +if($MFA.IsPresent) +{ + Write-Host Importing Skype for Business Online PowerShell Module... + $sfbSession = New-CsOnlineSession + Import-PSSession $sfbSession -AllowClobber | Out-Null + Write-Host Importing Microsoft Teams PowerShell Module... + Connect-MicrosoftTeams | Out-Null +} + +#Authentication using non-MFA +else +{ + #Storing credential in script for scheduling purpose/ Passing credential as parameter + if(($UserName -ne "") -and ($Password -ne "")) + { + $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force + $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword + } + else + { + $Credential=Get-Credential -Credential $null + } + Write-Host Importing Skype for Business Online PowerShell Module... + $sfbSession = New-CsOnlineSession -Credential $Credential + Import-PSSession $sfbSession -AllowClobber -WarningAction SilentlyContinue | Out-Null + Write-Host Importing Microsoft Teams PowerShell Module... + Connect-MicrosoftTeams -Credential $Credential | Out-Null +} +[boolean]$Delay=$false +Do { + if($Action -eq "") + { + if($Delay -eq $true) + { + Start-Sleep -Seconds 2 + } + $Delay=$true + Write-Host "" + Write-host `nPrivate Channel Management -ForegroundColor Yellow + Write-Host " 1.Allow for Organization" -ForegroundColor Cyan + Write-Host " 2.Disable for Organization" -ForegroundColor Cyan + Write-Host " 3.Allow for a User" -ForegroundColor Cyan + Write-Host " 4.Disable for a User" -ForegroundColor Cyan + Write-Host " 5.Allow User in bulk using CSV import" -ForegroundColor Cyan + Write-Host " 6.Disable User in bulk using CSV import" -ForegroundColor Cyan + Write-Host `nPrivate Channel Reporting -ForegroundColor Yellow + Write-Host " 7.All Private Channels in Organization" -ForegroundColor Cyan + Write-Host " 8.All Private Channels in Teams" -ForegroundColor Cyan + Write-Host " 9.Members and Owners Report of All Private Channels" -ForegroundColor Cyan + Write-Host " 10.Members and Owners Report of Single Private Channel" -ForegroundColor Cyan + Write-Host " 0.Exit" -ForegroundColor Cyan + Write-Host "" + $i = Read-Host 'Please choose the action to continue' + } + else + { + $i=$Action + } + Switch ($i) { + 1 { + Set-CsTeamsChannelsPolicy -Identity Global –AllowPrivateChannelCreation $True + Write-Host Private Channel creation allowed for Organization wide -ForegroundColor Green + } + + 2 { + Set-CsTeamsChannelsPolicy -Identity Global –AllowPrivateChannelCreation $False + Write-Host Blocked Private Channel creation Organization wide -ForegroundColor Green + } + + 3 { + if((Get-CsTeamsChannelsPolicy -Identity "Allow Private Channel Creation" -ErrorAction silent) -eq $null) + { + New-CsTeamsChannelsPolicy -Identity "Allow Private Channel Creation" -AllowPrivateChannelCreation $True | Out-Null + } + $User=Read-Host Enter User name"(UPN format)" to grant the right to create Private Channel + Grant-CsTeamsChannelsPolicy -PolicyName "Allow Private Channel Creation" -Identity $User + if($?) + { + Write-host `nNow $User can create Private Channel -ForegroundColor Green + } + } + + 4 { + if((Get-CsTeamsChannelsPolicy -Identity "Disable Private Channel Creation" -ErrorAction silent) -eq $null) + { + New-CsTeamsChannelsPolicy -Identity "Disable Private Channel Creation" -AllowPrivateChannelCreation $False | Out-Null + } + $User=Read-Host Enter User name"(UPN format)" to disable Private Channel creation + Grant-CsTeamsChannelsPolicy -PolicyName "Disable Private Channel Creation" -Identity $User + if($?) + { + $? + Write-host `nPrivate Channel creation blocked for $User -ForegroundColor green + } + } + + 5 { + if((Get-CsTeamsChannelsPolicy -Identity "Allow Private Channel Creation" -ErrorAction silent) -eq $null) + { + New-CsTeamsChannelsPolicy -Identity "Allow Private Channel Creation" -AllowPrivateChannelCreation $True + } + Write-Host `nThe file must follow the format: Users"'" UPN separated by new line without header -ForegroundColor Magenta + $UserNamesFile=Read-Host Enter CSV/txt file path"(Eg:C:\Users\Desktop\UserNames.txt)" + $Users=@() + $Users=Import-Csv -Header "UserPrincipalName" $UserNamesFile + foreach($User in $Users) + { + Grant-CsTeamsChannelsPolicy -PolicyName "Allow Private Channel Creation" -Identity $User.UserPrincipalName + if($?) + { + Write-host Now $User.UserPrincipalName can create Private Channel -ForegroundColor Green + } + } + } + + 6 { + if((Get-CsTeamsChannelsPolicy -Identity "Disable Private Channel Creation" -ErrorAction silent) -eq $null) + { + New-CsTeamsChannelsPolicy -Identity "Disable Private Channel Creation" -AllowPrivateChannelCreation $False + } + Write-Host `nThe file must follow the format: Users"'" UPN separated by new line without header -ForegroundColor Magenta + $UserNamesFile=Read-Host Enter CSV/txt file path"(Eg:C:\Users\Desktop\UserNames.txt)" + $Users=@() + $Users=Import-Csv -Header "UserPrincipalName" $UserNamesFile + foreach($User in $Users) + { + Grant-CsTeamsChannelsPolicy -PolicyName "Disable Private Channel Creation" -Identity $User.UserPrincipalName + if($?) + { + Write-host Private Channel creation blocked for $User.UserPrincipalName -ForegroundColor Green + } + } + } + + 7 { + $Result="" + $Results=@() + $Path="./AllPrivateChannels_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" + Write-Host Exporting Private Channels report... + $Count=0 + Get-Team | foreach { + $TeamName=$_.DisplayName + Write-Progress -Activity "`n Processed Teams count: $Count "`n" Currently Processing: $TeamName" + $Count++ + $GroupId=$_.GroupId + $PrivateChannels=(Get-TeamChannel -GroupId $GroupId -MembershipType Private).DisplayName + foreach($PrivateChannel in $PrivateChannels) + { + $Result=@{'Teams Name'=$TeamName;'Private Channel'=$PrivateChannel} + $Results= New-Object psobject -Property $Result + $Results | select 'Teams Name','Private Channel' | Export-Csv $Path -NoTypeInformation -Append + } + } + Write-Progress -Activity "`n Processed Teams count: $Count "`n" Currently Processing: $TeamName" -Completed + if((Test-Path -Path $Path) -eq "True") + { + Write-Host `nReport available in $Path -ForegroundColor Green + } + } + + 8 { + $TeamName=Read-Host Enter Teams name "(Case Sensitive)": + Write-Host Exporting Private Channel report... + $GroupId=(Get-Team -DisplayName $TeamName).GroupId + $Path=".\Private Channels available in $TeamName$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" + Get-TeamChannel -GroupId $GroupId -MembershipType Private | select DisplayName | Export-Csv $Path -NoTypeInformation + if((Test-Path -Path $Path) -eq "True") + { + Write-Host `nReport available in $Path -ForegroundColor Green + } + } + + 9{ + $Result="" + $Results=@() + Write-Host Exporting all Private Channel"'s" Members and Owners report... + $Count=0 + $Path="./AllPrivateChannels Members and Owners Report_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" + Get-Team | foreach { + $TeamName=$_.DisplayName + $GroupId=$_.GroupId + $PrivateChannels=(Get-TeamChannel -GroupId $GroupId -MembershipType Private).DisplayName + foreach($PrivateChannel in $PrivateChannels) + { + Write-Progress -Activity "`n Processed Private Channel count: $Count "`n" Currently Processing: $PrivateChannel" + $Count++ + Get-TeamChannelUser -GroupId $GroupId -DisplayName $PrivateChannel | foreach { + $Name=$_.Name + $UPN=$_.User + $Role=$_.Role + $Result=@{'Teams Name'=$TeamName;'Private Channel Name'=$PrivateChannel;'UPN'=$UPN;'User Display Name'=$Name;'Role'=$Role} + $Results= New-Object psobject -Property $Result + $Results | select 'Teams Name','Private Channel Name',UPN,'User Display Name',Role | Export-Csv $Path -NoTypeInformation -Append + } + } + } + Write-Progress -Activity "`n Processed Private Channel count: $Count "`n" Currently Processing: $PrivateChannel" -Completed + if((Test-Path -Path $Path) -eq "True") + { + Write-Host `nReport available in $Path -ForegroundColor Green + } + } + + 10 { + $Result="" + $Results=@() + $TeamName=Read-Host Enter Teams name in which Private Channel resides "(Case sensitive)": + $ChannelName=Read-Host Enter Private Channel name + $GroupId=(Get-Team -DisplayName $TeamName).GroupId + Write-Host Exporting $ChannelName"'s" Members and Owners report... + $Path=".\MembersOf $ChannelName$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" + Get-TeamChannelUser -GroupId $GroupId -DisplayName $ChannelName | foreach { + $Name=$_.Name + $UPN=$_.User + $Role=$_.Role + $Result=@{'Teams Name'=$TeamName;'Private Channel Name'=$ChannelName;'UPN'=$UPN;'User Display Name'=$Name;'Role'=$Role} + $Results= New-Object psobject -Property $Result + $Results | select 'Teams Name','Private Channel Name',UPN,'User Display Name',Role | Export-Csv $Path -NoTypeInformation -Append + } + if((Test-Path -Path $Path) -eq "True") + { + Write-Host `nReport available in $Path -ForegroundColor Green + } + } + } + if($Action -ne "") + {exit} + } + While ($i -ne 0) + Clear-Host + diff --git a/Office 365 User Logon History Report/O365UserLoginHistory.ps1 b/Office 365 User Logon History Report/O365UserLoginHistory.ps1 new file mode 100644 index 0000000..9652a1d --- /dev/null +++ b/Office 365 User Logon History Report/O365UserLoginHistory.ps1 @@ -0,0 +1,216 @@ +Param +( + [Parameter(Mandatory = $false)] + [switch]$Success, + [switch]$Failed, + [switch]$MFA, + [Nullable[DateTime]]$StartDate, + [Nullable[DateTime]]$EndDate, + [string]$UserName, + [string]$AdminName, + [string]$Password +) + +#Getting StartDate and EndDate for Audit log +if ((($StartDate -eq $null) -and ($EndDate -ne $null)) -or (($StartDate -ne $null) -and ($EndDate -eq $null))) +{ + Write-Host `nPlease enter both StartDate and EndDate for Audit log collection -ForegroundColor Red + exit +} +elseif(($StartDate -eq $null) -and ($EndDate -eq $null)) +{ + $StartDate=(((Get-Date).AddDays(-90))).Date + $EndDate=Get-Date +} +else +{ + $StartDate=[DateTime]$StartDate + $EndDate=[DateTime]$EndDate + if($StartDate -lt ((Get-Date).AddDays(-90))) + { + Write-Host `nAudit log can be retrieved only for past 90 days. Please select a date after (Get-Date).AddDays(-90) -ForegroundColor Red + Exit + } + if($EndDate -lt ($StartDate)) + { + Write-Host `nEnd time should be later than start time -ForegroundColor Red + Exit + } +} + + +#Authentication using MFA +if($MFA.IsPresent) +{ + $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) + If ($MFAExchangeModule -eq $null) + { + Write-Host `nPlease install Exchange Online MFA Module. -ForegroundColor yellow + Write-Host You can manually install module using below blog : `nhttps://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/ `nOR you can install module directly by entering "Y"`n + $Confirm= Read-Host `nAre you sure you want to install module directly? [Y] Yes [N] No + if($Confirm -match "[Y]") + { + Start-Process "iexplore.exe" "https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.application" + } + else + { + Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/' + Exit + } + $Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No + if($Confirmation -match "[yY]") + { + $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) + If ($MFAExchangeModule -eq $null) + { + Write-Host Exchange Online MFA module is not available -ForegroundColor red + Exit + } + } + else + { + Write-Host Exchange Online PowerShell Module is required + Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/' + Exit + } + } + + #Importing Exchange MFA Module + . "$MFAExchangeModule" + Write-Host Enter credential in prompt to connect to Exchange Online + Connect-EXOPSSession -WarningAction SilentlyContinue +} + +#Authentication using non-MFA +else +{ + #Storing credential in script for scheduling purpose/ Passing credential as parameter + if(($AdminName -ne "") -and ($Password -ne "")) + { + $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force + $Credential = New-Object System.Management.Automation.PSCredential $AdminName,$SecuredPassword + } + else + { + $Credential=Get-Credential -Credential $null + } + $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection -WarningAction SilentlyContinue + Import-PSSession $Session -AllowClobber -WarningAction SilentlyContinue | Out-Null +} + +$OutputCSV=".\AuditLog_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" +$IntervalTimeInMinutes=1440 #$IntervalTimeInMinutes=Read-Host Enter interval time period '(in minutes)' +$CurrentStart=$StartDate +$CurrentEnd=$CurrentStart.AddMinutes($IntervalTimeInMinutes) + +#Filter for successful login attempts +if($success.IsPresent) +{ + $Operation="UserLoggedIn,TeamsSessionStarted,MailboxLogin" +} +#Filter for successful login attempts +elseif($Failed.IsPresent) +{ + $Operation="UserLoginFailed" +} +else +{ + $Operation="UserLoggedIn,UserLoginFailed,TeamsSessionStarted,MailboxLogin" +} + +#Check whether CurrentEnd exceeds EndDate(checks for 1st iteration) +if($CurrentEnd -gt $EndDate) +{ + $CurrentEnd=$EndDate +} + +$AggregateResults = 0 +$CurrentResult= @() +$CurrentResultCount=0 +Write-Host `nRetrieving audit log from $StartDate to $EndDate... -ForegroundColor Yellow + +while($true) +{ + #Write-Host Retrieving audit log between StartDate $CurrentStart to EndDate $CurrentEnd ******* IntervalTime $IntervalTimeInMinutes minutes + if($CurrentStart -eq $CurrentEnd) + { + Write-Host Start and end time are same.Please enter different time range -ForegroundColor Red + Exit + } + + #Getting audit log for specific user(s) for a given time range + if($UserName -ne "") + { + $Results=Search-UnifiedAuditLog -UserIds $UserName -StartDate $CurrentStart -EndDate $CurrentEnd -operations $Operation -SessionId s -SessionCommand ReturnLargeSet -ResultSize 5000 + } + + #Getting audit log for all users for a given time range + else + { + $Results=Search-UnifiedAuditLog -StartDate $CurrentStart -EndDate $CurrentEnd -Operations $Operation -SessionId s -SessionCommand ReturnLargeSet -ResultSize 5000 + } + #$Results.count + $AllAuditData=@() + $AllAudits= + foreach($Result in $Results) + { + $AuditData=$Result.auditdata | ConvertFrom-Json + $AuditData.CreationTime=(Get-Date($AuditData.CreationTime)).ToLocalTime() + $AllAudits=@{'Login Time'=$AuditData.CreationTime;'User Name'=$AuditData.UserId;'IP Address'=$AuditData.ClientIP;'Operation'=$AuditData.Operation;'Result Status'=$AuditData.ResultStatus;'Workload'=$AuditData.Workload} + $AllAuditData= New-Object PSObject -Property $AllAudits + $AllAuditData | Sort 'Login Time','User Name' | select 'Login Time','User Name','IP Address',Operation,'Result Status',Workload | Export-Csv $OutputCSV -NoTypeInformation -Append + } + Write-Progress -Activity "`n Retrieving audit log from $StartDate to $EndDate.."`n" Processed audit record count: $AggregateResults" + #$CurrentResult += $Results + $currentResultCount=$CurrentResultCount+($Results.count) + $AggregateResults +=$Results.count + if(($CurrentResultCount -eq 50000) -or ($Results.count -lt 5000)) + { + if($CurrentResultCount -eq 50000) + { + Write-Host Retrieved max record for the current range.Proceeding further may cause data loss or rerun the script with reduced time interval. -ForegroundColor Red + $Confirm=Read-Host `nAre you sure you want to continue? [Y] Yes [N] No + if($Confirm -notmatch "[Y]") + { + Write-Host Please rerun the script with reduced time interval -ForegroundColor Red + Exit + } + else + { + Write-Host Proceeding audit log collection with data loss + } + } + #Check for last iteration + if(($CurrentEnd -eq $EndDate)) + { + break + } + [DateTime]$CurrentStart=$CurrentEnd + #Break loop if start date exceeds current date(There will be no data) + if($CurrentStart -gt (Get-Date)) + { + break + } + [DateTime]$CurrentEnd=$CurrentStart.AddMinutes($IntervalTimeInMinutes) + if($CurrentEnd -gt $EndDate) + { + $CurrentEnd=$EndDate + } + + $CurrentResultCount=0 + $CurrentResult = @() + } +} + +If($AggregateResults -eq 0) +{ + Write-Host No records found +} +else +{ + if((Test-Path -Path $OutputCSV) -eq "True") + { + Write-Host `nThe Output file availble in $OutputCSV -ForegroundColor Green + } + Write-Host `nThe output file contains $AggregateResults audit records +}