创建Azure SQL数据库服务器连接上下文New-AzureSqlDatabaseServerContext

时间:2016-01-20 01:57:40

标签: powershell azure azure-sql-database azure-powershell

我正在尝试创建Azure SQL数据库连接上下文,例如

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

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

响应是:

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

我一直在浏览文档和谷歌搜索,但无济于事。 https://msdn.microsoft.com/en-us/library/dn546736.aspx http://sqlmag.com/powershell/manage-azure-sql-databases-powershell

由于 帕维尔

2 个答案:

答案 0 :(得分:0)

我最近在PS Runbook中遇到过这个问题。在做了一些搜索后,我发现了一个适合我的解决方案。希望它会对你有所帮助。

错误消息并不特别有用(大惊喜),但引用的null对象是Azure订阅;我假设异常在cmdlet中冒泡而不是由您自己的代码抛出。将这三行添加到我的代码中:

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

我能够超越异常。上面,$automationCertificateName是我添加到自动化帐户的可变资产。有关如何进行设置的详细信息,请参阅https://github.com/Microsoft/sql-server-samples/tree/master/samples/manage/azure-automation-automated-export

答案 1 :(得分:0)

以下是我成功创建连接上下文的方法:

$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

请参阅此more details