|
| 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 | +} |
0 commit comments