gpt4 book ai didi

用于将 SQL Azure 备份到 Microsoft Azure Blob 的 Powershell 脚本

转载 作者:行者123 更新时间:2023-12-03 01:48:02 25 4
gpt4 key购买 nike

我想使用 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

最佳答案

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

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

关于用于将 SQL Azure 备份到 Microsoft Azure Blob 的 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43430001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com