2017-04-15 96 views
1

我想使用PowerShell將SQL Azure備份到Azure Blob存儲。我已經使用了以下腳本,但每當我嘗試運行時,它都會彈出憑據。SQL Azure的Powershell腳本備份到Microsoft Azure Blob

我將在Windows任務調度程序中使用此腳本,因此如何將用戶標識和密碼放入PowerShell腳本中,以便它不會要求用戶名/密碼進行訂閱?

$subscriptionId = "xxxxxxxxxxxxxxxxxxxxx" 

Login-AzureRmAccount 
Set-AzureRmContext -SubscriptionId $subscriptionId 

# Database to export 
$DatabaseName = "xxxxxxxxxxx" 
$ResourceGroupName = "xxxxxxxxxxx" 
$ServerName = "xxxxxxxxxxxx" 
$serverAdmin = "xxxxxxxxxxxxxxx" 
$serverPassword = "xxxxxxxxxxx" 
$securePassword = ConvertTo-SecureString -String $serverPassword -AsPlainText -Force 
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $serverAdmin, $securePassword 

# Generate a unique filename for the BACPAC 
$bacpacFilename = $DatabaseName + (Get-Date).ToString("yyyyMMddHHmm") + ".bacpac" 


# Storage account info for the BACPAC 
$BaseStorageUri = "https://xxxxxxx.blob.core.windows.net/xxxxx/" 
$BacpacUri = $BaseStorageUri + $bacpacFilename 
$StorageKeytype = "StorageAccessKey" 
$StorageKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 

$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName ` 
    -DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri ` 
    -AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password 

回答

0

您需要在腳本中實現非交互式登錄。只需要修改你的腳本如下:

##login Azure 
$subscriptionId = "xxxxxxxxxxxxxxxxxxxxx" 
$username = "<username>" 
$password = "<password>" 
$secstr = New-Object -TypeName System.Security.SecureString 
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)} 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr 
Login-AzureRmAccount -Credential $cred 
Select-AzureRmSubscription -SubscriptionId $subscriptionId 

注:使用微軟Live帳戶,如*@hotmail.com,*@outlook.com您無法登錄非intereractively天青。

0

可能有兩種方法可以實現此目的。

第一個是使用credential參數作爲Login-AzureRMAccount調用的一部分。您可以在PowerShell代碼中創建一個PSCredential,然後使用它。例如:Login-AzureRmAccount -Credential $credential

第二種,也許是更安全/更安全的方法,是創建一個服務主體,然後將證書放在有問題的機器上。你可以找到如何做到這一點的說明here。創建完成後,可以使用Login-AzureRMAccount-ServicePrincipal參數。有關更多信息,請參閱this link