Skip to content

Commit 9b21a8e

Browse files
Merge pull request #29 from teamviewer/TEAM-59155_Publish_inclusion_list_import_script
add new script Add-SsoInclusionsFromCSV
2 parents b914d51 + 2bb48d3 commit 9b21a8e

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) 2019-2024 TeamViewer Germany GmbH
2+
# See file LICENSE
3+
4+
BeforeAll {
5+
$testApiToken = [securestring]@{}
6+
. "$PSScriptRoot\Add-SsoInclusionsFromCSV.ps1" -ApiToken $testApiToken -Path 'testPath' -HeaderName 'Email' -InformationAction 'SilentlyContinue'
7+
8+
Mock Invoke-TeamViewerPing { $true }
9+
Mock Get-TeamViewerSsoDomain { @(
10+
[PSCustomObject]@{Id = '9602c5f4-2779-4f9a-80e8-4829531789fe'; Name = 'example1.org' },
11+
[PSCustomObject]@{Id = '33ad81bb-e88b-46d0-92e1-b4f1663abf31'; Name = 'example2.org' }
12+
)
13+
}
14+
Mock Add-TeamViewerSsoInclusion {}
15+
16+
Mock Import-Csv { ConvertFrom-Csv -InputObject @'
17+
"EMail","Name"
18+
19+
20+
21+
22+
23+
24+
'@
25+
}
26+
27+
function Resolve-TeamViewerSsoDomainId {
28+
param(
29+
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
30+
[object]
31+
$Domain
32+
)
33+
Process {
34+
if ($Domain.PSObject.TypeNames -contains 'TeamViewerPS.SsoDomain') {
35+
return [guid]$Domain.Id
36+
}
37+
elseif ($Domain -is [string]) {
38+
return [guid]$Domain
39+
}
40+
elseif ($Domain -is [guid]) {
41+
return $Domain
42+
}
43+
else {
44+
throw "Invalid SSO domain identifier '$Domain'. Must be either a [TeamViewerPS.SsoDomain], [guid] or [string]."
45+
}
46+
}
47+
}
48+
}
49+
50+
Describe 'Add-SsoInclusionsFromCSV' {
51+
52+
It 'Should blah' {
53+
Add-SsoInclusionsFromCSV -Path 'example.csv' -HeaderName 'Email'
54+
55+
Assert-MockCalled Get-TeamViewerSsoDomain -Times 1 -Scope It
56+
Assert-MockCalled Add-TeamViewerSsoInclusion -Times 1 -Scope It -ParameterFilter {
57+
$ApiToken -eq $testApiToken -And $DomainId -eq [guid]'9602c5f4-2779-4f9a-80e8-4829531789fe' -And $Email.Count -eq 2 }
58+
Assert-MockCalled Add-TeamViewerSsoInclusion -Times 1 -Scope It -ParameterFilter {
59+
$ApiToken -eq $testApiToken -And $DomainId -eq [guid]'33ad81bb-e88b-46d0-92e1-b4f1663abf31' -And $Email.Count -eq 3 }
60+
}
61+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

Add-SsoInclusionsFromCSV/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Add-SsoInclusionsFromCSV
2+
3+
Adds users from a CSV file to the TeamViewer SSO inclusion list of their respective domain.
4+
5+
## Prerequisites
6+
7+
This script requires the `TeamViewerPS` powershell module to be installed.
8+
9+
```powershell
10+
Install-Module TeamViewerPS
11+
```
12+
13+
## Examples
14+
15+
### Import users from a CSV file
16+
17+
```powershell
18+
.\Add-SsoInclusionsFromCSV -Path 'c:\Example.csv' -HeaderName 'Email'
19+
```
20+
21+
### Import users from a CSV file and use the given API token
22+
23+
```powershell
24+
$apiToken = 'SecretToken123' | ConvertTo-SecureString -AsPlainText -Force
25+
.\Add-SsoInclusionsFromCSV -Path 'c:\Example.csv' -HeaderName 'Email'
26+
```
27+
28+
### Run the import script in "Test Mode" to see the changes that would be made
29+
30+
```powershell
31+
.\Add-SsoInclusionsFromCSV -Path 'c:\Example.csv' -HeaderName 'Email' -WhatIf
32+
```
33+
34+
## More help
35+
36+
To get further help about the script and its parameters, execute the
37+
`Get-Help` PowerShell cmdlet:
38+
39+
```powershell
40+
Get-Help -Detailed .\Add-SsoInclusionsFromCSV.ps1
41+
```

Add-SsoInclusionsFromCSV/example.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Email,Name
2+
3+

0 commit comments

Comments
 (0)