Thursday, October 11, 2012

Powershell–Get the size of a database

Here is a powershell script that uses SMO to determine the size of a database:

$dbServer = “xxxxxx”
$databaseName = "yyyyyy"

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$sqlServerSmo = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server ($dbServer)
if ($sqlServerSmo.databases[$databaseName] -ne $null)
{
    Write-Host (($sqlServerSmo.databases[$databaseName].Size * 1MB) /1GB)
}
else
{
    Write-Host "Database $databaseName does not exist on server $dbServer"
}
Write-Host "Done"

No comments: