2017-04-15 97 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