-
Notifications
You must be signed in to change notification settings - Fork 10
/
Import-TeamViewerUser.ps1
227 lines (176 loc) · 7.81 KB
/
Import-TeamViewerUser.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<#
.SYNOPSIS
Imports a set of users to a TeamViewer company.
.DESCRIPTION
The script imports and updates a set of users to the TeamViewer company that corresponds to a given API token.
By default, the users will be loaded from a given CSV-formatted file. There is also an option to pipeline userdata to this script.
In contrast to the definition of the "Import-" verb for Powershell, this script does *NOT* import the users from TeamViewer to Powershell, `
but performs the reverse operation, by creating / updating TeamViewer users.
.PARAMETER ApiToken
The TeamViewer API token to use.
Must be a user access token.
The token requires the following access permissions: `User management: Create users, view users, edit users`
.PARAMETER Path
Path to the CSV-formatted file to load the user data from.
The file is expected to have a header-line, indicating column names.
.PARAMETER Users
Can be used to pipe userdata directly from the Powershell to this script.
Cannot be used in combination with the `Path` CSV option.
.PARAMETER Delimiter
The optional delimiter that is used when loading CSV data from the given file path.
Only works in combination with the `Path` option.
.PARAMETER DefaultUserLanguage
The fallback language code used for creating new users. This will be used for the welcome email.
This value is only considered if not given in the CSV or pipeline user data.
.PARAMETER DefaultUserPassword
The fallback user password used for creating new users. This value is only considered if not given in the CSV or pipeline user data.
.PARAMETER DefaultUserPermissions
The fallback user permissions used for creating new users. This value is only considered if not given in the CSV or pipeline user data.
Must be a comma-separated list of user permissions, see the "TeamViewer API Documentation" for valid inputs.
.PARAMETER DefaultSsoCustomerId
The fallback SSO customer ID, used for creating new users that are already enabled and activated for SSO logins.
This value is only considered if not given in the CSV or pipeline user data.
.EXAMPLE
.\Import-TeamViewerUser 'example.csv'
.EXAMPLE
.\Import-TeamViewerUser -Path 'example.csv' -Delimiter ';'
.EXAMPLE
$pwd = Read-Host -Prompt 'Enter default password' -AsSecureString
.\Import-TeamViewerUser 'example.csv' -DefaultUserPassword $pwd
.EXAMPLE
$users = @(
@{email = '[email protected]'; name = 'Test User 1'},
@{email = '[email protected]'; name = 'Test User 2'; password = 'AnotherPassword123'},
@{email = '[email protected]'; name = 'Test User 3'}
)
$pwd = Read-Host -Prompt 'Enter default password' -AsSecureString
$users | .\Import-TeamViewerUser -DefaultUserPassword $pwd
.EXAMPLE
.\Import-TeamViewerUser 'example.csv' -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 Germany GmbH
See file LICENSE
Version 2.1
#>
[CmdletBinding(DefaultParameterSetName = 'File', SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[securestring] $ApiToken,
[Parameter(ParameterSetName = 'File', Mandatory = $true, Position = 0)]
[string] $Path,
[Parameter(ParameterSetName = 'Pipeline', ValueFromPipeline = $true)]
[object[]] $Users,
[Parameter(ParameterSetName = 'File', Mandatory = $false)]
[char] $Delimiter = ',',
[Parameter(Mandatory = $false)]
[cultureinfo] $DefaultUserLanguage = 'en',
[Parameter(Mandatory = $false)]
[securestring] $DefaultUserPassword,
[Parameter(Mandatory = $false)]
[string[]] $DefaultUserPermissions = @('ShareOwnGroups', 'EditConnections', 'EditFullProfile', 'ViewOwnConnections'),
[Parameter(Mandatory = $false)]
[securestring] $DefaultSsoCustomerId
)
if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) {
$script:ErrorActionPreference = 'Stop'
}
if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) {
$script:InformationPreference = 'Continue'
}
function Install-TeamViewerModule {
Write-Information 'Checking for TeamViewerPS.'
if (!(Get-Module TeamViewerPS)) {
Write-Information 'Installing TeamViewerPS.'
Install-Module TeamViewerPS
}
}
function Import-TeamViewerUser {
Begin {
Write-Information 'Checking connection to TeamViewer web API.'
if (!(Invoke-TeamViewerPing $ApiToken)) {
Write-Error 'Failed to contact TeamViewer web API. Token or connection problem.'
}
$statistics = @{ Created = 0; Updated = 0; Failed = 0; }
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
}
Process {
if (!$_) {
return
}
# Convert the input object to a hashtable
$user = $_
if (!($_ -is [System.Collections.Hashtable]) -and $_ -is [psobject]) {
$user = @{ }
$_.psobject.Properties | ForEach-Object { $user."$($_.Name)" = $_.Value } | Out-Null
}
try {
# Check if the user already exists on the TeamViewer-side
$existingUser = (Get-TeamViewerUser -ApiToken $ApiToken -Email $user.email)
if ($existingUser) {
# Update the existing user.
Write-Information "User with email '$($user.email)' found. Updating user."
Set-TeamViewerUser -ApiToken $ApiToken -User $existingUser -Property $user | Out-Null
$statistics.Updated++
}
else {
# Create a new user
Write-Information "No user with email '$($user.email)' found. Creating user."
$additionalParameters = @{}
if ($user.password) {
$additionalParameters['Password'] = $user.password | ConvertTo-SecureString -AsPlainText -Force
}
elseif ($DefaultUserPassword) {
$additionalParameters['Password'] = $DefaultUserPassword
}
else {
$additionalParameters['WithoutPassword'] = $true
}
if ($user.permissions) {
$additionalParameters['Permissions'] = $user.permissions -split ','
}
elseif ($DefaultUserPermissions) {
$additionalParameters['Permissions'] = $DefaultUserPermissions
}
if ($user.language) {
$additionalParameters['Culture'] = [cultureinfo]$user.language
}
elseif ($DefaultUserLanguage) {
$additionalParameters['Culture'] = $DefaultUserLanguage
}
if ($user.sso_customer_id) {
$additionalParameters['SsoCustomerIdentifier'] = $user.sso_customer_id | ConvertTo-SecureString -AsPlainText -Force
}
elseif ($DefaultSsoCustomerId) {
$additionalParameters['SsoCustomerIdentifier'] = $DefaultSsoCustomerId
}
New-TeamViewerUser -ApiToken $ApiToken -Name $user.name -Email $user.email @additionalParameters | Out-Null
$statistics.Created++
}
}
catch {
Write-Information "Failed to process user with email '$($user.email)': $_"
$statistics.Failed++
}
}
End {
# Output some statistics
$stopwatch.Stop()
$statistics.Duration = $stopwatch.Elapsed
Write-Output $statistics
}
}
if ($MyInvocation.InvocationName -ne '.') {
Install-TeamViewerModule
$Users = if ($Path) {
Get-Content $Path | ConvertFrom-Csv -Delimiter $Delimiter
}
else {
$Users
}
$Users | Import-TeamViewerUser
}