PowerShell is used to complete multiple tasks nowadays, for example, we can have an Azure Function that runs PowerShell. This is also applicable if you want someone that works with you on a project that you’ve done a task or something as been created. A good way is to create a specific channel on Microsoft Teams Team with this connector that informs us when the PowerShell has finished.
The first step is to create a connection on the channel that we want to have this message. In this sample, I’ll make a simple script to create a SharePoint Communication site which will then be printed on this Team with the link to the site.
- Go to the channel where you want this to write the message
- Click on the settings of the channel
- Select the Connectors
- Search or find the Incoming Webhook
- Create a name for this webhook, if you want to have an image to identify upload it and then click on Create.
- Copy the URL created it will be needed for the PowerShell of this example of Create a new site
- Click on Done
The webhook is now ready to receive our messages sent via PowerShell. Now copy the code below and adapt it to your case with the site that you intend to create and on $connectorUri replace with the URL saved when created the webhook
$adminURL ="https://contoso-admin.sharepoint.com/"
$siteURL ="https://contoso.sharepoint.com/sites/siteA"
$siteTitle ="siteA"
$connectorUri = 'The URL of Incoming Webhook'
Connect-PnPOnline -Url $adminURL -UseWebLogin
$site = New-PnPSite -Type CommunicationSite -Title $siteTitle -Url $siteURL -ErrorAction SilentlyContinue
if($site){
$body = '{"text":"Site Created: <a href=\"'+$siteURL+'\">'+$siteURL+'</a>"}'
}else {
$body = '{"text":"'+$site+'"}'
}
Invoke-RestMethod -Uri $connectorUri -Method Post -Body $body -ContentType 'application/json'
Conclusion
There are multiple scenarios like this where you can send a message to Microsoft Teams Webhook. You can even include the adaptive cards as shown on the sample from Lee Ford which I strongly recommend to have a look – https://www.lee-ford.co.uk/send-message-cards-with-microsoft-teams/
[…] Send a message to Teams channel with PowerShell using Webhook […]
Is it possible to send/attach a file to the Teams notification at the same time? Thanks
How to add webhook to sharepoint URL?