# Load SMO libraries
[Reflection.Assembly]::Load("Microsoft.SqlServer.Smo, `
Version=9.0.242.0, Culture=neutral, `
PublicKeyToken=89845dcd8080cc91") > $null
[Reflection.Assembly]::Load("Microsoft.SqlServer.SqlEnum, `
Version=9.0.242.0, Culture=neutral, `
PublicKeyToken=89845dcd8080cc91") > $null
[Reflection.Assembly]::Load("Microsoft.SqlServer.SmoEnum, `
Version=9.0.242.0, Culture=neutral, `
PublicKeyToken=89845dcd8080cc91") > $null
[Reflection.Assembly]::Load("Microsoft.SqlServer.ConnectionInfo, `
Version=9.0.242.0, Culture=neutral, `
PublicKeyToken=89845dcd8080cc91") > $null
# define connection properties
[string] $server_name = 'myserver'
[string] $db_name = 'mydb'
[bool] $windows_login = $true
# Create server object
$server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $server_name
# if not using windows authentication then prompt for credentials
if ($windows_login -eq $false)
{
# prompt for connection details
$server.ConnectionContext.LoginSecure=$false;
$credential = Get-Credential
$username = $credential.UserName -replace("\\","")
$server.ConnectionContext.set_Login($username)
$server.ConnectionContext.set_SecurePassword($credential.Password)
}
$db = $server.Databases[$db_name]
# list all tables
foreach ($table in $db.Tables)
{
write-host $table.name
}