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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (66)

我试图使用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 
            }

            }
提问于

扫码关注云+社区

领取腾讯云代金券