azure - 如何使用 powershell 重新启动容器实例

标签 azure powershell automation powershell-cmdlet

我尝试使用 PowerShell 设置自动化作业来重新启动容器实例。但似乎没有内置的 PowerShell 模块/cmdlet 可以提供帮助

我可以使用 azure CLI 重新启动容器实例 ( https://learn.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az-container-restart )

az container restart --name <name> --resource-group <group>

有没有办法使用 Powershell 执行相同的操作。

提前致谢

最佳答案

Azure PowerShell不提供直接重启容器实例的建议,但我们可以使用powershell调用Azure管理API来重启容器实例作为解决方法。

  1. 在 Azure AD 中注册应用程序,为其创建客户端 key ,记下其值和应用程序 ID(您可以在概述中找到它): enter image description here

enter image description here

  • 为其分配贡献者角色,以便其有权管理您的 Azure 资源: enter image description here
  • 3.尝试下面的 powershell 脚本来重新启动容器实例:

    $appid = "<application ID we resistered>"
    $appSecret= "<application client secret we created>"
    $tenant = "<your tenant ID/NAME>"
    
    $resourceGroup = "<resource group>"
    $containerInstanceName = "<containerInstances name>"
    $subscrptionId = "<subscription ID>"
    
    $body=@{
        "grant_type"="client_credentials";
        "resource"="https://management.azure.com/";
        "client_id"=$appid;
        "client_secret"=$appSecret
    }
     
    $result=Invoke-RestMethod -Uri "https://login.windows.net/$tenant/oauth2/token" -Method POST -Body $body 
    $accessToken = $result.access_token
    
    Invoke-RestMethod -Uri "https://management.azure.com/subscriptions/$subscrptionId/resourceGroups/$resourceGroup/providers/Microsoft.ContainerInstance/containerGroups/$containerInstanceName/restart?api-version=2018-10-01" -Method POST -Headers @{"Authorization"="Bearer $accessToken"} 
    

    结果: enter image description here

    关于azure - 如何使用 powershell 重新启动容器实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59198965/

    相关文章:

    automation - 将 Restful /修昔底德与史波克相结合

    python - 有没有好的方法将python程序拆分成独立的模块?

    testing - 如何激活 CATJS 中的错误捕获机制?

    .net - MS Graph UserPrincipalName 与 OID 一起返回

    windows - Azure 虚拟机 (Windows) 的最佳工作流程是什么?

    azure - 如何使用 AAD 身份验证在 powershell 中调用 Azure CosmosDB REST API

    powershell - 如何在 Azure Devops 的任务中运行带参数的 exe?

    azure - 检查 MVC 应用程序中的用户角色以获取 Azure Active Directory 的授权

    laravel - php artisan 在 Azure 上迁移(在 BitBucket 管道中)

    powershell - 使用Powershell查找Open Metro应用