简体   繁体   English

如何在 DTU 的 azure 上自动运行此脚本

[英]how can i run this script automatically on azure for DTU

 $ServiceObjective = Get-AzureSqlDatabaseServiceObjective -ServerName exampledb-ServiceObjectiveName S0
 Set-AzureSqlDatabase -DatabaseName exampledb-ServerName exampledb-ServiceObjective $ServiceObjective

I can run above script to make my sql database S0 DTU level,我可以运行上面的脚本来使我的 sql 数据库 S0 DTU 级别,

Can i make this automatically.我可以自动做这个吗?

BTW i searched on forums and stackoverflow, they recommend automation account and runebook.顺便说一句,我在论坛和 stackoverflow 上搜索,他们推荐自动化帐户和 runebook。 But i dont have RunAsAccount.但我没有 RunAsAccount。 I dont have admin privileges and i cant create RunAsAccount.我没有管理员权限,也无法创建 RunAsAccount。 So i couldnt use runbook.所以我不能使用运行手册。

can you recommend me another way?你能推荐我另一种方式吗?

Thanks:)谢谢:)

But i dont have RunAsAccount.但我没有 RunAsAccount。 I dont have admin privileges and i cant create RunAsAccount.我没有管理员权限,也无法创建 RunAsAccount。 So i couldnt use runbook.所以我不能使用运行手册。

If you want to create an Automation account with run as account, you need to have Owner role in your subscription, because when creating the run as account(service principal), it will automatically add the service principal in your subscription as a Contributor , it just can be done with Owner .如果您想创建一个带有运行身份帐户的自动化帐户,您需要在您的订阅中拥有Owner角色,因为在创建运行身份帐户(服务主体)时,它会自动将您的订阅中的服务主体添加为Contributor ,它可以用Owner来完成。

But if you just want to use the runbook in automation account, you don't need the Owner role.但是,如果您只想在自动化帐户中使用 Runbook,则不需要Owner角色。 You just need to ask the Owner in your subscription to create an automation account with run as account for you, then you will be able to create a powershell runbook and run your command above with eg Contributor role.您只需要让订阅中的Owner为您创建一个运行身份帐户的自动化帐户,然后您就可以创建一个 powershell 运行手册并使用例如Contributor角色运行上面的命令。

After the Owner creating the automation account for you, follow the steps below. Owner为您创建自动化帐户后,请按照以下步骤操作。

1.Navigate to the automation account -> Runbooks -> Create a runbook -> create a Powershell runbook. 1.导航到自动化帐户 -> 运行手册 -> 创建运行手册 -> 创建Runbooks运行手册。

2.The two commands Get-AzureSqlDatabaseServiceObjective , Set-AzureSqlDatabase , you are using belong to Azure ie ASM powershell module, it is old, and if you want to use them, you need to use Azure Classic Run As Account(which is not supported in CSP subscription). 2.The two commands Get-AzureSqlDatabaseServiceObjective , Set-AzureSqlDatabase , you are using belong to Azure ie ASM powershell module, it is old, and if you want to use them, you need to use Azure Classic Run As Account(which is not supported在 CSP 订阅中)。 So I recommend you to use the new Az powershell module.所以我推荐你使用新的Az powershell 模块。 In your automation account -> Modules , check if there are Az.Accounts and Az.Sql module, if not, in the Modules -> Browse Gallery , search for the modules and import them.)在您的自动化帐户 -> Modules中,检查是否有Az.AccountsAz.Sql模块,如果没有,在Modules -> Browse Gallery中搜索模块并导入它们。)

After importing successfully, use the script as below to login and set the sql db with Standard S0.导入成功后,使用如下脚本登录并设置 sql db 为 Standard S0。

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Connect-AzAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

Set-AzSqlDatabase -ResourceGroupName "<resource-group-name>" -DatabaseName "<database-name>" -ServerName "<server-name>" -Edition "Standard" -RequestedServiceObjectiveName "S0"

3.If you want to run the script automatically with a schedule, you can follow this link Scheduling a runbook in Azure Automation to do that. 3.如果您想使用计划自动运行脚本,您可以按照此链接在 Azure 自动化中计划运行手册来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM