Wednesday, June 2, 2021

Connecting to M365 via PowerShell using stored credentials

 

One of the scripts that I needed to write necessitated me to be able to connect to the M365 cloud via PowerShell, and do it by storing credentials within the script so that it can be automated. This piece of script allows you to do that. Substitute your own credentials in the script.

$username = "user@domain.com"
$password = ConvertTo-SecureString "Password” -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Connect-MicrosoftTeams -credential $psCred

To create a new Team using the script, you would put it together like this:

Install-Module -Name MicrosoftTeams -Force
$username = "user@domain.com"
$password = ConvertTo-SecureString "Password” -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Connect-MicrosoftTeams -credential $psCred



$group = New-Team -DisplayName "HANDS ON Teams" -Description "Team to post technical articles and blogs" -Visibility "Public"

Add-TeamUser -GroupId $group.GroupId -User "owner@domain.com" -Role "owner"
New-TeamChannel -GroupId $group.GroupId -DisplayName "Article Ideas"
New-TeamChannel -GroupId $group.GroupId -DisplayName "Social Media"

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.