This category page is part of a series of Professional PowerShell SMO Recipes. You can find an index of all recipes on the main SMO Recipes page, and if you want to learn more about each recipe and see some sample screenshots, click on its category page.
These scripts were created and tested on Windows 8.1 with PowerShell v4 and SQL Server 2014, though most recipes should work with earlier versions of SQL Server. PowerShell v3 and above is required for many of the recipes. If you have a recipe request, leave a comment and I'll see what I can do. This cookbook will be continuously built, as I work more with SMO.
Recipe Categories
Connecting to SQL Server
Connect / Test Connection / Disconnect from SQL Server
The follow recipe will create the server instance, attempt a connection, then disconnect. In my formally-written PowerShell scripts, I’ll generally place the Connect() in PROCESS {} and Disconnect() in the END {}.
Using Windows Authentication
# Load SMO, create server object, test connection, disconnect [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") $server = New-Object Microsoft.SqlServer.Management.Smo.Server "sqlserver" try { $server.ConnectionContext.Connect() } catch { throw "Can't connect to SQL Server." } Write-Host "Connection succeeded." -ForegroundColor Green $server.ConnectionContext.Disconnect()
Using SQL Login Authentication
$servername = "sqlcluster" Write-Host "Attempting to connect to SQL Servers.." -ForegroundColor Green [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") $server = New-Object Microsoft.SqlServer.Management.Smo.Server $servername $message = "Enter the SQL Login credentials for the SQL Server, $($servername.ToUpper())"; $server.ConnectionContext.LoginSecure = $false $sqllogin = Get-Credential -Message $message $server.ConnectionContext.set_Login($sqllogin.username) $server.ConnectionContext.set_SecurePassword($sqllogin.Password) try { $server.ConnectionContext.Connect() } catch { throw "Can't connect to $servername or access denied. Quitting." } Write-Host "Connection succeeded." -ForegroundColor Green $server.ConnectionContext.Disconnect()
Connect to SQL Server on a specific port
SQL Server’s default port is port 1433, though named instances can reside on different ports. While SMO can usually automatically find the instance’s port, sometimes the port must be specified. Here’s the syntax.
# Load SMO, create server object, test connection, disconnect [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") $server = New-Object Microsoft.SqlServer.Management.Smo.Server "sqlserver,1433" try { $server.ConnectionContext.Connect() } catch { throw "Can't connect to SQL Server." } $server.ConnectionContext.Disconnect()
Connecting to SQL Server Configuration Manager (SqlWmiManagement)
See “Programmatically change SQL Server startup parameters” under Administration for more details.
$servername = "sqlservername\instance" [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") [void][Reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") $server = New-Object Microsoft.SqlServer.Management.Smo.Server $servername $computer = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $server.ComputerNamePhysicalNetBIOS $computer.Services | Select DisplayName
Connecting to a SQL Server Central Management Server
Most code examples show how to connect using a manually specified SqlConnectionObject but did you know you can get the SqlConnectionObject from within SMO’s ConnectionContext?
$cmserver = "sqlservername\instance" [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.RegisteredServers") $server = New-Object Microsoft.SqlServer.Management.Smo.Server $cmserver $sqlconnection = $server.ConnectionContext.SqlConnectionObject try { $cmstore = new-object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($sqlconnection)} catch { throw "Cannot access Central Management Server" }
Is SMO installed on the local computer?
If you don’t have SQL Server or SQL Server Management studio installed on a computer, you will need to manually install SMO to use these scripts. The links provided below will lead you to Microsoft’s SQL Server 2014 Feature Pack page. Click Download, and once the pop-up has appeared, search for your architecture’s SharedManagementObjects.msi file, download and install.
if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") -eq $null ) { throw "Quitting: SMO Required. You can download it from http://goo.gl/R4yA6u" } if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMOExtended") -eq $null ) { throw "Quitting: Extended SMO Required. You can download it from http://goo.gl/R4yA6u" }
Recent Comments