Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basics - Monitor SQL Server database Auto Create Statistics
# Code answers from Session Q & A (http://powershell.sqlpass.org/SessionQA.aspx)
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
$sqlservers = "sqlserver2014a","sql2012-esx"
foreach ($sqlserver in $sqlservers) {
Write-Output "Connecting to $sqlserver"
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $sqlserver
foreach ($db in $server.databases) {
if ($db.AutoCreateStatisticsEnabled -eq $false) {
# Set it back? Not sure what you're looking to do.
$db.AutoCreateStatisticsEnabled = $true
$db.alter()
# Or just write a warning?
Write-Warning "$($db.name) has the wrong setting. I have no idea who changed it, though."
}
}
$server.ConnectionContext.Disconnect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.