Skip to content

Commit 8d2dbee

Browse files
Fix bugs introduced in GPT review commits
- ConvertTo-DbaTimeline: Add backslash escaping to DatabaseName for JS output - Compare-DbaLogin.Tests: Wrap result in @() for PS 5.1 .Count compatibility - Start-DbaMigration.Tests: Wrap Where-Object result in @() for PS 5.1 .Count - Update-ServiceStatus: Fix severely broken indentation throughout file Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 8657130 commit 8d2dbee

4 files changed

Lines changed: 121 additions & 121 deletions

File tree

private/functions/Update-ServiceStatus.ps1

Lines changed: 118 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -110,142 +110,142 @@ function Update-ServiceStatus {
110110
$cimObject = $service._CimObject
111111
if (($cimObject.State -eq "Running" -and $action -eq "start") -or ($cimObject.State -eq "Stopped" -and $action -eq "stop")) {
112112
$service | Add-Member -Force -NotePropertyName Status -NotePropertyValue "Successful" -PassThru |
113-
Add-Member -Force -NotePropertyName Message -NotePropertyValue "The service is already $actionText, no action required" -PassThru
114-
} elseif ($cimObject.StartMode -eq "Disabled" -and $action -in "start", "restart") {
115-
$service | Add-Member -Force -NotePropertyName Status -NotePropertyValue "Failed" -PassThru |
116-
Add-Member -Force -NotePropertyName Message -NotePropertyValue "The service is disabled and cannot be $actionText" -PassThru
117-
} else {
118-
$servicesToRestart += $service
113+
Add-Member -Force -NotePropertyName Message -NotePropertyValue "The service is already $actionText, no action required" -PassThru
114+
} elseif ($cimObject.StartMode -eq "Disabled" -and $action -in "start", "restart") {
115+
$service | Add-Member -Force -NotePropertyName Status -NotePropertyValue "Failed" -PassThru |
116+
Add-Member -Force -NotePropertyName Message -NotePropertyValue "The service is disabled and cannot be $actionText" -PassThru
117+
} else {
118+
$servicesToRestart += $service
119+
}
120+
} else {
121+
throw "Unknown object in pipeline - make sure to use Get-DbaService cmdlet"
122+
}
119123
}
120-
} else {
121-
throw "Unknown object in pipeline - make sure to use Get-DbaService cmdlet"
122-
}
123-
}
124-
#Set desired $action
125-
if ($action -in "start", "restart") {
126-
$methodName = "StartService"
127-
$desiredState = "Running"
128-
$undesiredState = "Stopped"
129-
} elseif ($action -eq "stop") {
130-
$methodName = "StopService"
131-
$desiredState = "Stopped"
132-
$undesiredState = "Running"
133-
}
134-
$invokeResults = @()
135-
foreach ($service in $servicesToRestart) {
136-
if ($Pscmdlet.ShouldProcess("Sending $action request to service $($service.ServiceName) on $($service.ComputerName)")) {
137-
# Get a fresh CIM instance via the DCOM session to avoid issues with deserialized
138-
# CIM objects crossing runspace boundaries without their session context.
139-
if ($cimSession) {
140-
try {
141-
$splatGetFreshCim = @{
142-
CimSession = $cimSession
143-
Namespace = "root\cimv2"
144-
Query = "SELECT * FROM Win32_Service WHERE Name = '$($service.ServiceName)'"
124+
#Set desired $action
125+
if ($action -in "start", "restart") {
126+
$methodName = "StartService"
127+
$desiredState = "Running"
128+
$undesiredState = "Stopped"
129+
} elseif ($action -eq "stop") {
130+
$methodName = "StopService"
131+
$desiredState = "Stopped"
132+
$undesiredState = "Running"
133+
}
134+
$invokeResults = @()
135+
foreach ($service in $servicesToRestart) {
136+
if ($Pscmdlet.ShouldProcess("Sending $action request to service $($service.ServiceName) on $($service.ComputerName)")) {
137+
# Get a fresh CIM instance via the DCOM session to avoid issues with deserialized
138+
# CIM objects crossing runspace boundaries without their session context.
139+
if ($cimSession) {
140+
try {
141+
$splatGetFreshCim = @{
142+
CimSession = $cimSession
143+
Namespace = "root\cimv2"
144+
Query = "SELECT * FROM Win32_Service WHERE Name = '$($service.ServiceName)'"
145+
}
146+
$freshCimObj = Get-CimInstance @splatGetFreshCim
147+
if ($freshCimObj) {
148+
$service._CimObject = $freshCimObj
149+
}
150+
} catch {
151+
# Fall back to using the existing deserialized CIM object if session refresh fails
152+
}
145153
}
146-
$freshCimObj = Get-CimInstance @splatGetFreshCim
147-
if ($freshCimObj) {
148-
$service._CimObject = $freshCimObj
154+
#Invoke corresponding CIM method
155+
$invokeResult = Invoke-CimMethod -InputObject $service._CimObject -MethodName $methodName
156+
$invokeResults += [psobject]@{
157+
InvokeResult = $invokeResult
158+
ServiceState = $invokeResult.State
159+
ServiceExitCode = $invokeResult.ReturnValue
160+
CheckPending = $true
161+
Service = $service
149162
}
150-
} catch {
151-
# Fall back to using the existing deserialized CIM object if session refresh fails
152163
}
153164
}
154-
#Invoke corresponding CIM method
155-
$invokeResult = Invoke-CimMethod -InputObject $service._CimObject -MethodName $methodName
156-
$invokeResults += [psobject]@{
157-
InvokeResult = $invokeResult
158-
ServiceState = $invokeResult.State
159-
ServiceExitCode = $invokeResult.ReturnValue
160-
CheckPending = $true
161-
Service = $service
162-
}
163-
}
164-
}
165165

166-
$startTime = Get-Date
167-
if ($Pscmdlet.ShouldProcess("Waiting the services to $action on $computerName")) {
168-
#Wait for the service to complete the action until timeout
169-
while ($invokeResults.CheckPending -contains $true) {
170-
foreach ($result in ($invokeResults | Where-Object CheckPending -eq $true)) {
171-
try {
172-
#Refresh Cim instance - not using Get-DbaCmObject because module is not loaded here, but it only refreshes existing object
173-
if ($cimSession) {
174-
$splatRefreshCim = @{
175-
CimSession = $cimSession
176-
Namespace = "root\cimv2"
177-
Query = "SELECT State FROM Win32_Service WHERE Name = '$($result.Service.ServiceName)'"
166+
$startTime = Get-Date
167+
if ($Pscmdlet.ShouldProcess("Waiting the services to $action on $computerName")) {
168+
#Wait for the service to complete the action until timeout
169+
while ($invokeResults.CheckPending -contains $true) {
170+
foreach ($result in ($invokeResults | Where-Object CheckPending -eq $true)) {
171+
try {
172+
#Refresh Cim instance - not using Get-DbaCmObject because module is not loaded here, but it only refreshes existing object
173+
if ($cimSession) {
174+
$splatRefreshCim = @{
175+
CimSession = $cimSession
176+
Namespace = "root\cimv2"
177+
Query = "SELECT State FROM Win32_Service WHERE Name = '$($result.Service.ServiceName)'"
178+
}
179+
$refreshedCimObj = Get-CimInstance @splatRefreshCim
180+
if ($refreshedCimObj) {
181+
$result.Service._CimObject = $refreshedCimObj
182+
}
183+
} else {
184+
$result.Service._CimObject = $result.Service._CimObject | Get-CimInstance
185+
}
186+
} catch {
187+
$result.ServiceExitCode = -3
188+
$result.ServiceState = "Unknown"
189+
$result.CheckPending = $false
190+
continue
178191
}
179-
$refreshedCimObj = Get-CimInstance @splatRefreshCim
180-
if ($refreshedCimObj) {
181-
$result.Service._CimObject = $refreshedCimObj
192+
$result.ServiceState = $result.Service._CimObject.State
193+
#Failed or succeeded
194+
if ($result.ServiceExitCode -ne 0 -or $result.ServiceState -eq $desiredState) {
195+
$result.CheckPending = $false
196+
continue
182197
}
183-
} else {
184-
$result.Service._CimObject = $result.Service._CimObject | Get-CimInstance
198+
#Failed after being in the Pending state
199+
if ($result.CheckPending -and $result.ServiceState -eq $undesiredState) {
200+
$result.ServiceExitCode = -2
201+
$result.CheckPending = $false
202+
continue
203+
}
204+
#Timed out
205+
if ($timeout -gt 0 -and ((Get-Date) - $startTime).TotalSeconds -gt $timeout) {
206+
$result.ServiceExitCode = -1
207+
$result.CheckPending = $false
208+
continue
209+
}
210+
#Still pending - leave CheckPending as is and run again
185211
}
186-
} catch {
187-
$result.ServiceExitCode = -3
188-
$result.ServiceState = "Unknown"
189-
$result.CheckPending = $false
190-
continue
212+
Start-Sleep -Milliseconds 200
191213
}
192-
$result.ServiceState = $result.Service._CimObject.State
193-
#Failed or succeeded
194-
if ($result.ServiceExitCode -ne 0 -or $result.ServiceState -eq $desiredState) {
195-
$result.CheckPending = $false
196-
continue
197-
}
198-
#Failed after being in the Pending state
199-
if ($result.CheckPending -and $result.ServiceState -eq $undesiredState) {
200-
$result.ServiceExitCode = -2
201-
$result.CheckPending = $false
202-
continue
214+
}
215+
foreach ($result in $invokeResults) {
216+
#Add status
217+
$status = switch ($result.ServiceExitCode) {
218+
0 { "Successful" }
219+
10 { "Successful " } #Already running - FullText service is started automatically
220+
default { "Failed" }
203221
}
204-
#Timed out
205-
if ($timeout -gt 0 -and ((Get-Date) - $startTime).TotalSeconds -gt $timeout) {
206-
$result.ServiceExitCode = -1
207-
$result.CheckPending = $false
208-
continue
222+
Add-Member -Force -InputObject $result.Service -NotePropertyName Status -NotePropertyValue $status
223+
#Add error message
224+
$errorMessageFromReturnValue = if ($result.ServiceExitCode -in 0..($errorCodes.Length - 1)) {
225+
$errorCodes[$result.ServiceExitCode]
226+
} else { "Unknown error." }
227+
$message = switch ($result.ServiceExitCode) {
228+
-2 { "The service failed to $action." }
229+
-1 { "The attempt to $action the service has timed out." }
230+
0 { "Service was successfully $actionText." }
231+
default { "The attempt to $action the service returned the following error: $errorMessageFromReturnValue" }
209232
}
210-
#Still pending - leave CheckPending as is and run again
233+
Add-Member -Force -InputObject $result.Service -NotePropertyName Message -NotePropertyValue $message
234+
# Refresh service state for the object
235+
if ($result.ServiceState) { $result.Service.State = $result.ServiceState }
236+
$result
211237
}
212-
Start-Sleep -Milliseconds 200
213-
}
214-
}
215-
foreach ($result in $invokeResults) {
216-
#Add status
217-
$status = switch ($result.ServiceExitCode) {
218-
0 { "Successful" }
219-
10 { "Successful " } #Already running - FullText service is started automatically
220-
default { "Failed" }
221238
}
222-
Add-Member -Force -InputObject $result.Service -NotePropertyName Status -NotePropertyValue $status
223-
#Add error message
224-
$errorMessageFromReturnValue = if ($result.ServiceExitCode -in 0..($errorCodes.Length - 1)) {
225-
$errorCodes[$result.ServiceExitCode]
226-
} else { "Unknown error." }
227-
$message = switch ($result.ServiceExitCode) {
228-
-2 { "The service failed to $action." }
229-
-1 { "The attempt to $action the service has timed out." }
230-
0 { "Service was successfully $actionText." }
231-
default { "The attempt to $action the service returned the following error: $errorMessageFromReturnValue" }
239+
} finally {
240+
if ($cimSession) {
241+
Remove-CimSession -CimSession $cimSession -ErrorAction "SilentlyContinue"
232242
}
233-
Add-Member -Force -InputObject $result.Service -NotePropertyName Message -NotePropertyValue $message
234-
# Refresh service state for the object
235-
if ($result.ServiceState) { $result.Service.State = $result.ServiceState }
236-
$result
237243
}
238244
}
239-
} finally {
240-
if ($cimSession) {
241-
Remove-CimSession -CimSession $cimSession -ErrorAction "SilentlyContinue"
242-
}
243-
}
244-
}
245245

246-
$actionText = switch ($action) { stop { 'stopped' }; start { 'started' }; restart { 'restarted' } }
247-
$errorCodes = Get-DbaServiceErrorMessage
248-
}
246+
$actionText = switch ($action) { stop { 'stopped' }; start { 'started' }; restart { 'restarted' } }
247+
$errorCodes = Get-DbaServiceErrorMessage
248+
}
249249

250250
process {
251251
#Group services for each computer

public/ConvertTo-DbaTimeline.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function ConvertTo-DbaTimeline {
171171
$data = $InputObject | Select-Object @{ Name = "SqlInstance"; Expression = { $_.SqlInstance } }, @{ Name = "InstanceName"; Expression = { $_.InstanceName } }, @{ Name = "vLabel"; Expression = { "[" + $($_.SqlInstance -replace "\\", "\\\") + "] " + $_.Database } }, @{ Name = "hLabel"; Expression = { $_.Type } }, @{ Name = "StartDate"; Expression = { $(ConvertTo-JsDate($_.Start)) } }, @{ Name = "EndDate"; Expression = { $(ConvertTo-JsDate($_.End)) } }
172172
} elseif ($null -ne $InputObject[0].PSObject.Properties['EventClass'] -and $null -ne $InputObject[0].PSObject.Properties['ChangeInSize']) {
173173
$CallerName = "Find-DbaDbGrowthEvent"
174-
$data = $InputObject | Select-Object @{ Name = "SqlInstance"; Expression = { $_.SqlInstance } }, @{ Name = "InstanceName"; Expression = { $_.InstanceName } }, @{ Name = "vLabel"; Expression = { ("[" + $($_.SqlInstance -replace "\\", "\\\") + "] " + $_.DatabaseName).Replace("'", "\'") } }, @{ Name = "hLabel"; Expression = { switch ([int]$_.EventClass) { 92 { "Data Grow" } 93 { "Log Grow" } 94 { "Data Shrink" } 95 { "Log Shrink" } default { "Unknown" } } } }, @{ Name = "Style"; Expression = { if ([int]$_.EventClass -in 92, 93) { "#36B300" } else { "#FF8C00" } } }, @{ Name = "StartDate"; Expression = { $(ConvertTo-JsDate($_.StartTime)) } }, @{ Name = "EndDate"; Expression = { $(ConvertTo-JsDate($_.EndTime)) } }
174+
$data = $InputObject | Select-Object @{ Name = "SqlInstance"; Expression = { $_.SqlInstance } }, @{ Name = "InstanceName"; Expression = { $_.InstanceName } }, @{ Name = "vLabel"; Expression = { ("[" + $($_.SqlInstance -replace "\\", "\\\") + "] " + $($_.DatabaseName -replace "\\", "\\\")).Replace("'", "\'") } }, @{ Name = "hLabel"; Expression = { switch ([int]$_.EventClass) { 92 { "Data Grow" } 93 { "Log Grow" } 94 { "Data Shrink" } 95 { "Log Shrink" } default { "Unknown" } } } }, @{ Name = "Style"; Expression = { if ([int]$_.EventClass -in 92, 93) { "#36B300" } else { "#FF8C00" } } }, @{ Name = "StartDate"; Expression = { $(ConvertTo-JsDate($_.StartTime)) } }, @{ Name = "EndDate"; Expression = { $(ConvertTo-JsDate($_.EndTime)) } }
175175
} else {
176176
# sorry to be so formal, can't help it ;)
177177
Stop-Function -Message "Unsupported input data. To request support for additional commands, please file an issue at dbatools.io/issues and we'll take a look"

tests/Compare-DbaLogin.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Describe $CommandName -Tag UnitTests {
7878
It "skips failed destinations without reusing the previous connection" {
7979
$result = Compare-DbaLogin -Source "source1" -Destination "dest1", "dest2"
8080

81-
$result.Count | Should -Be 1
81+
@($result).Count | Should -Be 1
8282
$result.SourceServer | Should -Be "source1"
8383
$result.DestinationServer | Should -Be "dest1"
8484
Should -Invoke Get-DbaLogin -Times 2 -Exactly

tests/Start-DbaMigration.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ Describe $CommandName -Tag UnitTests {
318318
$null = Start-DbaMigration -Source "sql1" -Destination "sql2" -Exclude $excludeForSsisOnly
319319
$script:ssisCopied | Should -BeFalse
320320
$script:stopMessages | Should -BeNullOrEmpty
321-
($script:messages | Where-Object { $PSItem -like "*Skipping SSIS catalog migration*" }).Count | Should -Be 1
321+
@($script:messages | Where-Object { $PSItem -like "*Skipping SSIS catalog migration*" }).Count | Should -Be 1
322322
} finally {
323323
foreach ($functionName in $functionNames) {
324324
if ($originalFunctions.ContainsKey($functionName)) {

0 commit comments

Comments
 (0)