Scheduled Tasks in Office 365

It will become increasingly prominent that as customers delve into Office 365 that you will need to get Scheduled Tasks into production for your Office 365 tenancy. The main blocker of this will be that when you connect to either Exchange Online, Office 365 etc PowerShell modules you are prompted for your admin credentials. There is a method to secure your credentials in a hash file locally to bind into a credential variable.

Below is the PowerShell that you can append to your scripts in order to complete that:

Read-Host –AsSecureString | ConvertFrom-SecureString | Out-File C:\temp\EXOpassword.txt 

Initially you will need to create the hashed text file which the Read-Host will complete.

$Password = Get-Content C:\temp\EXOpassword.txt | ConvertTo-SecureString

 

$UserID =‘Enter.Username@yourdom.ain’

 

$Cred = New-Object System.Management.Automation.PSCredential $userid,$password 

 

$Session = New-PSSession ConfigurationName Microsoft.Exchange ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication basic AllowRedirection
 

Import-PSSession $Session AllowClobber
 

#Connect to O365 Tenant

Connect-MsolService -Credential $Cred

This example will connect you to 0365 PowerShell.

You can place this at the start of your scripts in order to authenticate you to your relevant cloud service before then appending your scripts below. An example of this code in a working script is the below, which gets any Unified Groups that are created as Public and emailing an Administrator.

$Password = Get-Content C:\temp\EXOpassword.txt | ConvertTo-SecureString

 

$UserID =‘hpo@mysmartplace.net’

 

$Cred = New-Object System.Management.Automation.PSCredential $UserID,$Password 

 

$Session = New-PSSession ConfigurationName Microsoft.Exchange ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication basic AllowRedirection

 

Import-PSSession $Session AllowClobber

 

#Connect to O365 Tenant 

Connect-MsolService -Credential $Cred 

  

$Groups = Get-UnifiedGroup | Where-Object {$_.WhenCreated ge ((Get-Date).AddDays(-1)) -and ($_.AccessType eq “Public”)}
 

if ($Groups eq $null){

 

“”

 

} else {

$Body = $Groups | select Alias, AccessType, ManagedBy, SharePointSiteURL | fl 

$Body = $Body | Out-String 

$Body = “The following PUBLIC groups were created in the last 24 hours. Please check with group owner that this is necessary.`n + $Body

 

Send-MailMessage -Body $Body -From Office365Reports@yourdom.ain -Subject “Office 365 Teams and Groups created in the last day” -To admin@yourdom.ain  

 SmtpServer SMTP.your.domain

 

} 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s