【问题标题】:Powershell script for SQL Azure back up to Microsoft Azure Blob用于将 SQL Azure 备份到 Microsoft Azure Blob 的 Powershell 脚本
【发布时间】:2017-04-15 18:53:23
【问题描述】:

我想使用 PowerShell 将 SQL Azure 备份到 Azure Blob 存储。我使用了以下脚本,但每当我尝试运行时,它都会弹出以获取凭据。

我将使用 Windows 任务调度程序中的这个脚本,那么如何将用户 ID 和密码放入 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

【问题讨论】:

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


    【解决方案1】:

    您需要在脚本中实现非交互式登录。只需修改您的脚本如下:

    ##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
    

    注意:您不能使用 Microsoft Live 帐户(例如 *@hotmail.com、*@outlook.com)以非交互方式登录 Azure。

    【讨论】:

      【解决方案2】:

      您可能有两种方法可以做到这一点。

      第一种是使用credential 参数作为Login-AzureRMAccount 调用的一部分。您可以在 powershell 代码中创建一个 PSCredential 然后使用它。例如:Login-AzureRmAccount -Credential $credential

      第二种方法,可能是更安全/更安全的方法,是创建一个服务主体,然后将证书放在相关机器上。您可以找到有关如何执行此操作的说明here。创建完成后,您可以将Login-AzureRMAccount-ServicePrincipal 参数一起使用。请参阅this link 了解更多信息。

      【讨论】:

        猜你喜欢
        • 2021-05-31
        • 2013-03-21
        • 1970-01-01
        • 2018-10-10
        • 2017-05-25
        • 2015-01-08
        • 2010-12-15
        • 1970-01-01
        • 2014-08-04
        相关资源
        最近更新 更多