Skip to content

Instantly share code, notes, and snippets.

@fabriceleal
Created November 23, 2012 16:12
Show Gist options
  • Save fabriceleal/4136290 to your computer and use it in GitHub Desktop.
Save fabriceleal/4136290 to your computer and use it in GitHub Desktop.
Microsoft.SqlServer.Smo - log database size to file
# You MUST have sql management tools installed
# ("...dll", "...dll", "...dll") | foreach { [System.Reflection.Assembly]::LoadFile($_) }
# Load these. Probabily missing in GAC; load with absolute path
[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Smo.dll")
#[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll")
#[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\
# Fill those:
$instance = "..."
$user = "..."
$pwd = "..."
$dbname = "..."
$filename = "..."
# Connect to instance
$srv = new-object microsoft.sqlserver.management.smo.server($instance)
$srv.connectionContext.Loginsecure = $false
$srv.connectionContext.login = $user
$srv.connectionContext.password = $pwd
# Get database
$db = $srv.databases | where { $_.name -eq $dbname}
# Update database info and return stringyfied size (MB)
function t(){
$db.Refresh();
return $db.size.toString()
}
# Dump to file (date;size)
while(1) {
[system.threading.thread]::Sleep(5000)
$s = ""
$s = $s + [system.datetime]::Now.toString("yyyy-MM-dd HH:mm:ss")
$s = $s + ";"
$v = t
$s = $s + $v
$s >> $filename
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment