Automate connecting to Office 365 with PowerShell

[vc_row][vc_column][vc_column_text]Connecting to Office 365 with PowerShell enables you to perform many functions not available in the GUI.  But what if you want to automate these functions.  Connecting to Office 365 with PowerShell will prompt you for credentials for that session.  Now credentials can be saved in the script but that would be a security issue having your password saved in a file.  The way around that would be to save your password as a secure string in a text file.  And that is easy:

Read-Host -Prompt “Enter your password” -AsSecureString | ConvertFrom-SecureString | Out-File “C:\scripts\cred.txt

Your password will be encrypted and look like this:

$AdminName = “admin@domain.onmicrosoft.com”
$Pass = Get-Content “c:\scripts\cred.txt” | ConvertTo-SecureString
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$Session = New-PSSession`
-ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/`
-Credential $cred -Authentication Basic -AllowRedirection

Import-PSSession $Session

After the connection part you can add your desired function to the script.  Let’s say for instance you want to export a list of shared mailboxes.

Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited | Select DisplayName | sort DisplayName | Export-Csv c:\scripts\SharedMailboxes.csv -NoTypeInformation

**This illustrates how an O365 administrator can introduce automation in scripts that they own and control individually. For scheduling automation tasks it is recommended to use a dedicated service account.[/vc_column_text][/vc_column][/vc_row]

Share:

Facebook
Twitter
LinkedIn
Picture of Matt Tinney

Matt Tinney

Professional IT executive & business leader having decades of experience with Microsoft technologies delivering modern-day cloud & security solutions.

Contact Us

=
On Key

More Posts

WME Cybersecurity Briefings No. 020
Cyber Security

WME Security Briefing 26 July 2024

Pro-Houthi Group Targets Yemen Aid Organizations with Android Spyware Overview A suspected pro-Houthi group, OilAlpha, is targeting humanitarian organizations in Yemen with advanced Android spyware. The operation is associated with the activity cluster codenamed OilAlpha. It

Read More »
WME Cybersecurity Briefings No. 019
Cyber Security

WME Security Briefing 23 July 2024

Samba File Shares Targeted by DarkGate Malware in Recent Cyber Offensive Overview Recent investigations by Palo Alto Networks uncover a brief but significant cyberattack campaign utilizing DarkGate malware. This malicious software exploited Samba file shares to

Read More »
WME Cybersecurity Briefings No. 018
Cyber Security

WME Security Briefing 15 July 2024

OVHcloud Mitigates Record-Breaking 840 Million PPS DDoS Attack Overview In April 2024, OVHcloud, a top French cloud computing firm, successfully stopped a massive DDoS attack. The attack hit a record-breaking rate of 840 million packets per second

Read More »
E-Commerce Security - Solutions for Online Retailers
Azure

E-commerce Security – Solutions for Online Retailers

Today’s hyper-charged e-commerce landscape demands top-notch cybersecurity measures. Cybersecurity for this bustling sector isn’t just about ticking a technical box; it’s the cornerstone of building trust. As businesses and consumers flock to the online space, the

Read More »
Be assured of everything

Get WME Services

Stay ahead of the competition with our Professional IT offerings.

=