Azure功能角色,如停止Azure虚拟机的权限

use*_*163 1 c# azure azure-functions

我希望使用预定的C#Azure功能管理一些Azure资源.

目前在我已经创建的命令行应用程序中,我一直在使用库"Microsoft.IdentityModel.Clients.ActiveDirectory"进行令牌授权,使用"Microsoft.Azure.Management.Compute"进行客户端调用以进行资源管理.

//... var credential generated my AD authentication and extending Microsoft.Rest.ServiceClientCredentials
using (var client = new ComputeManagementClient(credential)) {
    client.SubscriptionId = "[SOME_SUBSCRIPTION_ID]";
    client.VirtualMachines.BeginPowerOff("[RESOURCE_GROUP]", "[VM_NAME]");
}
Run Code Online (Sandbox Code Playgroud)

我的管理客户端是否可以与Azure资源交互而无需提供凭据建立的用户凭据或密钥保密?

我以前的经验与AWS有关,而且它确实混淆了我对Azure资源管理的看法.

我看过的旧帖子是:启动和停止Azure虚拟机

是否可以从Azure功能停止/启动Azure ARM虚拟机?

-EDIT 1-

我希望基于具有各种权限的已分配角色,在AWS资源客户端中为Lambda提供类似于运行时凭据的内容.我会看看证书.

Lin*_*Toh 7

在线使用C#进行REST API调用以启动和停止VM.这是这样一个文档的链接:

https://msftstack.wordpress.com/2016/01/03/how-to-call-the-azure-resource-manager-rest-api-from-c/

您可以使用上面的参考来创建C#函数来启动/停止您的VM.

但是,使用C#进行这些REST调用需要预先打包HTTP请求并对HTTP响应进行后处理.如果您的用例只是要求启动/停止VM,则更简单的方法是使用Azure Functions中的PowerShell来调用Start-AzureRmVMStop-AzureRmVMcmdlet.

以下是有关如何创建HTTP触发的PowerShell函数以启动和停止VM的步骤:

  1. 设置服务主体以获取用户名,密码和租户ID.某些用户可能认为这种初始设置很乏味,但由于这是一次性任务,我觉得在功能中利用运行Azure PowerShell是值得的.网上有很多文档,但是这里有一些指向如何设置服务主体的文档的链接:

    一世.http://blog.davidebbo.com/2014/12/azure-service-principal.html(我用过这个)

    II.https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal

  2. 登录函数门户以访问您的Function应用程序.

  3. 点击功能的应用程序设置- >配置应用程序设置并添加键值对的设置SP_USERNAME,SP_PASSWORD以及TENANTID(您可以使用其他需要的键名).

  4. 创建一个名为的HTTP触发的PowerShell函数,例如StartVm,其run.ps1文件中包含以下内容.

    $requestBody = Get-Content $req -Raw | ConvertFrom-Json

    # Set Service Principal credentials
    # SP_PASSWORD, SP_USERNAME, TENANTID are app settings
    $secpasswd = ConvertTo-SecureString $env:SP_PASSWORD -AsPlainText -Force;
    $mycreds = New-Object System.Management.Automation.PSCredential ($env:SP_USERNAME, $secpasswd)
    Add-AzureRmAccount -ServicePrincipal -Tenant $env:TENANTID -Credential $mycreds;
    $context = Get-AzureRmContext;
    Set-AzureRmContext -Context $context;

    # Start VM
    Start-AzureRmVM -ResourceGroupName $requestBody.resourcegroup -Name  $requestBody.vmname | Out-String

  1. 单击" 保存"按钮.

  2. 接下来,单击" 日志"按钮以打开日志查看器.

  3. 单击" 测试"按钮以打开简单的HTTP客户端.在请求正文中,提供VM的vmname和resourcegroup值,例如

    {
        "vmname": "testvm",
        "resourcegroup": "testresourcegroup"
    }
  1. 单击" 运行"按钮并等待几秒钟.Start-AzureRmVMcmdlet运行完成需要一些时间.如果是这样,您应该在日志查看器中看到类似的条目.

    2016-11-30T07:11:26.479 Function started (Id=1e38ae2c-3cca-4e2f-a85d-f62c0d565c34)
    2016-11-30T07:11:28.276 Microsoft.Azure.Commands.Profile.Models.PSAzureContext
    2016-11-30T07:11:28.276 Microsoft.Azure.Commands.Profile.Models.PSAzureContext
    2016-11-30T07:11:59.312 RequestId            IsSuccessStatusCode          StatusCode ReasonPhrase       
    ---------            -------------------          ---------- ------------       
                                        True                  OK OK
    2016-11-30T07:11:59.327 Function completed (Success, Id=1e38ae2c-3cca-4e2f-a85d-f62c0d565c34)

  1. 重复步骤4-8以在其文件中创建具有以下内容的StopVm函数run.ps1.如果执行成功,则日志输出应类似于StartVm函数的日志条目.

    $requestBody = Get-Content $req -Raw | ConvertFrom-Json

    # Set Service Principal credentials
    # SP_PASSWORD, SP_USERNAME, TENANTID are app settings
    $secpasswd = ConvertTo-SecureString $env:SP_PASSWORD -AsPlainText -Force;
    $mycreds = New-Object System.Management.Automation.PSCredential ($env:SP_USERNAME, $secpasswd)
    Add-AzureRmAccount -ServicePrincipal -Tenant $env:TENANTID -Credential $mycreds;
    $context = Get-AzureRmContext;
    Set-AzureRmContext -Context $context;

    # Stop VM
    Stop-AzureRmVM -ResourceGroupName $requestBody.resourcegroup -Name $requestBody.vmname -Force | Out-String

  1. StopVm函数执行成功时,您还可以在其文件中添加另一个具有以下内容的GetVm函数,run.ps1以验证VM是否确实已停止.

    $requestBody = Get-Content $req -Raw | ConvertFrom-Json

    # Set Service Principal credentials
    # SP_PASSWORD, SP_USERNAME, TENANTID are app settings
    $secpasswd = ConvertTo-SecureString $env:SP_PASSWORD -AsPlainText -Force;
    $mycreds = New-Object System.Management.Automation.PSCredential ($env:SP_USERNAME, $secpasswd)
    Add-AzureRmAccount -ServicePrincipal -Tenant $env:TENANTID -Credential $mycreds;
    $context = Get-AzureRmContext;
    Set-AzureRmContext -Context $context;

    # Get VM
    Get-AzureRmVM -ResourceGroupName $requestBody.resourcegroup -Name $requestBody.vmname -Status | Out-String

