agent: |
WbSuWk8UcmuwTCRA9MFfEnable Azure User Account and verify account status
Enable Azure User Account and verify account status
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task activates a user's account in Azure Active Directory and confirms its active status, ensuring the user can immediately access their Microsoft 365 services and other resources.
inputs
outputs
import requests
from azure.identity import ClientSecretCredential
# Set up Azure AD credentials
tenant_id = 'your-tenant-id'
client_id = "your-client-id"
client_secret = "your-client-secret"
# Initialize the Azure credentials using Azure's ClientSecretCredential
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
# Define the User Principal Name (UPN) of the user to be modified
# user_principal_name = "johndoe@yashyadav34gmail.onmicrosoft.com" # Specify the user_principal_name like this if running the task in a standalone manner
# user_info to be received from upstream task
user_principal_name = user_info["userPrincipalName"]
# Function to enable or disable a user account
def update_user_account_enabled(user_principal_name, enable=True):
# Construct the Microsoft Graph API URL for updating user details
url = f"https://graph.microsoft.com/v1.0/users/{user_principal_name}"
# Obtain an access token from Azure AD
access_token = credential.get_token('https://graph.microsoft.com/.default').token
# Set the authorization header with the bearer token
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}
# Define the payload to update the 'accountEnabled' field
payload = {"accountEnabled": enable}
# Send a PATCH request to update the user's account status
response = requests.patch(url, headers=headers, json=payload)
try:
response.raise_for_status() # This raises an HTTPError for bad requests (400 or 500 level)
if response.status_code == 204:
print("Update successful, no content returned.") # No content is expected on successful PATCH
elif response.text:
print("Update successful:", response.json()) # Print the response if any content is returned
else:
print("Update successful, no content to display.")
except requests.exceptions.HTTPError as e:
print(f"Failed to update user: {e.response.status_code} - {e.response.text}")
# Function to retrieve the 'accountEnabled' status of a user
def get_user_account_enabled(user_principal_name):
# Construct the URL with a query parameter to select specific fields
url = f"https://graph.microsoft.com/v1.0/users/{user_principal_name}?$select=displayName,accountEnabled"
# Obtain an access token from Azure AD
access_token = credential.get_token('https://graph.microsoft.com/.default').token
# Set the authorization header with the bearer token
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}
# Send a GET request to retrieve user details
response = requests.get(url, headers=headers)
try:
response.raise_for_status() # This raises an HTTPError for bad HTTP responses
if response.text:
user_data = response.json()
print(user_data)
account_status = user_data.get("accountEnabled")
print("User account status:", "Enabled" if account_status else "Disabled or not set")
else:
print("No data returned for user.")
except requests.exceptions.HTTPError as e:
print(f"Failed to retrieve user: {e.response.status_code} - {e.response.text}")
# Example usage of the functions
update_user_account_enabled(user_principal_name, enable=True) # Enable the user account
current_status = get_user_account_enabled(user_principal_name) # Retrieve the current account status
"""
# Sample Output
Update successful, no content returned.
{'@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#users(displayName,accountEnabled)/$entity', 'displayName': 'John Doe', 'accountEnabled': True}
User account status: Enabled
"""
copied