-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAXSQLStartupScript.psm1
More file actions
285 lines (255 loc) · 10.3 KB
/
AXSQLStartupScript.psm1
File metadata and controls
285 lines (255 loc) · 10.3 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#region Load Assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")
#endregion Load Assemblies
#region Get AX SQL Instance
Function Get-AXSQLInstance {
[CmdletBinding()]
PARAM(
[parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string[]] $Computername,
[parameter(Mandatory=$false)]
[string] $Instance
)
BEGIN {
Write-Verbose "Starting Get-AXSQLInstance"
if ( $null -eq $Computername ) {
$Computername = $ENV:COMPUTERNAME
}
}
PROCESS {
ForEach ( $Computer in $Computername ) {
Write-Verbose "Checking $Computer"
if ($PSBoundParameters.ContainsKey("Instance")) {
if ( @(".","(local)") -contains $Instance ) {
$InstanceName = $Computer
}
else {
$InstanceName = "{0}\{1}" -f $Computer, $InstanceName
}
}
if ( [string]::IsNullOrEmpty($InstanceName) ) {
$SQLInstances = Get-WMIObject win32_service -computername $Computer -ErrorAction SilentlyContinue | Where-Object { ($_.Name -eq "MSSQLSERVER" -or $_.Name -like "MSSQL$*") -and $_.State -eq "Running" }
ForEach ( $SQLInstance in $SQLInstances ) {
if ( $SQLInstance.Name -eq "MSSQLSERVER" ) {
Write-Verbose "Setting instance name to $Computer"
$InstanceName = $Computer
}
else {
$msg = "Non default instance, connecting to {0}" -f $SQLInstance.Name
Write-Verbose $msg
$InstanceName = $SQLInstance.Name -replace "MSSQL\$", "${Computer}\"
}
Write-Verbose "Connecting to $InstanceName"
New-Object Microsoft.SqlServer.Management.Smo.Server $InstanceName
}
}
else {
Write-Verbose "Instance $InstanceName was passed"
New-Object Microsoft.SqlServer.Management.Smo.Server $InstanceName
}
}
}
}
#endregion Get AX SQL Instance
#region Get AX Database
Function Get-AXDatabase {
[CmdletBinding()]
PARAM(
[parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Microsoft.SqlServer.Management.Smo.Server[]] $Instance,
[parameter(Mandatory=$false)]
[string[]] $Database
)
PROCESS {
ForEach ( $SQLServer in $Instance ) {
if ( $PSBoundParameters.ContainsKey("Database") ) {
$SQLServer.Databases | Where-Object { -not($_.IsSystemObject) -and ($Database -contains $_.Name) -and ($null -ne $_.Tables["SYSSERVERSESSIONS"])}
}
else {
$SQLServer.Databases | Where-Object { -not($_.IsSystemObject) -and $null -ne $_.Tables["SYSSERVERSESSIONS"] }
}
}
}
}
#endregion Get AX Database
#region Get AOS Connections
Function Get-AXConnectedAOS {
[CmdletBinding()]
PARAM(
[parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Microsoft.SqlServer.Management.Smo.Database[]] $Database
)
BEGIN {
# Alive = 1
# Drain = 2
$TSQL = "SELECT AOSID, INSTANCE_NAME FROM [SYSSERVERSESSIONS] WHERE STATUS IN (1,2)"
}
PROCESS {
ForEach ( $SQLDb in $Database ) {
$AOSDataTable = $SQLDb.ExecuteWithResults($TSQL).Tables[0].Rows
ForEach ($Record in $AOSDataTable) {
$AOSServer = $Record.AOSID.Split("@")[0]
$AOSInstance = $Record.INSTANCE_NAME
$InstanceSearchFilter = '%Ax32Serv.exe" {0}' -f $AOSInstance
$Service = Get-WmiObject Win32_Service -Computer $AOSServer -Filter "PathName like '$InstanceSearchFilter'" -ErrorAction SilentlyContinue
$Service
}
}
}
}
#endregion Get AOS Connections
#region Restart AX Instance
Function Restart-AXInstancesOnServer {
[CmdletBinding(SupportsShouldProcess=$True, ConfirmImpact="Medium")]
PARAM(
[parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Alias("Computer","Server")]
[string[]] $Computername,
[parameter(Mandatory=$false)]
[string] $Instance,
[parameter(Mandatory=$false)]
[string[]] $Database,
[switch]$Force
)
BEGIN {
#region initialize
if ( $null -eq $Computername ) {
Write-Verbose "Using local computer"
$Computername = $ENV:COMPUTERNAME
}
#endregion initialize
}
PROCESS {
#region process
ForEach ( $Computer in $Computername ) {
#region find SQL Instances
if ($PSBoundParameters.ContainsKey("Instance")) {
Write-Verbose "Checking $instance on $computername"
$SQLInstances = Get-AXSQLInstance -Computername $Computername -Instance $Instance
}
else {
Write-Verbose "Scanning for SQL Services on $Computername"
$SQLInstances = Get-AXSQLInstance -Computername $Computername
}
#endregion find SQL Instances
ForEach ( $SQLInstance in $SQLInstances ) {
#region find SQL Services
$ConnectedAOSs = @()
$InstanceName = $SQLInstance.InstanceName
$NetBIOSName = $SQLInstance.ComputerNamePhysicalNetBIOS
if ( [string]::IsNullOrEmpty($InstanceName) ) {
$InstanceName = $NetBIOSName
$ServiceName = "MSSQLSERVER"
}
else {
$ServiceName = 'MSSQL${0}' -f $InstanceName
}
$msg = "Checking {0} instance for Dynamics AX Databases" -f $InstanceName
Write-Verbose $msg
$SQLService = Get-Service -Computername $NetBIOSName $ServiceName
#endregion find SQL Services
#region find SQL Databases
if ( $PSBoundParameters.ContainsKey("Database") ) {
$AXDBs = Get-AXDatabase -Instance $SQLInstance -Database $Database
}
else {
$AXDBs = Get-AXDatabase -Instance $SQLInstance
}
#endregion find SQL Databases
#region find AOS Instances
ForEach ( $DB in $AXDBs ) {
$msg = "Found database {0}, checking for AOS instances." -f $DB.Name
Write-Verbose $msg
$ConnectedAOSs += Get-AXConnectedAOS $DB
}
if ( $ConnectedAOSs.Count -eq 0 ) {
Write-Verbose "Didn't identify any AOS instances for $InstanceName"
continue
}
else {
$msg = "AOSInstances Found: {0}" -f $ConnectedAOSs.Count
Write-Verbose $msg
}
#endregion find AOS Instances
#region Stop AOS Instances
ForEach ( $AOS in $ConnectedAOSs ) {
# Stop the services
if ( $Force -or $PSCmdlet.ShouldProcess($AOS.Name,"Stop service") ) {
$msg = "Stopping {0}" -f $AOS.Name
Write-Verbose $msg
$AOS.StopService()
}
}
#endregion Stop AOS Instances
#region Restart SQL
if ( $Force -or $PSCmdlet.ShouldProcess($SQLService.Name,"Restart service") ) {
$msg = "Stopping {0}" -f $ServiceName
Write-Verbose $msg
Restart-Service -InputObject $SQLService
}
#endregion Restart SQL
#region Start AOS Instances
ForEach ( $AOS in $ConnectedAOSs ) {
# Stop the services
if ( $Force -or $PSCmdlet.ShouldProcess($AOS.Name,"Start service") ) {
$msg = "Starting {0}" -f $AOS.Name
Write-Verbose $msg
$AOS.StartService()
}
}
#endregion Start AOS Instances
}
}
#endregion process
}
}
#region Restart AX Instance
Function Get-AXAOSInstanceReport {
[CmdletBinding()]
PARAM(
[parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[Alias("Computer","Server")]
[string[]] $Computername,
[parameter(Mandatory=$false)]
[string] $Instance,
[parameter(Mandatory=$false)]
[string[]] $Database
)
BEGIN {
#region initialize
if ( $null -eq $Computername ) {
Write-Verbose "Using local computer"
$Computername = $ENV:COMPUTERNAME
}
#endregion initialize
}
PROCESS {
#region process
if ( $PSBoundParameters.ContainsKey("Instance") ) {
$AXSQLInstances = Get-AXSQLInstance -Computername $Computername -Instance $Instance
}
else {
$AXSQLInstances = Get-AXSQLInstance -Computername $Computername
}
if ( $PSBoundParameters.ContainsKey("Database") ) {
$AXDBs = Get-AXDatabase -Instance $AXSQLInstances -Database $Database
}
else {
$AXDBs = Get-AXDatabase -Instance $AXSQLInstances
}
$ConnectedAOSs = Get-AXConnectedAOS $AXDBs
$ConnectedAOSs | select PSComputerName, Name, ProcessId, State, PathName | format-table -autosize
#endregion process
}
}