TechShizz | All posts tagged 'Script'

Office 365 | Add / Remove / Check Calendar Permissions

 This script is designed to be used with the script password encryption for Office 365. See here https://www.techshizz.com/post/powershell-script-password-ecryption-for-multi-site-administration for more info. 

Import-Module MSOnline
$rootpath = (get-item '.\' ).parent.FullName
$clientname = Get-Content "$rootpath\client.txt" -Raw
$user = Get-Content "$rootpath\UserID.txt"
$PasswordFile = "$rootpath\Password.txt"
$KeyFile = "C:\ICU\AES.key"
$key = Get-Content $KeyFile
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Write-Host "Connecting to Exchange Online for $clientname"
Import-PSSession $Session

function Menu{
$menu=@"
1 Add Calendar Permissions
2 Remove Calendar Permissions
3 Check Calendar Permissions

S Search for an email address
Q Quit

Select a task by number or Q to quit
"@

Write-Host "Calendar Permissions" -ForegroundColor Cyan
$r = Read-Host $menu

Switch ($r) {
"1" {
        $emailaddress = Read-Host "Enter the email of the mailbox to provide Calendar access to"
        $accesslevel = Read-Host "Type the level of access you want to add:
        Author
        Contributor
        Editor
        None
        NonEditingAuthor
        Owner
        PublishingEditor
        PublishingAuthor
        Reviewer
        AvailabilityOnly
        LimitedDetails"
        $deligate = Read-Host "Enter the email address of the deligate"
        Add-MailboxFolderPermission –Identity $emailaddress":\Calendar" –User $deligate –AccessRights $accesslevel
        Write-Host "Adding Mailbox Calendar Permissions..."
        Menu
}

"2" {
    $emailaddress = Read-Host "Enter the email of the mailbox who currently has access that you want to remove"
    $deligate = Read-Host "Enter the email address of the mailbox to which the user has access to"
    Write-Host "Removing Mailbox Calendar Access..."
    Remove-MailboxFolderPermission –Identity $emailaddress":\Calendar" –User $deligate
    Menu
}

"3" {
    $emailaddress = Read-Host "Enter the email address that you want to check permissions for"
    Write-Host "Loading..."
    Get-MailboxFolderPermission -Identity $emailaddress":\Calendar" | Format-Table
    Menu
    }
"S" {
    $search = Read-Host "Enter Name to Search"
    Write-Host "Searching..."
    Get-Mailbox | Where-Object { $_.Name -Like "*$search*" } | Select-Object DisplayName,PrimarySmtpAddress | Format-Table
    Menu
}

"Q" {
    Write-Host "Removing any PS Sessions..." -ForegroundColor Green
    Get-PSSession | Remove-PSSession
    Exit
}

default {
    Write-Host "Choose a valid option... Fool!" -ForegroundColor Yellow
}
} #end switch 
}

Menu

Office 365 - Custom Out of Office Script

This script is designed to be used with the script password encryption for Office 365. See here https://www.techshizz.com/post/powershell-script-password-ecryption-for-multi-site-administration for more info. 

$rootpath = (get-item '.\' ).parent.FullName
$clientname = Get-Content "$rootpath\client.txt" -Raw
$user = Get-Content "$rootpath\UserID.txt"
$PasswordFile = "$rootpath\Password.txt"
$KeyFile = "C:\ICU\AES.key"
$key = Get-Content $KeyFile
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
#Import the session 
Write-Host "Connecting to Exchange Online for $clientname"
Import-PSSession $Session -AllowClobber | Out-Null


function Menu {
$menu=@"
1 Set an Out of Office
2 Disable an Out of Office
Q Quit

Select a task by number or Q to quit
"@

Write-Host "Out of Office Configuration" -ForegroundColor Cyan
$r = Read-Host $menu

Switch ($r) {
"1" {
    #Collect Info
$mailbox = Read-Host "Enter the email address for the account to apply an out of office message"
$customemessage = Read-Host "Please enter your custom Out of Office message"
#Set the Our of Office
Write-Host "Setting out of office..."
Set-MailboxAutoReplyConfiguration $mailbox -AutoReplyState enabled -ExternalAudience all -InternalMessage "$customemessage" -ExternalMessage "$customemessage"
Write-Host "Out of Office has been set."
}

"2" {
    $mailbox = Read-Host "Enter the email address of the account to disable Out Of Office"
#Disable
Write-Host "Disabling Auto reply for $mailbox"
Set-MailboxAutoReplyConfiguration $mailbox -AutoReplyState disabled
Write-Host "Confirming changes made..."
Get-MailboxAutoReplyConfiguration $mailbox
Write-Host "Ensure the value now reads 'Disabled'"
Menu
}

"Q" {
    Write-Host "Quitting" -ForegroundColor Green
    Get-PSSession | Remove-PSSession
    Exit
}

default {
    Write-Host "Choose a valid option... Fool!" -ForegroundColor Yellow
    Menu
}
} #end switch

}

Menu
## The following four lines only need to be declared once in your script.
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
 
## Prompt to finsh
$title = "Are you done?"
$message = "Do you want to enable/disable another?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
    0{
        Write-Host "Script has completed."
        Get-PSSession | Remove-PSSession
    }1{
        Menu
        }
}

Office 365 - Out of Office Script

This script is designed to be used with the script password encryption for Office 365. See here https://www.techshizz.com/post/powershell-script-password-ecryption-for-multi-site-administration for more info. 

$rootpath = (get-item '.\' ).parent.FullName
$clientname = Get-Content "$rootpath\client.txt" -Raw
$user = Get-Content "$rootpath\UserID.txt"
$PasswordFile = "$rootpath\Password.txt"
$KeyFile = "C:\ICU\AES.key"
$key = Get-Content $KeyFile
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
#Import the session 
Write-Host "Connecting to Exchange Online for $clientname"
Import-PSSession $Session -AllowClobber | Out-Null


function Menu {
$menu=@"
1 Set an Out of Office
2 Disable an Out of Office
3 Get status of Out of Office
Q Quit

Select a task by number or Q to quit
"@

Write-Host "Out of Office Configuration" -ForegroundColor Cyan
$r = Read-Host $menu

Switch ($r) {
"1" {
    #Collect Info
$mailbox = Read-Host "Enter the email address for the account to apply an out of office message"
$alternatecontact = Read-Host "Enter the name of the person to contact in their absence"
$tel = Read-Host "Enter the telephone number for the alternate contact"
$email = Read-Host "Enter an alternate email address the user can be contacted on"
#Set the Our of Office
Write-Host "Setting out of office..."
Set-MailboxAutoReplyConfiguration $mailbox -AutoReplyState enabled -ExternalAudience all -InternalMessage "I am out of the office and unable to reply to your email. In my absence please contact $alternatecontact on $tel." -ExternalMessage "I am out of the office and unable to reply to your email. In my absence please contact $alternatecontact on $tel or email $email"
Write-Host "Out of Office has been set."
}

"2" {
    $mailbox = Read-Host "Enter the email address of the account to disable Out Of Office"
#Disable
Write-Host "Disabling Auto reply for $mailbox"
Set-MailboxAutoReplyConfiguration $mailbox -AutoReplyState disabled
Write-Host "Confirming changes made..."
Get-MailboxAutoReplyConfiguration $mailbox
Write-Host "Ensure the value now reads 'Disabled'"
Menu
}

"3" {
    $mailbox = Read-Host "Enter the email address of the account you want to see the status of Out of Office"
#Disable
Write-Host "Checking Auto reply status for $mailbox"
get-MailboxAutoReplyConfiguration $mailbox
Write-Host "You can now review the current out of office status for $mailbox above."
Menu
}

"Q" {
    Write-Host "Quitting" -ForegroundColor Green
    Get-PSSession | Remove-PSSession
    Exit
}

default {
    Write-Host "Choose a valid option... Fool!" -ForegroundColor Yellow
    Menu
}
} #end switch

}

Menu
## The following four lines only need to be declared once in your script.
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
 
## Prompt to finsh
$title = "Are you done?"
$message = "Do you want to enable/disable another?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
    0{
        Write-Host "Script has completed."
        Get-PSSession | Remove-PSSession
    }1{
        Menu
        }
}

Office 365 - Leavers script

This script is designed to be used with the script password encryption for Office 365. See here https://www.techshizz.com/post/powershell-script-password-ecryption-for-multi-site-administration for more info. 

The out of office part of the script relies on invoking two other scripts I have made. 

Out of Office

Out of Office Custom

## The following four lines only need to be declared once in your script.
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

## Auto Reply Function
function AutoReply {
$menu=@"

You can now set an out of office, or Quit.

1 Out Of Office - Set template (Must provide alternate contact details)
2 Out of Office - Set a custom message

Q Quit

Select a task by number or Q to quit
"@

Write-Host "Out Of Office" -ForegroundColor Cyan
$r = Read-Host $menu

Switch ($r) {
"1" {
    Write-Host "Loading..." -ForegroundColor Green
    Invoke-Expression -command .\_04Out_Of_Office.ps1
    Exit
}

"2" {
    Write-Host "Loading..." -ForegroundColor Green
    Invoke-Expression -command .\_05Out_Of_Office_Custom_Message.ps1
    Exit
}

"Q" {
    Write-Host "Removing any PS Sessions..." -ForegroundColor Green
    Get-PSSession | Remove-PSSession
    Exit
}

default {
    Write-Host "Choose a valid option... Fool!" -ForegroundColor Yellow
}
} #end switch 
}


Import-Module MSOnline
$rootpath = (get-item '.\' ).parent.FullName
$clientname = Get-Content "$rootpath\client.txt" -Raw
$user = Get-Content "$rootpath\UserID.txt"
$PasswordFile = "$rootpath\Password.txt"
$KeyFile = "C:\ICU\AES.key"
$key = Get-Content $KeyFile
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Write-Host "Connecting to Exchange Online for $clientname"
Import-PSSession $Session
Write-Host "Connecting to Office 365..."
Connect-MsolService -Credential $UserCredential

## Prompt to search for email address
function EmailSearch {
$title = "Email Search"
$message = "Do you want to search for an email address before you start?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
    0{
        $search = Read-Host "Enter Name to Search"
        Write-Host "Searching..."
        Get-Mailbox | Where-Object { $_.Name -Like "*$search*" } | Select-Object DisplayName,PrimarySmtpAddress | Format-Table
        
	EmailSearch
    }1{
        Write-Host "Exiting Search..."
        }
}
}


function RemoveUser{
#Block User Sign in
Write-Host "Blocking User Sign-in Access..."
Set-MsolUser -UserPrincipalName $emailaddress -BlockCredential $true
#Convert Mailbox to shared
Write-Host "Converting Mailbox to Shared..."
Set-Mailbox $emailaddress -type Shared
Write-Host "Waiting for mailbox to finish converting..."
Start-Sleep 30
#Remove Users Licence
Write-Host "Removing Office 365 License..."
$O365User = Get-MsolUser -UserPrincipalName $emailaddress
Set-MsolUserLicense -userprincipalname $emailaddress -Removelicenses $O365User.Licenses.AccountSkuID
}

## Prompt to add deligate access repated
function DeligateAccessLoop {
$title = "Deligate Access"
$message = "Do you want to provide access to the mailbox to someone else?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
    0{
        $deligate = Read-Host "Enter the email address of the deligate"
        Add-MailboxPermission –Identity $emailaddress –User $deligate –AccessRights Fullaccess
        Write-Host "Adding Mailbox permissions..." -ForegroundColor Cyan
        DeligateAccessLoop
    }1{
        AutoReply
        EXIT
        }
}
}

function RemovefromDistros {
$title = "Remove from groups"
$message = "Do you want to remove this user from all distro groups in the tenant?"
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result) {
    0{
    Write-Host "Output will show errors for each group that the user was not a member of, this is noraml, press enter to continue."
    pause
    $DGs= Get-DistributionGroup
    foreach($dg in $DGs)
    {Remove-DistributionGroupMember $dg.name -Member $emailaddress -confirm:$false}
    }
    1{
    
        Write-Host "Skipping Distro Removal..."
    }
    }
    }

EmailSearch

#Grab User ID
$emailaddress = Read-Host "Enter the users Email address"

RemoveUser
RemovefromDistros
DeligateAccessLoop

Get all members of all AD Groups

This isn't the best looking report but it works...

 

$Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=Groups,DC=corp,DC=ourcompany,DC=Com" 
Foreach($G In $Groups)
{
    Write-Host $G.Name
    Write-Host "-------------"
    $G.Members
}