SQL Azure 的 Powershell 脚本备份到 Microsoft Azure Blob
Powershell script for SQL Azure back up to Microsoft Azure Blob
我想使用 PowerShell 将 SQL Azure 备份到 Azure Blob 存储。我使用了以下脚本,但每当我尝试 运行.
时,它都会弹出凭据
我将使用 windows 任务计划程序中的这个脚本,那么如何将用户 ID 和密码放入 PowerShell 脚本中,这样它就不会要求 username/password 进行订阅?
$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
可能有两种方法可以实现这一点。
第一种是使用 credential
参数作为 Login-AzureRMAccount
调用的一部分。您可以在 powershell 代码中创建一个 PSCredential,然后使用它。例如:Login-AzureRmAccount -Credential $credential
第二种,也可能是 safer/more 安全的方法,是创建一个服务主体,然后将证书放在有问题的机器上。您可以找到有关如何执行此操作的说明 here. After you have that created, you can use the Login-AzureRMAccount
with the -ServicePrincipal
parameter. See this link 以获取更多信息。
您需要在脚本中实现非交互式登录。只需按如下方式修改您的脚本:
##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 帐户以非交互方式登录 Azure,例如 *@hotmail.com、*@outlook.com.
我想使用 PowerShell 将 SQL Azure 备份到 Azure Blob 存储。我使用了以下脚本,但每当我尝试 运行.
时,它都会弹出凭据我将使用 windows 任务计划程序中的这个脚本,那么如何将用户 ID 和密码放入 PowerShell 脚本中,这样它就不会要求 username/password 进行订阅?
$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
可能有两种方法可以实现这一点。
第一种是使用 credential
参数作为 Login-AzureRMAccount
调用的一部分。您可以在 powershell 代码中创建一个 PSCredential,然后使用它。例如:Login-AzureRmAccount -Credential $credential
第二种,也可能是 safer/more 安全的方法,是创建一个服务主体,然后将证书放在有问题的机器上。您可以找到有关如何执行此操作的说明 here. After you have that created, you can use the Login-AzureRMAccount
with the -ServicePrincipal
parameter. See this link 以获取更多信息。
您需要在脚本中实现非交互式登录。只需按如下方式修改您的脚本:
##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 帐户以非交互方式登录 Azure,例如 *@hotmail.com、*@outlook.com.