Skip to content

Commit 21110c1

Browse files
so we can check logins that must change passwords
1 parent 2b6c37d commit 21110c1

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

developing/Robs-Instance.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $Checks = 'LoginAuditSuccessful'
1616
$Checks = 'LoginCheckPolicy'
1717
$Checks = 'SuspectPageLimit'
1818
$Checks = 'SupportedBuild'
19+
$Checks = 'LoginMustChange'
1920
$Checks = 'LoginAuditSuccessful', 'LoginAuditFailed'
2021

2122
Invoke-PerfAndValidateCheck -Checks $Checks

source/checks/Instancev5.Tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ Describe "Login Check Policy" -Tag LoginCheckPolicy, Security, CIS, Medium, Inst
227227
}
228228
}
229229

230+
Describe "Login Must Change" -Tag LoginMustChange, Security, CIS, Medium, Instance -ForEach $InstancesToTest {
231+
$skip = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.security.LoginMustChange' }).Value
232+
Context "Testing if the new SQL logins that have not logged have to change their password when they log in on <_.Name>" {
233+
It "All new sql logins should have the have to change their password when they log in for the first time on <_.Name>" -Skip:$skip {
234+
$PsItem.LoginMustChangeCount | Should -Be 0 -Because "We expected the all the new sql logins to have to change the password on first login"
235+
}
236+
}
237+
}
238+
230239
Describe "Instance MaxDop" -Tag MaxDopInstance, MaxDop, Medium, Instance -ForEach ($InstancesToTest | Where-Object { $psitem.Name -notin $psitem.ConfigValues.ExcludeInstanceMaxDop }) {
231240
$skip = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.instance.MaxDopInstance' }).Value
232241
Context "Testing Instance MaxDop Value on <_.Name>" {

source/internal/assertions/Instance.Assertions.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ function Get-AllInstanceInfo {
717717
$role = Get-DbaServerRole -SqlInstance $instance -ServerRole "sysadmin"
718718

719719
$LoginMustChange = [pscustomobject] @{
720-
Count = @(Get-DbaLogin -SqlInstance $instance -Login @($role.Login) -Type SQL | Where-Object { $_.MustChangePassword -eq $false -and $_.IsDisabled -eq $false -and $null -eq $_LastLogin }).Count
720+
Count = @(Get-DbaLogin -SqlInstance $instance -Login @($role.Login) -Type SQL | Where-Object { $_.MustChangePassword -eq $false -and $_.IsDisabled -eq $false -and $null -eq $_.LastLogin }).Count
721721
}
722722
} catch {
723723
$There = $false

source/internal/functions/NewGet-AllInstanceInfo.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ function NewGet-AllInstanceInfo {
461461
}
462462
}
463463

464+
'LoginMustChange' {
465+
$loginTimeSql = "SELECT login_name, MAX(login_time) AS login_time FROM sys.dm_exec_sessions GROUP BY login_name"
466+
$loginTimes = $instance.ConnectionContext.ExecuteWithResults($loginTimeSql).Tables[0]
467+
$lastlogin = @{Name = 'LastLogin' ; Expression = { $Name = $_.name; ($loginTimes | Where-Object { $_.login_name -eq $name }).login_time
468+
}
469+
}
470+
$LoginMustChangeCount = ($Instance.Logins | Where-Object { $_.Name -in $Instance.Roles['sysadmin'].EnumMemberNames() } | Select-Object Name, $lastlogin, MustChangePassword, IsDisabled | Where-Object { $_.MustChangePassword -eq $false -and $_.IsDisabled -eq $false -and $null -eq $_.LastLogin }).Count
471+
}
472+
464473
Default { }
465474
}
466475

@@ -563,6 +572,7 @@ function NewGet-AllInstanceInfo {
563572
HideInstance = $HideInstance
564573
SuspectPageCountResult = $SuspectPageCountResult
565574
SupportedBuild = $SupportedBuild
575+
LoginMustChangeCount = $LoginMustChangeCount
566576
# TempDbConfig = [PSCustomObject]@{
567577
# TF118EnabledCurrent = $tempDBTest[0].CurrentSetting
568578
# TF118EnabledRecommended = $tempDBTest[0].Recommended

0 commit comments

Comments
 (0)