-
Notifications
You must be signed in to change notification settings - Fork 10
add new script Add-SsoExclusionsFromCSV #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
Add-SsoExclusionsFromCSV/Add-SsoExclusionsFromCSV.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Copyright (c) 2019-2021 TeamViewer GmbH | ||
| # See file LICENSE.txt | ||
|
|
||
| BeforeAll { | ||
| $testApiToken = [securestring]@{} | ||
| . "$PSScriptRoot\Add-SsoExclusionsFromCSV.ps1" ` | ||
| -ApiToken $testApiToken ` | ||
| -csvPAth "testPath" ` | ||
| -HeaderName "Email" ` | ||
| -InformationAction 'SilentlyContinue' | ||
|
|
||
| Mock Invoke-TeamViewerPing { $true } | ||
| Mock Get-TeamViewerSsoDomain { @( | ||
| [PSCustomObject]@{Id = '9602c5f4-2779-4f9a-80e8-4829531789fe'; Name = "example1.org" }, | ||
| [PSCustomObject]@{Id = '33ad81bb-e88b-46d0-92e1-b4f1663abf31'; Name = "example2.org" } | ||
| ) | ||
| } | ||
| Mock Add-TeamViewerSsoExclusion {} | ||
|
|
||
| Mock Import-Csv { ConvertFrom-Csv -InputObject @' | ||
| "EMail","Name" | ||
| "user1@example1.org","User1" | ||
| "user2@example1.org","User2" | ||
| "user3@example2.org","User3" | ||
| "user4@example2.org","User4" | ||
| "user5@example2.org","User5" | ||
| "user5@example3.org","User6" | ||
| '@ | ||
| } | ||
|
|
||
| function Resolve-TeamViewerSsoDomainId { | ||
| param( | ||
| [Parameter(ValueFromPipeline = $true, Mandatory = $true)] | ||
| [object] | ||
| $Domain | ||
| ) | ||
| Process { | ||
| if ($Domain.PSObject.TypeNames -contains 'TeamViewerPS.SsoDomain') { | ||
| return [guid]$Domain.Id | ||
| } | ||
| elseif ($Domain -is [string]) { | ||
| return [guid]$Domain | ||
| } | ||
| elseif ($Domain -is [guid]) { | ||
| return $Domain | ||
| } | ||
| else { | ||
| throw "Invalid SSO domain identifier '$Domain'. Must be either a [TeamViewerPS.SsoDomain], [guid] or [string]." | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Describe 'Add-SsoExclusionsFromCSV' { | ||
|
|
||
| It 'Should blah' { | ||
| Add-SsoExclusionsFromCSV -csvPath "example.csv" -HeaderName "Email" | ||
|
|
||
| Assert-MockCalled Get-TeamViewerSsoDomain -Times 1 -Scope It | ||
| Assert-MockCalled Add-TeamViewerSsoExclusion -Times 1 -Scope It -ParameterFilter { | ||
| $ApiToken -eq $testApiToken -And ` | ||
| $DomainId -eq [guid]'9602c5f4-2779-4f9a-80e8-4829531789fe' -And ` | ||
| $Email.Count -eq 2 } | ||
| Assert-MockCalled Add-TeamViewerSsoExclusion -Times 1 -Scope It -ParameterFilter { | ||
| $ApiToken -eq $testApiToken -And ` | ||
| $DomainId -eq [guid]'33ad81bb-e88b-46d0-92e1-b4f1663abf31' -And ` | ||
| $Email.Count -eq 3 } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Adds users from provided CSV file to SSO exclusion list of their respective domain | ||
|
|
||
| .DESCRIPTION | ||
| The script fetches a list of SSO domains you have configured, loads the CSV file, | ||
| will check for email addresses for each of your domains in the CSV and add them to the exclusion list of their repective domain | ||
| emails not matching to any of your domains will be skipped | ||
|
|
||
| .PARAMETER ApiToken | ||
| The TeamViewer API token to use. | ||
| Must be a user access token. | ||
| The token requires the following access permissions: | ||
| - `Manage SSO domains > View details about domains, add and remove email exclusions` | ||
|
|
||
| .PARAMETER CSVPath | ||
| Path of .csv file that contains the emails | ||
|
|
||
| .PARAMETER HeaderName | ||
| Column name where to find emails in imported csv file | ||
|
|
||
| .EXAMPLE | ||
| $apiToken = 'SecretToken123' | ConvertTo-SecureString -AsPlainText -Force | ||
| .\Add-SsoExclusionsFromCSV -csvPath 'c:\ps playground\test.csv' -HeaderName 'Email' -WhatIf | ||
|
|
||
| .NOTES | ||
| This script requires the TeamViewerPS module to be installed. | ||
| This can be done using the following command: | ||
|
|
||
| ``` | ||
| Install-Module TeamViewerPS | ||
| ``` | ||
|
|
||
| Copyright (c) 2019-2023 TeamViewer GmbH | ||
| See file LICENSE.txt | ||
| Version 2.0 | ||
| #> | ||
|
|
||
| [CmdletBinding(DefaultParameterSetName = "csvPath", SupportsShouldProcess = $true)] | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [securestring] $ApiToken, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string] $csvPath, | ||
|
|
||
| [Parameter(Mandatory = $true)] | ||
| [string] $HeaderName | ||
| ) | ||
|
|
||
| if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) { $script:ErrorActionPreference = 'Stop' } | ||
| if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) { $script:InformationPreference = 'Continue' } | ||
|
|
||
| function Install-TeamViewerModule { if (!(Get-Module TeamViewerPS)) { Install-Module TeamViewerPS } } | ||
|
|
||
| function Add-SsoExclusionsFromCSV { | ||
| [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')] | ||
| param($csvPath, $HeaderName) | ||
|
|
||
| #import emails fom csv | ||
| $csvRows = Import-Csv -Path $csvPath | ||
|
|
||
| if ($csvRows.Count -eq 0) { | ||
| Write-Information "No entries found in CSV file" | ||
| exit | ||
| } | ||
| else { | ||
| Write-Information "Found $($csvRows.Count) rows in CSV file" | ||
| } | ||
|
|
||
| $emails = $csvRows | Select-Object -ExpandProperty $HeaderName | ||
|
|
||
| if ($emails.Count -eq 0) { | ||
| Write-Information "No valid emails found in CSV file" | ||
| exit | ||
| } | ||
| else { | ||
| Write-Information "Found $($emails.Count) emails in CSV file" | ||
| } | ||
|
|
||
| $domains = Get-TeamViewerSsoDomain -ApiToken $apiToken | ||
|
|
||
| if ($domains.Count -eq 0) { | ||
| Write-Information "No valid sso domains found" | ||
| exit | ||
| } | ||
|
|
||
| foreach ($domain in $domains) { | ||
| $domainUsers = $emails | Where-Object -FilterScript { $_.Split("@")[1] -eq $domain.Name } | ||
| Write-Information "Adding $($domainUsers.Count) email exclusions for $($domain.Name)" | ||
| if ($domainUsers.Count -gt 0 -And -Not $WhatIfPreference) { | ||
| Add-TeamViewerSsoExclusion -ApiToken $apiToken -DomainId $domain.Id -Email $domainUsers | ||
| Write-Information "Completed for domain $($domain.Name)" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($MyInvocation.InvocationName -ne '.') { | ||
| Install-TeamViewerModule | ||
| Add-SsoExclusionsFromCSV -csvPath $csvPath -HeaderName $HeaderName | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Add-SsoExclusionsFromCSV | ||
|
|
||
| Adds users from provided CSV file to SSO exclusion list of their respective domain | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| This script requires the `TeamViewerPS` powershell module to be installed. | ||
|
|
||
| ```powershell | ||
| Install-Module TeamViewerPS | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Import users from a CSV file | ||
|
|
||
| ```powershell | ||
| .\Add-SsoExclusionsFromCSV -csvPath 'c:\ps playground\test.csv' -HeaderName 'Email' | ||
| ``` | ||
|
|
||
| ### Import users from a CSV file that uses semi-colon as delimiter. Use the given API token | ||
|
|
||
| ```powershell | ||
| $apiToken = 'SecretToken123' | ConvertTo-SecureString -AsPlainText -Force | ||
| .\Add-SsoExclusionsFromCSV -csvPath 'c:\ps playground\test.csv' -HeaderName 'Email' | ||
| ``` | ||
|
|
||
| ### Run the import script in "Test Mode" to see the changes that would be made. | ||
|
|
||
| ```powershell | ||
| .\Add-SsoExclusionsFromCSV -csvPath 'c:\ps playground\test.csv' -HeaderName 'Email' -WhatIf | ||
| ``` | ||
|
|
||
| ## More help | ||
|
|
||
| To get further help about the script and its parameters, execute the | ||
| `Get-Help` PowerShell cmdlet: | ||
|
|
||
| ```powershell | ||
| Get-Help -Detailed .\Add-SsoExclusionsFromCSV.ps1 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Email,Name | ||
| user1@example.test,User1 | ||
| user2@example.test,User2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the example missing the api token? Or should this be in the CSV