-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsTeamsProvisioning.ps1
118 lines (87 loc) · 4.42 KB
/
msTeamsProvisioning.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
param
([object]$WebhookData)
$VerbosePreference = 'continue'
#region Verify if Runbook is started from Webhook.
# If runbook was called from Webhook, WebhookData will not be null.
if ($WebHookData){
# Collect properties of WebhookData
$WebhookName = $WebHookData.WebhookName
$WebhookHeaders = $WebHookData.RequestHeader
$WebhookBody = $WebHookData.RequestBody
# Collect individual headers. Input converted from JSON.
$From = $WebhookHeaders.From
$Input = (ConvertFrom-Json -InputObject $WebhookBody)
Write-Verbose "WebhookBody: $Input"
Write-Output -InputObject ('Runbook started from webhook' -f $WebhookName, $From)
}
else
{
Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
}
#endregion
#Automatic Teams creation starts here
#Global variables
$tenantId = "yourTenantID"
$guestAccess = "$($Input.allowGuestAccess)"
$SPSite = $Input.SiteURL
$SPList = $Input.ListName
$SPListItemID = $Input.ListItemID
$teamsName = $Input.TeamsName
$teamsAlias = $teamsName -replace '[^a_-zA-Z0-9]', ''
Function Update-site{
#add channel folders to SharePoint using PnP PowerShell
$spoconn = Connect-PnPOnline –Url https://jh365dev.sharepoint.com/sites/$teamsAlias –Credentials (Get-AutomationPSCredential -Name 'YourAutomationAccount') -ReturnConnection
$newfolder = Add-PnPFolder -Name "General" -Folder "/Shared Documents"
$newfolder = Add-PnPFolder -Name "01 Planning" -Folder "/Shared Documents"
$newfolder = Add-PnPFolder -Name "02 Execution" -Folder "/Shared Documents"
$newfolder = Add-PnPFolder -Name "03 Final" -Folder "/Shared Documents"
#add to hubsite if needed
$hubassosiation = Add-PnPHubSiteAssociation -Site https://jh365dev.sharepoint.com/sites/$teamsAlias -HubSite "yourhubsiteURL"
#copy files to new Team channel if needed
$filecopy = Copy-PnPFile -SourceUrl /sites/templates/Shared%20Documents/Templates.docx -TargetUrl /sites/$teamsAlias/Shared%20Documents/General -Force -Confirm
} #End Update-site
#Connecting to O365
Connect-MicrosoftTeams -TenantId $tenantId -Credential (Get-AutomationPSCredential -Name 'YourAutomationAccount')
#Create new Team
$team = New-Team -MailNickName $teamsAlias -DisplayName $Input.TeamsDisplayName -Visibility Private
Add-TeamUser -GroupId $team.GroupId -User $Input.TeamsOwner -Role Owner
#Add channels
New-TeamChannel -GroupId $team.GroupId -DisplayName "01 Planning"
New-TeamChannel -GroupId $team.GroupId -DisplayName "02 Execution"
New-TeamChannel -GroupId $team.GroupId -DisplayName "03 Final"
#Teams created
Write-Output 'Teams created'
#call Update site function
Update-site
#Disabling Guest Access to Teams
Write-Output "GuestAccess allowed: $guestAccess"
if($guestAccess -eq "No")
{
try{
#importing AzureADPreview modules
Import-Module AzureADPreview
Connect-AzureAD -TenantId $tenantId -Credential (Get-AutomationPSCredential -Name 'YourAutomationAccount')
#Turn OFF guest access
$template = Get-AzureADDirectorySettingTemplate | ? {$_.displayname -eq "group.unified.guest"}
$settingsCopy = $template.CreateDirectorySetting()
$settingsCopy["AllowToAddGuests"]=$False
New-AzureADObjectSetting -TargetType Groups -TargetObjectId $team.GroupId -DirectorySetting $settingsCopy
#Verify settings
Get-AzureADObjectSetting -TargetObjectId $team.GroupId -TargetType Groups | fl Values
#reset $guestaccess flag
$guestAccess = "NA"
}
catch{
#Catch errors
Write-Output "An error occurred:"
Write-Output $_.Exception.Message
$spoconn = Connect-PnPOnline –Url $SPSite –Credentials (Get-AutomationPSCredential -Name 'YourAutomationAccount') -ReturnConnection -Verbose
$itemupdate = Set-PnPListItem -List $SPList -Identity $SPListItemID -Values @{"TeamsCreated" = "Error Occured setting GuestAccess"} -Connection $spoconn
}
}
#Updating SharePoint list item status
$spoconn = Connect-PnPOnline –Url $SPSite –Credentials (Get-AutomationPSCredential -Name 'YourAutomationAccount') -ReturnConnection -Verbose
$itemupdate = Set-PnPListItem -List $SPList -Identity $SPListItemID -Values @{"TeamsCreated" = "Success"; "Link" = "https://jh365dev.sharepoint.com/sites/$teamsAlias, Link"} -Connection $spoconn
Write-Output "All done.."
Disconnect-PnPOnline
Disconnect-MicrosoftTeams