I am trying to delete the resource groups and resources in it using python code. I have tried this in powershell and it works perfect. Now my organization wanted in Python. I am really new to python and trying to write the code and failed .
Here is the powershell code for the same. Can anyone help on getting the code in python.
Thanks in advance.
$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 '[email protected]' -Subject $subject -Body $body -UseSsl -Port 587 -SmtpServer 'smtp.office365.com' -From $userid -Credential $creds
}
}