33 Imports a set of users to a TeamViewer company.
44
55 . DESCRIPTION
6- The script imports and updates a set of users to the TeamViewer company that
7- corresponds to a given API token. By default, the users will be loaded from
8- a given CSV-formatted file. There is also an option to pipeline userdata to
9- this script.
10- In contrast to the definition of the "Import-" verb for Powershell, this
11- script does *NOT* import the users from TeamViewer to Powershell but
12- performs the reverse operation, by creating/updating TeamViewer users.
6+ The script imports and updates a set of users to the TeamViewer company that corresponds to a given API token.
7+ By default, the users will be loaded from a given CSV-formatted file. There is also an option to pipeline userdata to this script.
8+ In contrast to the definition of the "Import-" verb for Powershell, this script does *NOT* import the users from TeamViewer to Powershell, `
9+ but performs the reverse operation, by creating / updating TeamViewer users.
1310
1411 . PARAMETER ApiToken
1512 The TeamViewer API token to use.
1613 Must be a user access token.
17- The token requires the following access permissions:
18- - `User management: Create users, view users, edit users`
14+ The token requires the following access permissions: `User management: Create users, view users, edit users`
1915
2016 . PARAMETER Path
2117 Path to the CSV-formatted file to load the user data from.
2622 Cannot be used in combination with the `Path` CSV option.
2723
2824 . PARAMETER Delimiter
29- The optional delimiter that is used when loading CSV data from the given
30- file path. Only works in combination with the `Path` option.
25+ The optional delimiter that is used when loading CSV data from the given file path.
26+ Only works in combination with the `Path` option.
3127
3228 . PARAMETER DefaultUserLanguage
33- The fallback language code used for creating new users. This will be used
34- for the welcome email. This value is only considered if not given in the CSV
35- or pipeline user data.
29+ The fallback language code used for creating new users. This will be used for the welcome email.
30+ This value is only considered if not given in the CSV or pipeline user data.
3631
3732 . PARAMETER DefaultUserPassword
38- The fallback user password used for creating new users. This value is only
39- considered if not given in the CSV or pipeline user data.
33+ The fallback user password used for creating new users. This value is only considered if not given in the CSV or pipeline user data.
4034
4135 . PARAMETER DefaultUserPermissions
42- The fallback user permissions used for creating new users. This value is
43- only considered if not given in the CSV or pipeline user data.
44- Must be a comma-separated list of user permissions.
45- See the "TeamViewer API Documentation" for valid inputs.
36+ The fallback user permissions used for creating new users. This value is only considered if not given in the CSV or pipeline user data.
37+ Must be a comma-separated list of user permissions, see the "TeamViewer API Documentation" for valid inputs.
4638
4739 . PARAMETER DefaultSsoCustomerId
48- The fallback SSO customer ID, used for creating new users that are already
49- enabled and activated for SSO logins. This value is only considered if not
50- given in the CSV or pipeline user data.
40+ The fallback SSO customer ID, used for creating new users that are already enabled and activated for SSO logins.
41+ This value is only considered if not given in the CSV or pipeline user data.
5142
5243 . EXAMPLE
5344 .\Import-TeamViewerUser 'example.csv'
7970 Install-Module TeamViewerPS
8071 ```
8172
82- Copyright (c) 2019-2021 TeamViewer GmbH
83- See file LICENSE.txt
84- Version 2.0
73+ Copyright (c) 2019-2023 TeamViewer Germany GmbH
74+ See file LICENSE
75+ Version 2.1
8576#>
8677
8778[CmdletBinding (DefaultParameterSetName = ' File' , SupportsShouldProcess = $true )]
@@ -111,25 +102,43 @@ param(
111102 [securestring ] $DefaultSsoCustomerId
112103)
113104
114- if (-Not $MyInvocation.BoundParameters.ContainsKey (' ErrorAction' )) { $script :ErrorActionPreference = ' Stop' }
115- if (-Not $MyInvocation.BoundParameters.ContainsKey (' InformationAction' )) { $script :InformationPreference = ' Continue' }
105+ if (-Not $MyInvocation.BoundParameters.ContainsKey (' ErrorAction' )) {
106+ $script :ErrorActionPreference = ' Stop'
107+ }
108+
109+ if (-Not $MyInvocation.BoundParameters.ContainsKey (' InformationAction' )) {
110+ $script :InformationPreference = ' Continue'
111+ }
116112
117- function Install-TeamViewerModule { if (! (Get-Module TeamViewerPS)) { Install-Module TeamViewerPS } }
113+ function Install-TeamViewerModule {
114+ Write-Information ' Checking for TeamViewerPS.'
115+
116+ if (! (Get-Module TeamViewerPS)) {
117+ Write-Information ' Installing TeamViewerPS.'
118+
119+ Install-Module TeamViewerPS
120+ }
121+ }
118122
119123function Import-TeamViewerUser {
120124 Begin {
121- Write-Information " Checking connection to TeamViewer API."
125+ Write-Information ' Checking connection to TeamViewer web API.'
126+
122127 if (! (Invoke-TeamViewerPing $ApiToken )) {
123- Write-Error " Failed to contact TeamViewer API. Token or connection problem."
128+ Write-Error ' Failed to contact TeamViewer web API. Token or connection problem.'
124129 }
130+
125131 $statistics = @ { Created = 0 ; Updated = 0 ; Failed = 0 ; }
126132 $stopwatch = [System.Diagnostics.Stopwatch ]::StartNew()
127133 }
128134 Process {
129- if (! $_ ) { return }
135+ if (! $_ ) {
136+ return
137+ }
130138
131139 # Convert the input object to a hashtable
132140 $user = $_
141+
133142 if (! ($_ -is [System.Collections.Hashtable ]) -and $_ -is [psobject ]) {
134143 $user = @ { }
135144 $_.psobject.Properties | ForEach-Object { $user ." $ ( $_.Name ) " = $_.Value } | Out-Null
@@ -138,15 +147,19 @@ function Import-TeamViewerUser {
138147 try {
139148 # Check if the user already exists on the TeamViewer-side
140149 $existingUser = (Get-TeamViewerUser - ApiToken $ApiToken - Email $user.email )
150+
141151 if ($existingUser ) {
142152 # Update the existing user.
143153 Write-Information " User with email '$ ( $user.email ) ' found. Updating user."
154+
144155 Set-TeamViewerUser - ApiToken $ApiToken - User $existingUser - Property $user | Out-Null
156+
145157 $statistics.Updated ++
146158 }
147159 else {
148160 # Create a new user
149161 Write-Information " No user with email '$ ( $user.email ) ' found. Creating user."
162+
150163 $additionalParameters = @ {}
151164
152165 if ($user.password ) {
@@ -180,29 +193,35 @@ function Import-TeamViewerUser {
180193 $additionalParameters [' SsoCustomerIdentifier' ] = $DefaultSsoCustomerId
181194 }
182195
183- New-TeamViewerUser `
184- - ApiToken $ApiToken `
185- - Name $user.name `
186- - Email $user.email `
187- @additionalParameters | Out-Null
196+ New-TeamViewerUser - ApiToken $ApiToken - Name $user.name - Email $user.email @additionalParameters | Out-Null
197+
188198 $statistics.Created ++
189199 }
190200 }
191201 catch {
192202 Write-Information " Failed to process user with email '$ ( $user.email ) ': $_ "
203+
193204 $statistics.Failed ++
194205 }
195206 }
196207 End {
197208 # Output some statistics
198209 $stopwatch.Stop ()
199210 $statistics.Duration = $stopwatch.Elapsed
211+
200212 Write-Output $statistics
201213 }
202214}
203215
204216if ($MyInvocation.InvocationName -ne ' .' ) {
205217 Install-TeamViewerModule
206- $Users = if ($Path ) { Get-Content $Path | ConvertFrom-Csv - Delimiter $Delimiter } else { $Users }
218+
219+ $Users = if ($Path ) {
220+ Get-Content $Path | ConvertFrom-Csv - Delimiter $Delimiter
221+ }
222+ else {
223+ $Users
224+ }
225+
207226 $Users | Import-TeamViewerUser
208227}
0 commit comments