-
-
Save w3bward/0844df5ec5585c507fbb248eaa84404d to your computer and use it in GitHub Desktop.
SMO example using an AD (or AAD) username and password for authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Server = "myazuresqlserver.database.windows.net" | |
$Database = "MyDatabase" | |
$Credential = Get-Credential | |
# Step 1, create the SqlConnectionInfo object | |
$ConnectionInfo = [Microsoft.SqlServer.Management.Common.SqlConnectionInfo]::new($Server) | |
$ConnectionInfo.DatabaseName = $Database | |
$ConnectionInfo.Authentication = [Microsoft.SqlServer.Management.Common.SqlConnectionInfo+AuthenticationMethod]::ActiveDirectoryPassword | |
$ConnectionInfo.SecurePassword = $Credential.Password | |
$ConnectionInfo.UserName = $Credential.UserName | |
# Step 2, create the ServerConnection object | |
$ServerConnection = [Microsoft.SqlServer.Management.Common.ServerConnection]::new($ConnectionInfo) | |
# Step 3 Create the SMO Server object | |
$ServerInstance = [Microsoft.SqlServer.Management.Smo.Server]::new($ServerConnection) | |
# (Optional) Step 4 Get an object representing the target database | |
$DatabaseInstance = $ServerInstance.Databases | where Name -EQ $Database |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment