Skip to content

Commit 3d762e9

Browse files
authored
add new script Add-SsoExclusionsFromCSV (#24)
add new script Add-SsoExclusionsFromCSV that allows to add emails to SSO exclusion list from given CSV file
1 parent cf38cd5 commit 3d762e9

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (c) 2019-2021 TeamViewer GmbH
2+
# See file LICENSE.txt
3+
4+
BeforeAll {
5+
$testApiToken = [securestring]@{}
6+
. "$PSScriptRoot\Add-SsoExclusionsFromCSV.ps1" `
7+
-ApiToken $testApiToken `
8+
-csvPAth "testPath" `
9+
-HeaderName "Email" `
10+
-InformationAction 'SilentlyContinue'
11+
12+
Mock Invoke-TeamViewerPing { $true }
13+
Mock Get-TeamViewerSsoDomain { @(
14+
[PSCustomObject]@{Id = '9602c5f4-2779-4f9a-80e8-4829531789fe'; Name = "example1.org" },
15+
[PSCustomObject]@{Id = '33ad81bb-e88b-46d0-92e1-b4f1663abf31'; Name = "example2.org" }
16+
)
17+
}
18+
Mock Add-TeamViewerSsoExclusion {}
19+
20+
Mock Import-Csv { ConvertFrom-Csv -InputObject @'
21+
"EMail","Name"
22+
"user1@example1.org","User1"
23+
"user2@example1.org","User2"
24+
"user3@example2.org","User3"
25+
"user4@example2.org","User4"
26+
"user5@example2.org","User5"
27+
"user5@example3.org","User6"
28+
'@
29+
}
30+
31+
function Resolve-TeamViewerSsoDomainId {
32+
param(
33+
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
34+
[object]
35+
$Domain
36+
)
37+
Process {
38+
if ($Domain.PSObject.TypeNames -contains 'TeamViewerPS.SsoDomain') {
39+
return [guid]$Domain.Id
40+
}
41+
elseif ($Domain -is [string]) {
42+
return [guid]$Domain
43+
}
44+
elseif ($Domain -is [guid]) {
45+
return $Domain
46+
}
47+
else {
48+
throw "Invalid SSO domain identifier '$Domain'. Must be either a [TeamViewerPS.SsoDomain], [guid] or [string]."
49+
}
50+
}
51+
}
52+
}
53+
54+
Describe 'Add-SsoExclusionsFromCSV' {
55+
56+
It 'Should blah' {
57+
Add-SsoExclusionsFromCSV -csvPath "example.csv" -HeaderName "Email"
58+
59+
Assert-MockCalled Get-TeamViewerSsoDomain -Times 1 -Scope It
60+
Assert-MockCalled Add-TeamViewerSsoExclusion -Times 1 -Scope It -ParameterFilter {
61+
$ApiToken -eq $testApiToken -And `
62+
$DomainId -eq [guid]'9602c5f4-2779-4f9a-80e8-4829531789fe' -And `
63+
$Email.Count -eq 2 }
64+
Assert-MockCalled Add-TeamViewerSsoExclusion -Times 1 -Scope It -ParameterFilter {
65+
$ApiToken -eq $testApiToken -And `
66+
$DomainId -eq [guid]'33ad81bb-e88b-46d0-92e1-b4f1663abf31' -And `
67+
$Email.Count -eq 3 }
68+
}
69+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<#
2+
.SYNOPSIS
3+
Adds users from provided CSV file to SSO exclusion 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+
will check for email addresses for each of your domains in the CSV and add them to the exclusion list of their repective domain
8+
emails not matching to 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 exclusions`
15+
16+
.PARAMETER CSVPath
17+
Path of .csv file that contains the emails
18+
19+
.PARAMETER HeaderName
20+
Column name where to find emails in imported csv file
21+
22+
.EXAMPLE
23+
$apiToken = 'SecretToken123' | ConvertTo-SecureString -AsPlainText -Force
24+
.\Add-SsoExclusionsFromCSV -csvPath 'c:\ps playground\test.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 GmbH
35+
See file LICENSE.txt
36+
Version 2.0
37+
#>
38+
39+
[CmdletBinding(DefaultParameterSetName = "csvPath", SupportsShouldProcess = $true)]
40+
param(
41+
[Parameter(Mandatory = $true)]
42+
[securestring] $ApiToken,
43+
44+
[Parameter(Mandatory = $true)]
45+
[string] $csvPath,
46+
47+
[Parameter(Mandatory = $true)]
48+
[string] $HeaderName
49+
)
50+
51+
if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) { $script:ErrorActionPreference = 'Stop' }
52+
if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) { $script:InformationPreference = 'Continue' }
53+
54+
function Install-TeamViewerModule { if (!(Get-Module TeamViewerPS)) { Install-Module TeamViewerPS } }
55+
56+
function Add-SsoExclusionsFromCSV {
57+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
58+
param($csvPath, $HeaderName)
59+
60+
#import emails fom csv
61+
$csvRows = Import-Csv -Path $csvPath
62+
63+
if ($csvRows.Count -eq 0) {
64+
Write-Information "No entries found in CSV file"
65+
exit
66+
}
67+
else {
68+
Write-Information "Found $($csvRows.Count) rows in CSV file"
69+
}
70+
71+
$emails = $csvRows | Select-Object -ExpandProperty $HeaderName
72+
73+
if ($emails.Count -eq 0) {
74+
Write-Information "No valid emails found in CSV file"
75+
exit
76+
}
77+
else {
78+
Write-Information "Found $($emails.Count) emails in CSV file"
79+
}
80+
81+
$domains = Get-TeamViewerSsoDomain -ApiToken $apiToken
82+
83+
if ($domains.Count -eq 0) {
84+
Write-Information "No valid sso domains found"
85+
exit
86+
}
87+
88+
foreach ($domain in $domains) {
89+
$domainUsers = $emails | Where-Object -FilterScript { $_.Split("@")[1] -eq $domain.Name }
90+
Write-Information "Adding $($domainUsers.Count) email exclusions for $($domain.Name)"
91+
if ($domainUsers.Count -gt 0 -And -Not $WhatIfPreference) {
92+
Add-TeamViewerSsoExclusion -ApiToken $apiToken -DomainId $domain.Id -Email $domainUsers
93+
Write-Information "Completed for domain $($domain.Name)"
94+
}
95+
}
96+
}
97+
98+
if ($MyInvocation.InvocationName -ne '.') {
99+
Install-TeamViewerModule
100+
Add-SsoExclusionsFromCSV -csvPath $csvPath -HeaderName $HeaderName
101+
}

Add-SsoExclusionsFromCSV/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Add-SsoExclusionsFromCSV
2+
3+
Adds users from provided CSV file to SSO exclusion 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-SsoExclusionsFromCSV -csvPath 'c:\ps playground\test.csv' -HeaderName 'Email'
19+
```
20+
21+
### Import users from a CSV file that uses semi-colon as delimiter. Use the given API token
22+
23+
```powershell
24+
$apiToken = 'SecretToken123' | ConvertTo-SecureString -AsPlainText -Force
25+
.\Add-SsoExclusionsFromCSV -csvPath 'c:\ps playground\test.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-SsoExclusionsFromCSV -csvPath 'c:\ps playground\test.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-SsoExclusionsFromCSV.ps1
41+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Email,Name
2+
user1@example.test,User1
3+
user2@example.test,User2

0 commit comments

Comments
 (0)