首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用资源组标签在azure中删除资源组代码的Python代码

使用资源组标签在azure中删除资源组代码的Python代码
EN

Stack Overflow用户
提问于 2018-07-20 19:36:38
回答 1查看 659关注 0票数 0

我正在尝试使用python代码删除其中的资源组和资源。我已经在powershell中尝试过了,它工作得很好。现在我的组织需要Python。我是python的新手,试着写代码却失败了。

下面是同样的powershell代码。有没有人能帮我弄到python的代码?

提前谢谢。

 $rgs = Get-AzureRmResourceGroup; 

$rgs=Get-AzureRmResourceGroup -name "TestResourceGroupToClean1";

if(!$rgs)
{ 
    Write-Output "No resource groups in your subscription"; 
} 
else{


    Write-Output "You have $($(Get-AzureRmResourceGroup).Count) resource groups in your subscription"; 
    foreach($resourceGroup in $rgs)
    { 
        $name=  $resourceGroup.ResourceGroupName; 
     if($resourceGroup.Tags.ExpiryDate)
     {
    try{
         $ResourceGroupTagDate=[datetime]::ParseExact($resourceGroup.Tags.ExpiryDate,'MM/dd/yyyy',$null)
         $count = (Get-AzureRmResource | where { $_.ResourceGroupName -match $name }).Count;
            if($ResourceGroupTagDate.Date -lt $today.Date)
            { 
                $subject="Automated Mail from Resource Group Cleaner"
                $body="Resource Group $($resourceGroup.ResourceGroupName) including resources has been deleted"
                Write-Output "The resource group $name has $count resources. Deleting it..."; 
                Remove-AzureRmResourceGroup -Name $resourceGroup.ResourceGroupName -Force; 
                Write-Output "The resource group $name and $count resources. Deleted..";
                Send-MailMessage -To 'XXXX@XXXXXXXX.com' -Subject $subject -Body $body -UseSsl  -Port 587 -SmtpServer 'smtp.office365.com' -From $userid -Credential $creds 
            }

            }
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51441665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档