Jack0fshad0ws
Jack0fshad0ws

Reputation: 571

Creating Azure SQL Database Server connection context New-AzureSqlDatabaseServerContext

I am trying to create an Azure SQL Database connection context e.g.

$cred = Get-Credential
$ctx = New-AzureSqlDatabaseServerContext -ServerName “mydatabasename” -credential $cred

or

$pwd = ConvertTo-SecureString "[password1234]" -AsPlainText -Force;
$cred1 = New-Object System.Management.Automation.PSCredential -ArgumentList "databaseadmin", $pwd 
New-AzureSqlDatabaseServerContext -ServerName "myservername" -Credential $cred1

And the response is:

New-AzureSqlDatabaseServerContext : Object reference not set to an instance of an object.
At line:2 char:8
+ $ctx = New-AzureSqlDatabaseServerContext -ServerName “myservername” - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureSqlDatabaseServerContext], NullReferenceException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.SqlDatabase.Database.Cmdlet.NewAzureSqlDatabaseServerContext

I've been through the docs and google searches but to no avail. https://msdn.microsoft.com/en-us/library/dn546736.aspx http://sqlmag.com/powershell/manage-azure-sql-databases-powershell

Thanks Pavel

Upvotes: 2

Views: 1037

Answers (2)

Andrei
Andrei

Reputation: 44700

Here is how I successfully created connection context:

$sqlServerUser = "test-user"
$sqlServerUserPassword = "P@$$w0rd"
$sqlServerName = "mysqlserver"

$sqlCred = New-Object System.Management.Automation.PSCredential($sqlServerUser, ($sqlServerUserPassword | ConvertTo-SecureString -asPlainText -Force))
$sqlContext = New-AzureSqlDatabaseServerContext -ServerName $sqlServerName -Credential $sqlCred

Please see this for more details.

Upvotes: 0

jvance
jvance

Reputation: 551

I recently ran into this issue in a PS runbook. After doing some searching, I found a solution that worked for me. Hopefully it will help you.

The error message isn't particularly helpful (big surprise), but the null object being referenced is the Azure subscription; I'm assuming the exception bubbles up from within the cmdlet rather than being thrown by your own code. By adding these three lines to my code:

$cert = Get-AutomationCertificate -Name $automationCertificateName;
Set-AzureSubscription -SubscriptionName $subName -Certificate $cert -SubscriptionId $subID;
Select-AzureSubscription -Current $subName;

I was able to get past the exception. Above, $automationCertificateName is a variable asset that I added to the automation account. See https://github.com/Microsoft/sql-server-samples/tree/master/samples/manage/azure-automation-automated-export for details about how to set that up.

Upvotes: 0

Related Questions