|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Gets the SQL Agent configuration properies of the targeted SQL server |
| 4 | + |
| 5 | +.DESCRIPTION |
| 6 | + Uses SMO Object DLLs |
| 7 | + |
| 8 | +.EXAMPLE |
| 9 | + 04_Agent_Configuration.ps1 localhost |
| 10 | + |
| 11 | +.EXAMPLE |
| 12 | + 04_Agent_Configuration.ps1 server01 sa password |
| 13 | +
|
| 14 | +.Inputs |
| 15 | + ServerName, [SQLUser], [SQLPassword] |
| 16 | +
|
| 17 | +.Outputs |
| 18 | +
|
| 19 | + |
| 20 | +.NOTES |
| 21 | +
|
| 22 | + |
| 23 | +.LINK |
| 24 | + https://github.com/gwalkey |
| 25 | + |
| 26 | +#> |
| 27 | + |
| 28 | +[CmdletBinding()] |
| 29 | +Param( |
| 30 | + [parameter(Position=0,mandatory=$false,ValueFromPipeline)] |
| 31 | + [ValidateNotNullOrEmpty()] |
| 32 | + [string]$SQLInstance='localhost', |
| 33 | + |
| 34 | + [parameter(Position=1,mandatory=$false,ValueFromPipeline)] |
| 35 | + [ValidateLength(0,50)] |
| 36 | + [string]$myuser, |
| 37 | + |
| 38 | + [parameter(Position=2,mandatory=$false,ValueFromPipeline)] |
| 39 | + [ValidateLength(0,50)] |
| 40 | + [string]$mypass |
| 41 | +) |
| 42 | + |
| 43 | +# Load Common Modules and .NET Assemblies |
| 44 | +try |
| 45 | +{ |
| 46 | + Import-Module ".\SQLTranscriptase.psm1" -ErrorAction Stop |
| 47 | +} |
| 48 | +catch |
| 49 | +{ |
| 50 | + Throw('SQLTranscriptase.psm1 not found') |
| 51 | +} |
| 52 | + |
| 53 | +try |
| 54 | +{ |
| 55 | + Import-Module ".\LoadSQLSmo.psm1" |
| 56 | +} |
| 57 | +catch |
| 58 | +{ |
| 59 | + Throw('LoadSQLSmo.psm1 not found') |
| 60 | +} |
| 61 | + |
| 62 | +LoadSQLSMO |
| 63 | + |
| 64 | +# Init |
| 65 | +Set-StrictMode -Version latest; |
| 66 | +[string]$BaseFolder = (get-location).path |
| 67 | +Write-Host -f Yellow -b Black "04 - Agent Configuration" |
| 68 | +Write-Output("Server: [{0}]" -f $SQLInstance) |
| 69 | + |
| 70 | +# Server connection check |
| 71 | +$SQLCMD1 = "select serverproperty('productversion') as 'Version'" |
| 72 | +try |
| 73 | +{ |
| 74 | + if ($mypass.Length -ge 1 -and $myuser.Length -ge 1) |
| 75 | + { |
| 76 | + Write-Output "Testing SQL Auth" |
| 77 | + $myver = ConnectSQLAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -User $myuser -Password $mypass -ErrorAction Stop| Select-Object -ExpandProperty Version |
| 78 | + $serverauth="sql" |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + Write-Output "Testing Windows Auth" |
| 83 | + $myver = ConnectWinAuth -SQLInstance $SQLInstance -Database "master" -SQLExec $SQLCMD1 -ErrorAction Stop | Select-Object -ExpandProperty Version |
| 84 | + $serverauth = "win" |
| 85 | + } |
| 86 | + |
| 87 | + if($null -ne $myver) |
| 88 | + { |
| 89 | + Write-Output ("SQL Version: {0}" -f $myver) |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | +catch |
| 94 | +{ |
| 95 | + Write-Host -f red "$SQLInstance appears offline." |
| 96 | + Set-Location $BaseFolder |
| 97 | + exit |
| 98 | +} |
| 99 | + |
| 100 | +# Create folder |
| 101 | +$fullfolderPath = "$BaseFolder\$sqlinstance\04 - Agent Configuration" |
| 102 | +if(!(test-path -path $fullfolderPath)) |
| 103 | +{ |
| 104 | + mkdir $fullfolderPath | Out-Null |
| 105 | +} |
| 106 | + |
| 107 | +# New UP SMO Server Object |
| 108 | +if ($serverauth -eq "win") |
| 109 | +{ |
| 110 | + try |
| 111 | + { |
| 112 | + $srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLInstance |
| 113 | + } |
| 114 | + catch |
| 115 | + { |
| 116 | + Write-Output "Cannot Create an SMO Object" |
| 117 | + Write-Output("Error is: {0}" -f $error[0]) |
| 118 | + exit |
| 119 | + } |
| 120 | +} |
| 121 | +else |
| 122 | +{ |
| 123 | + try |
| 124 | + { |
| 125 | + $srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $SQLInstance |
| 126 | + $srv.ConnectionContext.LoginSecure=$false |
| 127 | + $srv.ConnectionContext.set_Login($myuser) |
| 128 | + $srv.ConnectionContext.set_Password($mypass) |
| 129 | + } |
| 130 | + catch |
| 131 | + { |
| 132 | + Write-Output "Cannot Create an SMO Object" |
| 133 | + Write-Output("Error is: {0}" -f $error[0]) |
| 134 | + exit |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +# Create File and Header |
| 140 | +$fullFileName = $fullfolderPath+"\04_Agent_Configuration.txt" |
| 141 | +New-Item $fullFileName -type file -force | Out-Null |
| 142 | +$Now = (Get-Date -f "MM/dd/yyyy HH:mm:ss.fff") |
| 143 | +Write-Output("{0} - SQL Agent Configuration for [{1}] `r`n" -f $now, $SQLInstance) | out-file $fullFileName -Encoding Ascii -Append |
| 144 | + |
| 145 | +# Create Datatable for table-based formatting |
| 146 | +$DataTable = New-Object System.Data.DataTable |
| 147 | +$DataTable.Columns.Add("Setting","string") | out-null |
| 148 | +$DataTable.Columns.Add("Value","string") | out-null |
| 149 | + |
| 150 | +# Get Agent Configuration Settings using SMO |
| 151 | +$Agent = $srv.JobServer |
| 152 | + |
| 153 | +[void]$DataTable.Rows.Add('AgentDomainGroup',$Agent.AgentDomainGroup) |
| 154 | +[void]$DataTable.Rows.Add('AgentLogLevel',$Agent.AgentLogLevel) |
| 155 | +[void]$DataTable.Rows.Add('AgentShutdownWaitTime',$Agent.AgentShutdownWaitTime) |
| 156 | +[void]$DataTable.Rows.Add('AlertSystem',$Agent.AlertSystem) |
| 157 | +[void]$DataTable.Rows.Add('DatabaseEngineType',$Agent.DatabaseEngineType) |
| 158 | +[void]$DataTable.Rows.Add('DatabaseEngineEdition',$Agent.DatabaseEngineEdition) |
| 159 | +[void]$DataTable.Rows.Add('DatabaseMailProfile',$Agent.DatabaseMailProfile) |
| 160 | +[void]$DataTable.Rows.Add('ErrorLogFile',$Agent.ErrorLogFile) |
| 161 | +[void]$DataTable.Rows.Add('ExecutionManager',$Agent.ExecutionManager) |
| 162 | +[void]$DataTable.Rows.Add('HostLoginName',$Agent.HostLoginName) |
| 163 | +[void]$DataTable.Rows.Add('IdleCpuDuration',$Agent.IdleCpuDuration) |
| 164 | +[void]$DataTable.Rows.Add('IdleCpuPercentage',$Agent.IdleCpuPercentage) |
| 165 | +[void]$DataTable.Rows.Add('IsCpuPollingEnabled',$Agent.IsCpuPollingEnabled) |
| 166 | +[void]$DataTable.Rows.Add('JobServerType',$Agent.JobServerType) |
| 167 | +[void]$DataTable.Rows.Add('JobServerType',$Agent.JobServerType) |
| 168 | +[void]$DataTable.Rows.Add('LocalHostAlias',$Agent.LocalHostAlias) |
| 169 | +[void]$DataTable.Rows.Add('LoginTimeout',$Agent.LoginTimeout) |
| 170 | +[void]$DataTable.Rows.Add('MaximumHistoryRows',$Agent.MaximumHistoryRows) |
| 171 | +[void]$DataTable.Rows.Add('MaximumJobHistoryRows',$Agent.MaximumJobHistoryRows) |
| 172 | +[void]$DataTable.Rows.Add('MsxAccountCredentialName',$Agent.MsxAccountCredentialName) |
| 173 | +[void]$DataTable.Rows.Add('MsxAccountName',$Agent.MsxAccountName) |
| 174 | +[void]$DataTable.Rows.Add('MsxServerName',$Agent.MsxServerName) |
| 175 | +[void]$DataTable.Rows.Add('Name',$Agent.Name) |
| 176 | +[void]$DataTable.Rows.Add('NetSendRecipient',$Agent.NetSendRecipient) |
| 177 | +[void]$DataTable.Rows.Add('Parent',$Agent.Parent) |
| 178 | +[void]$DataTable.Rows.Add('ParentCollection',$Agent.ParentCollection) |
| 179 | +[void]$DataTable.Rows.Add('ReplaceAlertTokensEnabled',$Agent.ReplaceAlertTokensEnabled) |
| 180 | +[void]$DataTable.Rows.Add('ReplaceAlertTokensEnabled',$Agent.ReplaceAlertTokensEnabled) |
| 181 | +[void]$DataTable.Rows.Add('ServerVersion',$Agent.ServerVersion) |
| 182 | +[void]$DataTable.Rows.Add('ServiceAccount',$Agent.ServiceAccount) |
| 183 | +[void]$DataTable.Rows.Add('ServiceStartMode',$Agent.ServiceStartMode) |
| 184 | +[void]$DataTable.Rows.Add('SqlAgentAutoStart',$Agent.SqlAgentAutoStart) |
| 185 | +[void]$DataTable.Rows.Add('SqlAgentMailProfile',$Agent.SqlAgentMailProfile) |
| 186 | +[void]$DataTable.Rows.Add('SqlAgentRestart',$Agent.SqlAgentRestart) |
| 187 | +[void]$DataTable.Rows.Add('SqlServerRestart',$Agent.SqlServerRestart) |
| 188 | +[void]$DataTable.Rows.Add('State',$Agent.State) |
| 189 | +[void]$DataTable.Rows.Add('SysAdminOnly',$Agent.SysAdminOnly) |
| 190 | +[void]$DataTable.Rows.Add('Urn',$Agent.Urn) |
| 191 | +[void]$DataTable.Rows.Add('UserData',$Agent.UserData) |
| 192 | +[void]$DataTable.Rows.Add('UserData',$Agent.UserData) |
| 193 | +[void]$DataTable.Rows.Add('WriteOemErrorLog',$Agent.WriteOemErrorLog) |
| 194 | + |
| 195 | +# Add Table-Formatted Setting/Values |
| 196 | +$DataTable | select-object -property Setting, Value | sort-object -property Setting| Format-Table | out-string | out-file $fullFileName -Encoding ascii -Append |
| 197 | + |
| 198 | +# Add other Multi-Valued collections |
| 199 | +$mystring = "Job Categories: " + ($agent.JobCategories | select-object Parent, CategoryType, ID, Name, State | Format-Table | out-string) |
| 200 | +$mystring | out-file $fullFileName -Encoding ascii -Append |
| 201 | + |
| 202 | +$mystring = "AlertCategories: " + ($agent.AlertCategories | select-object ID, Name | Format-Table | out-string) |
| 203 | +$mystring | out-file $fullFileName -Encoding ascii -Append |
| 204 | + |
| 205 | +# Return to Initial Folder |
| 206 | +set-location $BaseFolder |
0 commit comments