0

Trying to get the list unattached disks/orphan disks on Azure automation account using python sdk runbook version is 3.8, getting error as ** Failed Traceback (most recent call last): File "C:\Temp\0np1hfy2.ldk\4504115d-cee5-4b9d-b712-cc053092810e", line 15, in for disk in disks: File "C:\WPy64-3800\python-3.8.0.amd64\lib\site-packages\msrest\paging.py", line 143, in next self.advance_page() File "C:\WPy64-3800\python-3.8.0.amd64\lib\site-packages\msrest\paging.py", line 129, in advance_page self.response = line 336, in send pipeline_response = self.config.pipeline.run(request, **kwargs) File "C:\WPy64-3800\python-3.8.0.amd64\lib\site-packages\msrest\pipeline_init.py", line 197, in run return first_node.send(pipeline_request, **kwargs) # type: ignore File "C:\WPy64-3800\python-3.8.0.amd64\lib\site-packages\msrest\pipeline_init_.py", line 150, in send response = self.next.send(request, **kwargs) File "C:\WPy64-3800\python-3.8.0.amd64\lib\site-packages\msrest\pipeline\requests.py", line 65, in send self._creds.signed_session(session)AttributeError: 'DefaultAzureCredential' object has no attribute 'signed_session' ** Below is the script

#!/usr/bin/env python3
import os
import json
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient

credential = DefaultAzureCredential()

subscription_id = "id"
compute_client = ComputeManagementClient(credential, subscription_id)

disks = compute_client.disks.list()

orphaned_disks = []
for disk in disks:
    if disk.managed_by is None:
        orphaned_disks.append(disk)

print("Orphaned disks:")
for disk in orphaned_disks:
    print(disk.name)

Assist me to solve this. Thanks in advance!

User
  • 31
  • 4

1 Answers1

0

I used the below sample and worked fine in my local environment. Could you please run the same below sample locally and confirm ? Then, please run the same sample on your Automation account.

Please note that, before running the sample, ensure that you are using all updated python packages. Example:

pip install --upgrade azure-identity

#!/usr/bin/env python3
import os
import json
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.resource import ResourceManagementClient

credential = DefaultAzureCredential()

subscription_id = "XXXX-XXXX-XXXX-XXXX"

compute_client = ComputeManagementClient(credential, subscription_id)
resource_client = ResourceManagementClient(credential, subscription_id)

# a list to store the names of orphaned disks
orphaned_disks = []

# iterate over all disks
for disk in compute_client.disks.list():
    # check if disk is managed, if not it's orphaned
    if disk.managed_by is None:
        # get the resource of the disk
        disk_resource = resource_client.resources.get_by_id(disk.id,api_version='2023-01-02')
        # check if resource type is a "disk" resource
        if disk_resource.type == 'Microsoft.Compute/disks':
            # add disk name to list
            orphaned_disks.append(disk.name)

# print list of orphaned disks
print("Orphaned disks:")
for disk in orphaned_disks:
    print(disk)
NaveenBaliga
  • 449
  • 1
  • 2
  • 5