Skip to content

Commit cf38cd5

Browse files
Merge pull request #22 from teamviewer/Import-TeamViewerUser_FormatAndScriptAnalyzer
Import-TeamViewerUser: format & ScriptAnalyzer
2 parents 5f56933 + ebe17a2 commit cf38cd5

File tree

4 files changed

+82
-53
lines changed

4 files changed

+82
-53
lines changed

Import-TeamViewerUser/Import-TeamViewerUser.Tests.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# Copyright (c) 2019-2021 TeamViewer GmbH
2-
# See file LICENSE.txt
1+
# Copyright (c) 2019-2023 TeamViewer Germany GmbH
2+
# See file LICENSE
33

44
BeforeAll {
55
$testApiToken = [securestring]@{}
6-
. "$PSScriptRoot\Import-TeamViewerUser.ps1" `
7-
-ApiToken $testApiToken `
8-
-Path "testPath" `
9-
-InformationAction 'SilentlyContinue'
6+
7+
. "$PSScriptRoot\Import-TeamViewerUser.ps1" -ApiToken $testApiToken -Path 'testPath' -InformationAction 'SilentlyContinue'
108

119
Mock Invoke-TeamViewerPing { $true }
1210
Mock Get-TeamViewerUser {}
@@ -20,16 +18,19 @@ BeforeAll {
2018
Describe 'Import-TeamViewerUser' {
2119
It 'Should check the connection to the TeamViewer API' {
2220
{ Import-TeamViewerUser } | Should -Not -Throw
21+
2322
Assert-MockCalled Invoke-TeamViewerPing -Times 1 -Scope It
2423
}
2524

2625
It 'Should abort if TeamViewer API is not reachable' {
2726
Mock Invoke-TeamViewerPing { $false }
27+
2828
{ Import-TeamViewerUser -ErrorAction Stop } | Should -Throw
2929
}
3030

3131
It 'Should check for existence of a user' {
3232
$testUser | Import-TeamViewerUser
33+
3334
Assert-MockCalled Get-TeamViewerUser -Times 1 -Scope It -ParameterFilter {
3435
$Email -And $Email -eq $testUser.email
3536
}
@@ -39,7 +40,7 @@ Describe 'Import-TeamViewerUser' {
3940
BeforeAll {
4041
Mock Get-TeamViewerUser {
4142
@{
42-
Id = 'u1234';
43+
Id = 'u1234'
4344
Email = $testUser.email
4445
Name = 'Old Name'
4546
}
@@ -51,6 +52,7 @@ Describe 'Import-TeamViewerUser' {
5152
$result | Should -Not -BeNullOrEmpty
5253
$result.Updated | Should -Be 1
5354
$result.Created | Should -Be 0
55+
5456
Assert-MockCalled Set-TeamViewerUser -Times 1 -Scope It -ParameterFilter {
5557
$User -And $User.Id -eq 'u1234' -And
5658
$Property -And $Property -eq $testUser
@@ -59,6 +61,7 @@ Describe 'Import-TeamViewerUser' {
5961

6062
It 'Should acknowledge errors and continue' {
6163
Mock Set-TeamViewerUser { Write-Error 'test failure' }
64+
6265
$result = ($testUser | Import-TeamViewerUser)
6366
$result | Should -Not -BeNullOrEmpty
6467
$result.Updated | Should -Be 0
@@ -73,6 +76,7 @@ Describe 'Import-TeamViewerUser' {
7376
$result | Should -Not -BeNullOrEmpty
7477
$result.Created | Should -Be 1
7578
$result.Updated | Should -Be 0
79+
7680
Assert-MockCalled New-TeamViewerUser -Times 1 -Scope It -ParameterFilter {
7781
$Email -And $Email -eq $testUser.email -And
7882
$Name -And $Name -eq $testUser.name -And
@@ -88,10 +92,12 @@ Describe 'Import-TeamViewerUser' {
8892
password = 'test123'
8993
sso_customer_id = 'foobar'
9094
}
95+
9196
$result = ($testUser2 | Import-TeamViewerUser)
9297
$result | Should -Not -BeNullOrEmpty
9398
$result.Created | Should -Be 1
9499
$result.Updated | Should -Be 0
100+
95101
Assert-MockCalled New-TeamViewerUser -Times 1 -Scope It -ParameterFilter {
96102
$Email -eq $testUser2.email -And
97103
$Name -eq $testUser2.name -And
@@ -103,6 +109,7 @@ Describe 'Import-TeamViewerUser' {
103109

104110
It 'Should acknowledge errors and continue' {
105111
Mock New-TeamViewerUser { Write-Error 'test failure' }
112+
106113
$result = ($testUser | Import-TeamViewerUser)
107114
$result | Should -Not -BeNullOrEmpty
108115
$result.Updated | Should -Be 0

Import-TeamViewerUser/Import-TeamViewerUser.ps1

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
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.
@@ -26,28 +22,23 @@
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'
@@ -79,9 +70,9 @@
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

119123
function 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

204216
if ($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
}

Import-TeamViewerUser/README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
Imports a set of users to a TeamViewer company.
44

5-
The script imports and updates a set of users to the TeamViewer company
6-
that corresponds to a given API token. By default, the users will be loaded from
7-
a given CSV-formatted file. There is also an option to pipeline userdata to this
8-
script. In contrast to the definition of the "Import-" verb for Powershell, this
9-
script does *NOT* import the users from TeamViewer to Powershell but performs
10-
the reverse operation, by creating/updating TeamViewer users.
5+
The script imports and updates a set of users to the TeamViewer company that corresponds to a given API token.
6+
By default, the users will be loaded from a given CSV-formatted file. There is also an option to pipeline userdata to this script.
7+
In contrast to the definition of the "Import-" verb for Powershell, this script does *NOT* import the users from TeamViewer to Powershell,
8+
but performs the reverse operation, by creating/updating TeamViewer users.
119

1210
## Prerequisites
1311

@@ -39,7 +37,7 @@ $pwd = ConvertTo-SecureString 'MyPassword123' -AsPlainText -Force
3937
.\Import-TeamViewerUser 'example.csv' -DefaultUserPassword $pwd
4038
```
4139

42-
### Import users from Powershell data. Use the given default password as fallback.
40+
### Import users from Powershell data. Use the given default password as fallback
4341

4442
```powershell
4543
$users = @(
@@ -51,7 +49,7 @@ $pwd = ConvertTo-SecureString 'MyPassword123' -AsPlainText -Force
5149
$users | .\Import-TeamViewerUser -DefaultUserPassword $pwd
5250
```
5351

54-
### Run the import script in "Test Mode" to see the changes that would be made.
52+
### Run the import script in "Test Mode" to see the changes that would be made
5553

5654
```powershell
5755
.\Import-TeamViewerUser 'example.csv' -WhatIf

PSScriptAnalyzerSettings.psd1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@{
2+
Severity = @('Error', 'Warning')
3+
# ToDo: Remove excludes after Import-TeamViewerUser was adapted
4+
ExcludeRules = @('PSUseProcessBlockForPipelineCommand', 'PSAvoidUsingConvertToSecureStringWithPlainText')
5+
}

0 commit comments

Comments
 (0)