2016-01-20 22 views
2

我想創建一個Azure SQL數據庫連接上下文例如創建Azure SQL數據庫服務器連接上下文新AzureSqlDatabaseServerContext

$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

感謝 帕維爾

+0

什麼是您正在使用的Microsoft Azure PowerShell版本? – juvchan

+0

這是否始終如一地重現? – juvchan

+0

是的,始終如一地轉載。 版本:1.0.3 名稱:Azure 作者:Microsoft Corporation PowerShellVersion:3.0 – Jack0fshad0ws

回答

0

我最近就遇到了這個問題,在PS運行手冊。在做了一些搜索之後,我找到了一個適合我的解決方案。希望它會幫助你。

錯誤消息不是特別有用(大驚喜),但被引用的空對象是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

0

下面是我成功創建連接上下文:

$sqlServerUser = "test-user" 
$sqlServerUserPassword = "[email protected]$$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

相關問題