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 
            }

            }
4

2 回答 2

1

这是我收到的错误############################ Failed Traceback(最近一次调用最后一次):文件“C:\ Temp\kukzwo13.tgf\9bd12580-5780-4109-abab-66e88fb4df87",第 59 行,如果不是 item.tags['ExpiryDate']:KeyError: 'ExpiryDate'

在 python 代码中,我们需要检查 key 是否对字典有效。我已经做了一个演示,用 python 代码通过标签删除资源组。

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
import datetime
from datetime import time

# Tenant ID for your Azure Subscription
TENANT_ID = 'tenant id '
# Your Service Principal App ID
CLIENT = 'client id'
# Your Service Principal Password
KEY = 'scret key'
subscription_id = 'subscrptionId'
credentials = ServicePrincipalCredentials(client_id=CLIENT, secret=KEY, tenant=TENANT_ID)
client = ResourceManagementClient(credentials, subscription_id)
groups = client.resource_groups.list()
null_variable = None
for item in groups:
    if item.tags != null_variable:
            if 'ExpiryDate' in item.tags:
                expiryDate = datetime.datetime.strptime(item.tags["ExpiryDate"],"%m/%d/%Y")
                timediff = expiryDate < datetime.datetime.today()
                if timediff:
                    print(item.tags["ExpiryDate"])
                    print(item.name)
                    client.resource_groups.delete(item.name)
于 2018-07-30T05:32:33.883 回答
0

我将从示例页面开始: https ://github.com/Azure-Samples/resource-manager-python-resources-and-groups

和支持文档 https://docs.microsoft.com/en-us/python/azure/?view=azure-python

于 2018-07-20T20:45:11.703 回答