-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd-UsersToStaffDB.ps1
37 lines (29 loc) · 1.22 KB
/
Add-UsersToStaffDB.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
$server = 'localhost\sqlexpress'
$databaseName = 'Personal'
# Suffix to be appended after each record in the command
$suffix = ", `r`n"
# Get users from AD
$users = Get-ADUser -Filter *
# Initialize command SQL INSERT INTO
<#
A string builder provides a much better performance when composing a string out of large number or partial strings
See https://powershellexplained.com/2017-11-20-Powershell-StringBuilder/ for some numbers
#>
$commandBuilder = [System.Text.StringBuilder]::new('INSERT INTO Personen (Name, Extension) VALUES ')
# Add Values for each user
foreach ($user in $users) {
# Replace single quote with two single quotes to escape character for SQL
$name = $user.Name -replace '''', ''''''
# Generate some random value
$extension = Get-Random
# Append values
$commandBuilder.Append("('{0}', '{1}')$suffix" -f ($name, $extension)) | Out-Null
}
# Remove last suffix
$commandBuilder.Remove($commandBuilder.Length - $suffix.Length, $suffix.Length) | Out-Null
# Generate string
$command = $commandBuilder.ToString()
# Execute command against SQL server
$connection = Connect-SqlServer -Database $databaseName -Server $server
Invoke-SqlCommand -Connection $connection -Command $command
$connection.Close()