1+ <#
2+ . SYNOPSIS
3+ Adds users from a CSV file to the TeamViewer SSO inclusion list of their respective domain.
4+
5+ . DESCRIPTION
6+ The script fetches a list of SSO domains you have configured, loads the CSV file,
7+ checks for email addresses for each of your domains in the CSV, and adds them to the inclusion list of their respective domain.
8+ Email addresses not matching any of your domains will be skipped.
9+
10+ . PARAMETER ApiToken
11+ The TeamViewer API token to use.
12+ Must be a user access token.
13+ The token requires the following access permissions:
14+ `Manage SSO domains > View details about domains, add and remove email inclusions`
15+
16+ . PARAMETER Path
17+ Path of a csv file that contains the email addresses.
18+
19+ . PARAMETER HeaderName
20+ Column name where to find email addresses in the imported csv file.
21+
22+ . EXAMPLE
23+ $apiToken = 'SecretToken123' | ConvertTo-SecureString -AsPlainText -Force
24+ .\Add-SsoInclusionsFromCSV -Path 'c:\Example.csv' -HeaderName 'Email' -WhatIf
25+
26+ . NOTES
27+ This script requires the TeamViewerPS module to be installed.
28+ This can be done using the following command:
29+
30+ ```
31+ Install-Module TeamViewerPS
32+ ```
33+
34+ Copyright (c) 2019-2023 TeamViewer Germany GmbH
35+ See file LICENSE
36+ Version 2.0
37+ #>
38+
39+ [CmdletBinding (DefaultParameterSetName = ' Path' , SupportsShouldProcess = $true )]
40+ param (
41+ [Parameter (Mandatory = $true )]
42+ [securestring ] $ApiToken ,
43+
44+ [Parameter (Mandatory = $true )]
45+ [string ] $Path ,
46+
47+ [Parameter (Mandatory = $true )]
48+ [string ] $HeaderName
49+ )
50+
51+ if (-Not $MyInvocation.BoundParameters.ContainsKey (' ErrorAction' )) {
52+ $script :ErrorActionPreference = ' Stop'
53+ }
54+ if (-Not $MyInvocation.BoundParameters.ContainsKey (' InformationAction' )) {
55+ $script :InformationPreference = ' Continue'
56+ }
57+
58+ function Install-TeamViewerModule {
59+ if (! (Get-Module TeamViewerPS)) {
60+ Install-Module TeamViewerPS
61+ }
62+ }
63+
64+ function Add-SsoInclusionsFromCSV {
65+ [CmdletBinding (SupportsShouldProcess , ConfirmImpact = ' Medium' )]
66+ param ($Path , $HeaderName )
67+
68+ # Import email addresses from CSV
69+ $csvRows = Import-Csv - Path $Path
70+
71+ if ($csvRows.Count -eq 0 ) {
72+ Write-Information ' No entries found in CSV file!'
73+ exit
74+ }
75+ else {
76+ Write-Information " Found $ ( $csvRows.Count ) rows in CSV file."
77+ }
78+
79+ $emails = $csvRows | Select-Object - ExpandProperty $HeaderName
80+
81+ if ($emails.Count -eq 0 ) {
82+ Write-Information ' No valid email addresses found in CSV file!'
83+ exit
84+ }
85+ else {
86+ Write-Information " Found $ ( $emails.Count ) email addresses in CSV file."
87+ }
88+
89+ $domains = Get-TeamViewerSsoDomain - ApiToken $apiToken
90+
91+ if ($domains.Count -eq 0 ) {
92+ Write-Information ' No valid SSO domains found!'
93+ exit
94+ }
95+
96+ foreach ($domain in $domains ) {
97+ $domainUsers = $emails | Where-Object - FilterScript { $_.Split (' @' )[1 ] -eq $domain.Name }
98+
99+ Write-Information " Adding $ ( $domainUsers.Count ) email inclusions for $ ( $domain.Name ) ..."
100+
101+ if ($domainUsers.Count -gt 0 -And -Not $WhatIfPreference ) {
102+ Add-TeamViewerSsoInclusion - ApiToken $apiToken - DomainId $domain.Id - Email $domainUsers
103+
104+ Write-Information " Completed for domain $ ( $domain.Name ) ."
105+ }
106+ }
107+ }
108+
109+ if ($MyInvocation.InvocationName -ne ' .' ) {
110+ Install-TeamViewerModule
111+
112+ Add-SsoInclusionsFromCSV - Path $Path - HeaderName $HeaderName
113+ }
0 commit comments