Create a gist now

Instantly share code, notes, and snippets.

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