agent: |
ZdAzd3zM24VClAMTrF1BDisable Users in Microsoft Entra ID (Azure AD)
Disable Users in Microsoft Entra ID (Azure AD)
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task deactivates user accounts in Azure Active Directory to prevent access to organizational resources. Disabling accounts helps maintain security and manage user privileges effectively within an organization's IT infrastructure.
inputs
outputs
from azure.identity import DefaultAzureCredential
from msgraph.core import GraphClient
# Sample inactive users list
inactive_users = [{'displayName': 'test-user', 'userPrincipalName': 'test-user@yashyadav34gmail.onmicrosoft.com', 'id': '71f44091-8d06-4a8b-8d36-5ff79d753675'}]
# Initialize the DefaultAzureCredential and GraphClient
credential = DefaultAzureCredential()
client = GraphClient(credential=credential, scopes=['https://graph.microsoft.com/.default'])
def disable_user(user_id):
try:
response = client.patch(f'/users/{user_id}', json={'accountEnabled': False})
if response.status_code == 204:
return True
else:
return False
except Exception as e:
print(f"An unexpected error occurred while disabling user {user_id}: {e}")
return False
# Initialize the list for disabled users
disabled_users = []
# Check if there are any inactive users to process
if not inactive_users:
print("No inactive users provided for processing.")
else:
disabled_count = 0
for user in inactive_users:
display_name = user.get('displayName', 'N/A')
upn = user.get('userPrincipalName', 'N/A')
user_id = user.get('id')
print(f"Processing user: {display_name} ({upn})")
# Disable the user account
if disable_user(user_id):
print(f"Successfully disabled user: {display_name} ({upn})")
disabled_users.append(user) # Add the user to the disabled_users list
disabled_count += 1
else:
print(f"Failed to disable user: {display_name} ({upn})")
print("-" * 30)
print(f"Operation completed. {disabled_count} out of {len(inactive_users)} inactive users were disabled.")
#print(f"Disabled Users: {disabled_users}") # for debugging
copied