我正在尝试使用 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
}
}