停止的VM上的GetVM功能的日志条目将类似于以下内容:


    2016-11-30T07:53:59.956 Function started (Id=1841757f-bbb8-45cb-8777-80edb4e75ced)
    2016-11-30T07:54:02.040 Microsoft.Azure.Commands.Profile.Models.PSAzureContext
    2016-11-30T07:54:02.040 Microsoft.Azure.Commands.Profile.Models.PSAzureContext
    2016-11-30T07:54:02.977 ResourceGroupName          : testresourcegroup
    Name                       : testvm
    BootDiagnostics            : 
      ConsoleScreenshotBlobUri : https://teststorage.blob.core.windows.net/boot
    diagnostics-vmtest-[someguid]/testvm.[someguid].screenshot.bmp
    Disks[0]                   : 
      Name                     : windowsvmosdisk
      Statuses[0]              : 
        Code                   : ProvisioningState/succeeded
        Level                  : Info
        DisplayStatus          : Provisioning succeeded
        Time                   : 11/30/2016 7:15:15 AM
    Extensions[0]              : 
      Name                     : BGInfo
    VMAgent                    : 
      VmAgentVersion           : Unknown
      Statuses[0]              : 
        Code                   : ProvisioningState/Unavailable
        Level                  : Warning
        DisplayStatus          : Not Ready
        Message                : VM Agent is unresponsive.
        Time                   : 11/30/2016 7:54:02 AM
    Statuses[0]                : 
      Code                     : ProvisioningState/succeeded
      Level                    : Info
      DisplayStatus            : Provisioning succeeded
      Time                     : 11/30/2016 7:15:15 AM
    Statuses[1]                : 
      Code                     : PowerState/deallocated
      Level                    : Info
      DisplayStatus            : VM deallocated
    2016-11-30T07:54:02.977 Function completed (Success, Id=1841757f-bbb8-45cb-8777-80edb4e75ced)

注意:仅供参考,虽然您可以通过调用New-AzureRmVMcmdlet 编写函数来创建VM ,但它不会在Azure Functions中运行完成.Azure Function的基础架构中的VM创建似乎需要大约9分钟才能完成,但Function的执行在5分钟后终止.您可以编写另一个脚本来单独轮询结果.当我们在即将发布的一个版本中开始支持自定义配置以实现最长执行时间时,将解除此限制.

- 更新 - 我刚刚意识到你正在尝试创建预定的功能.在这种情况下,您可以使用Timer触发的PowerShell函数并对vmname和resourcegroup进行硬编码.