Skip to content

Commit

Permalink
Add DbaOperator v5 #887
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudioESSilva committed May 3, 2022
1 parent e28f7ec commit d8d1708
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
21 changes: 12 additions & 9 deletions Perf Testing pesterv5.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

ipmo ./dbachecks.psd1

#
#

$Checks = 'TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
$Checks = 'TraceFlagsExpected'
Compare-CheckRuns -Checks $checks


<#
When there are default skips (some of the CIS checks) we need to set the configs and check
Expand Down Expand Up @@ -58,14 +57,14 @@ Disable-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred -TraceFlag

# If you get odd results - or you dont get any checks run

# run the import module and the Invoke Dbc Check with Verbose and that might show you New-Json messing
# run the import module and the Invoke Dbc Check with Verbose and that might show you New-Json messing
# with your files or that you are looking in PSMOdulePath instead of Git Repo path (run Reset-dbcConfig to fix that)

function Compare-CheckRuns {
param($Checks)
$password = ConvertTo-SecureString "dbatools.IO" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sqladmin", $password
$Sqlinstances = 'localhost,7401', 'localhost,7402', 'localhost,7403'
$Sqlinstances = 'localhost,14333', 'localhost,14334' #'localhost,7401', 'localhost,7402', 'localhost,7403'

$originalCode = {
Invoke-DbcCheck -SqlInstance $Sqlinstances -Check $Checks -SqlCredential $cred -legacy $true -Show None
Expand All @@ -82,16 +81,16 @@ $originalCodeMessage = "With original Code it takes {0} MilliSeconds" -f $origin


$savingMessage = "
Running with
Running with
{3}
{3}
Checks against 3 SQL Containers
Checks against $($Sqlinstances.Count) SQL Containers
With original Code it takes {1} Seconds
With New Code it takes {4} Seconds
New Code for these {5} checks
New Code for these {5} checks
is saving {0} seconds
from a run of {1} seconds
New Code runs in {2} % of the time
Expand All @@ -100,3 +99,7 @@ cls

Write-PSFMessage -Message $savingMessage -Level Output
}


$Checks = 'DbaOperator'
Compare-CheckRuns -Checks $checks
2 changes: 2 additions & 0 deletions internal/configurations/configuration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ Set-PSFConfig -Module dbachecks -Name skip.instance.defaulttrace -Validation boo
Set-PSFConfig -Module dbachecks -Name skip.agent.databasemailenabled -Validation bool -Value $false -Initialize -Description "Skip the Database Mail Enabled agent check"
Set-PSFConfig -Module dbachecks -Name skip.agent.servicestartmode -Validation bool -Value $false -Initialize -Description "Skip the Agent Service State check"
Set-PSFConfig -Module dbachecks -Name skip.agent.servicestate -Validation bool -Value $false -Initialize -Description "Skip the Agent Service Start Mode check"
Set-PSFConfig -Module dbachecks -Name skip.agent.operatorname -Validation bool -Value $false -Initialize -Description "Skip the Agent Operator Name check"
Set-PSFConfig -Module dbachecks -Name skip.agent.operatoremail -Validation bool -Value $false -Initialize -Description "Skip the Agent Operator Email check"
Set-PSFConfig -Module dbachecks -Name skip.agent.longrunningjobs -Validation bool -Value $false -Initialize -Description "Skip the long running agent jobs check"
Set-PSFConfig -Module dbachecks -Name skip.agent.lastjobruntime -Validation bool -Value $false -Initialize -Description "Skip the last agent job time check"
Set-PSFConfig -Module dbachecks -Name skip.instance.dac -Validation bool -Value $false -Initialize -Description "Skip Dedicated Administrator Connection (DAC) check"
Expand Down
34 changes: 32 additions & 2 deletions internal/functions/Get-AllAgentInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Get-AllAgentInfo {
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Login], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Job], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Operator], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.StoredProcedure], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Information], $false)
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Settings], $false)
Expand All @@ -20,6 +21,9 @@ function Get-AllAgentInfo {
$ServerInitFields.Add("VersionMajor") | Out-Null # so we can check versions
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Server], $ServerInitFields)

# Job Server Initial fields
$OperatorInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Operator])

# Database Initial Fields
$DatabaseInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database])

Expand Down Expand Up @@ -51,11 +55,11 @@ function Get-AllAgentInfo {

'DatabaseMailEnabled' {
$configurations = $true
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'databasemailenabled' -Value (Get-DbcConfigValue policy.security.databasemailenabled)
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'DatabaseMailEnabled' -Value (Get-DbcConfigValue policy.security.databasemailenabled)
}
'AgentServiceAccount' {
if (($Instance.VersionMajor -ge 14) -or $IsLinux -or $Instance.HostPlatform -eq 'Linux') {
$Agent = @($Instance.Query("SELECT status_desc, startup_type_desc FROM sys.dm_server_services") | Where-Object servicename -like '*Agent*').Foreach{
$Agent = @($Instance.Query("SELECT status_desc, startup_type_desc FROM sys.dm_server_services") | Where-Object servicename -like '*Agent*').ForEach{
[PSCustomObject]@{
State = $PSItem.status_desc
StartMode = $PSItem.startup_type_desc
Expand All @@ -66,7 +70,32 @@ function Get-AllAgentInfo {
}
}
'DbaOperator' {
$OperatorInitFields.Add("Name") | Out-Null # so we can check operators
$Instance.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Operator], $OperatorInitFields)
$OperatorInitFields = $Instance.GetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Agent.Operator])

$ConfigValues | Add-Member -MemberType NoteProperty -Name 'DbaOperatorName' -Value (Get-DbcConfigValue agent.dbaoperatorname)
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'DbaOperatorEmail' -Value (Get-DbcConfigValue agent.dbaoperatoremail)

$Operator = $ConfigValues.DbaOperatorName.ForEach{
[PSCustomObject]@{
InstanceName = $Instance.Name
ExpectedOperatorName = $PSItem
ActualOperatorName = $Instance.JobServer.Operators.Name
ExpectedOperatorEmail = 'null'
ActualOperatorEmail = 'null'
}
}

$Operator += $ConfigValues.DbaOperatorEmail.ForEach{
[PSCustomObject]@{
InstanceName = $Instance.Name
ExpectedOperatorName = 'null'
ActualOperatorName = 'null'
ExpectedOperatorEmail = $PSItem
ActualOperatorEmail = $Instance.JobServer.Operators.EmailAddress
}
}
}
'FailsafeOperator' {

Expand Down Expand Up @@ -111,6 +140,7 @@ function Get-AllAgentInfo {
IsClustered = $Instance.IsClustered
DatabaseMailEnabled = $Instance.Configuration.DatabaseMailEnabled.ConfigValue
Agent = @($Agent)
Operator = @($Operator)
}
return $testInstanceObject
}

0 comments on commit d8d1708

Please sign in to comment.