This repository was archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathProvisionAxDeveloper.ps1
More file actions
215 lines (182 loc) · 6.75 KB
/
ProvisionAxDeveloper.ps1
File metadata and controls
215 lines (182 loc) · 6.75 KB
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
<#
.SYNOPSIS
Script used to provision users to use Dynamics AX Developer Tools.
.DESCRIPTION
Script used to provision users to use Dynamics AX Developer Tools. The current user must be part of Administrators group to run this script as well to use
Dynamics AX Developer tools. This script creates a Sql Server logins with user[s] provided as arguments and creates corresponding user[s] for Dynamics Xref
database.
.NOTES
Name: ProvisionAxDeveloper
Author: Microsoft
DateCreated: 11Jan2016
.EXAMPLE
ProvisionAxDeveloper.ps1 <dbServerName> <domain or hostname\user1>,<domain or hostname\user2>,<domain or hostname\user3>...
Description
-----------
The user who runs this command must be an administrator. On running this command we will check if the users given in the arguments
are part of administrators group. If the check passes we will go provision the Dynamics AX Developer Tools for all provided users. If any
of the users are not part of administrators group we will fail the script in the validation phase itself.
Disclaimer:
This code is made available AS IS and is not supported by Microsoft.
The risk of the use or the results from the use of this code remains with the user.
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[string] $databaseServerName,
[Parameter(Mandatory=$True)]
[string[]] $users
)
$AdminUsers = {}
#
# Check if the current user has admin privileges. User must be an administrator or part of builtin\administrators group
#
Try
{
$AdminUsers = invoke-command { net localgroup administrators | where {$_ -AND $_ -notmatch "command completed successfully"} | select -skip 4 }
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
$userName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
If(($principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) -eq $false) -AND ($AdminUsers -contains $userName -eq $false ))
{
Write-Host "You must be an administrator to run this script"
return -1
}
}
Catch
{
$ErrorRecord = $Error[0]
$ErrorRecord | Format-List * -Force
$ErrorRecord.InvocationInfo |Format-List *
$Exception = $ErrorRecord.Exception
For ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
{ "$i" * 80
$Exception |Format-List * -Force
}
Throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_
}
If($PSBoundParameters.Count -lt 1)
{
Write-Host "Usage: \n PrepareAxTools.ps1 <user1>,<user2>,<user3>...\n Users must be part of Administrators group"
return -1
}
$AdminUsers = Invoke-command { net localgroup administrators | where {$_ -AND $_ -notmatch "command completed successfully"} | select -skip 4 }
#
# Validate if the user[s] argument are part of Administrators group
#
#Begin Validation
$quit = $false
Foreach ($user in $users)
{
$userNameComponents = $user.Split('\')
$username = ''
$domain = ''
If($userNameComponents.Count -eq 2)
{
$domain = $userNameComponents[0]
$username = $userNameComponents[1]
#
# For the local user accounts, windows does not store the Computer Name in the administrators user group.
#
If($domain -eq $env:computername)
{
$user = $username
}
}
Else
{
Write-Host "Invalid format. User name must of format 'domain or hostname\username'"
return -1
}
If(-NOT ($AdminUsers -contains $user))
{
Write-Host $user "is not part of Administrators group."
$quit = $true
}
}
If($quit -eq $true)
{
Write-Host "Users must be part of Administrators group. Please add the user[s] to builtin\Administrators group and re-run the script"
return -1
}
#End Validation
#
# Provision SQL access to the users
#
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
$databaseServerName = $env:databaseServerName
$ManagedComputer = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $databaseServerName
$serverInstance = ""
#
# Provision user access
#
#Begin Provision
Foreach($user in $users)
{
Try
{
$sqlSrv = New-Object 'Microsoft.SqlServer.Management.Smo.Server' "$databaseServerName"
$login = $sqlSrv.Logins.Item($user)
$dbName = "DYNAMICSXREFDB"
$database = $sqlSrv.Databases[$dbName]
$dbRoleName = "db_owner"
$dbRole = $database.Roles[$dbRoleName]
If(-Not ($login))
{
$login = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Login -ArgumentList $sqlSrv, $user
$login.LoginType = "WindowsUser"
$login.Create()
}
else
{
Write-Host "User $user already exists"
}
If(-Not ($login.IsMember("sysadmin")))
{
$login.AddToRole("sysadmin")
$login.Alter()
$sqlSrv.Refresh()
}
else
{
Write-Host "User $user is already a member of sysadmin"
}
If(-Not $database.Users[$user] )
{
#
# Map the user to database
#
$sql = "CREATE USER `"$user`" FOR LOGIN `"$user`" WITH DEFAULT_SCHEMA=[dbo];
EXEC sp_addrolemember 'db_owner', `"$user`""
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "server=$databaseServerName;integrated security=TRUE;database=$dbName"
$sqlConnection.Open()
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandTimeout = 120
$sqlCommand.Connection = $sqlConnection
$sqlCommand.CommandText= $sql
$text = $sql.Substring(0, 50)
Write-Progress -Activity "Executing SQL" -Status "Executing SQL => $text..."
Write-Host "Executing SQL => $text..."
$result = $sqlCommand.ExecuteNonQuery()
$sqlConnection.Close()
}
else
{
Write-Host "User $user is already mapped to database $database"
}
}
Catch
{
$ErrorRecord = $Error[0]
$ErrorRecord | Format-List * -Force
$ErrorRecord.InvocationInfo |Format-List *
$Exception = $ErrorRecord.Exception
for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
{ "$i" * 80
$Exception |Format-List * -Force
}
Throw "Failed to provision database access for the user: $user"
}
}
#End Provision