我的要求是让订阅中的所有虚拟机都具有启动(创建)时间。我没有在仪表板中找到虚拟机创建时间,就像在Activity log
找到时间戳中一样。我想获取由一个订阅 ID 创建的所有虚拟机以及创建时间。
(对于此帐户详细信息,2FA 已启用,因此 - UserPassCredentials将不起作用)
订阅 ID 中所有 VM 的列表:
import os
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
credentials = ServicePrincipalCredentials(client_id=os.environ['AZURE_CLIENT_ID'], secret=os.environ['AZURE_CLIENT_SECRET'], tenant=os.environ['AZURE_TENANT_ID'])
compute_client = ComputeManagementClient(credentials, subscription_id)
for vm in compute_client.virtual_machines.list_all():
print("\tVM: {}".format(vm.name))
从以下位置获取创建时间Activity log
:
import os
import datetime
from pprint import pprint
from azure.monitor import MonitorClient
from azure.common.credentials import ServicePrincipalCredentials
today = datetime.datetime.now().date()
filter = " and ".join([ "eventTimestamp le '{}T00:00:00Z'".format(today), "resourceGroupName eq 'test-group'" ])
subscription_id = 'xxxxx'
credentials = ServicePrincipalCredentials(client_id=os.environ['AZURE_CLIENT_ID'], secret=os.environ['AZURE_CLIENT_SECRET'], tenant=os.environ['AZURE_TENANT_ID'])
client = MonitorClient(credentials, subscription_id)
select = ",".join([ "Administrative", "Write VirtualMachines" ])
activity_logs = client.activity_logs.list( filter=filter, select=select )
for i in activity_logs:
pprint(i.__dict__)
我能够获取所有虚拟机(第一个示例程序),但是在尝试Activity log
获取一些错误(第二个示例程序)时。
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/msrest/paging.py", line 109, in __next__
self.advance_page()
File "/Library/Python/2.7/site-packages/msrest/paging.py", line 95, in advance_page
self._response = self._get_next(self.next_link)
File "/Library/Python/2.7/site-packages/azure/monitor/operations/activity_logs_operations.py", line 117, in internal_paging
raise models.ErrorResponseException(self._deserialize, response)
azure.monitor.models.error_response.ErrorResponseException: Operation returned an invalid status code 'Bad Request'
有人可以帮我找到问题吗?任何帮助真的很感激。