Unable add module "Microsoft.graph" for a runbook.

Jatin Lingwal 20 Reputation points
2023-08-23T15:26:12.99+00:00

User's image

When I am trying to add a module by using the Azure portal, it shows approximately 32 dependencies that need to be added first, but after that, it also gives the same result. For the above image, I have used Azure CLI.
$moduleName = <ModuleName>

$moduleVersion = <ModuleVersion>

New-AzAutomationModule -AutomationAccountName <AutomationAccountName> -ResourceGroupName <ResourceGroupName> -Name $moduleName -ContentLinkUri "https://www.powershellgallery.com/api/v2/package/$moduleName/$moduleVersion"
The output was positive, but it failed to import the module without giving any error.

# Import the necessary modules
Import-Module Az.Accounts
Import-Module Az.Resources
Import-Module Az.Automation
Import-Module Microsoft.Graph

# Replace with your actual values
$subscriptionId = "<Your Subscription ID>"
$resourceGroupName = "<Your Resource Group Name>"
$automationAccountName = "<Your Automation Account Name>"
$appIds = @("-", "-")
$userId = "-"
$managedIdentityObjectId = "<Your Managed Identity Object ID>"

# Connect to Azure using the system managed identity of the automation account
Connect-AzAccount -Identity

# Retrieve secrets' expiration dates from Azure AD App credentials
$secretsExpiration = @()
foreach ($appId in $appIds) {
    $appSecrets = Get-AzADAppCredential -ApplicationId $appId
    if ($appSecrets -ne $null) {
        $secretsExpiration += @{
            AppId = $appId
            ExpirationDate = $appSecrets.EndDate
        }
    }
}

# Format secrets' expiration dates
$expirationDatesString = $secretsExpiration | ForEach-Object {
    "AppId: $($_.AppId)`r`nExpiration Date: $($_.ExpirationDate)`r`n"
} | Out-String

# Prepare email content
$emailContent = @{
    subject = "Secret Expiration Reminder from Azure Automation"
    body = @{
        contentType = "Text"
        content = @"
Hi Team,

Here are the expiration dates of the secrets for the App Registrations:

$expirationDatesString

Please update the secrets before they expire.

Best regards,
Jatin
"@
    }
    toRecipients = @(@{emailAddress = @{address = $userId}})
}

# Convert the email content to JSON
$jsonContent = $emailContent | ConvertTo-Json

# Acquire access token using Managed Identity
$graphApiToken = Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com" -ObjectId $managedIdentityObjectId

# Construct the REST API URL
$apiUrl = "https://graph.microsoft.com/v1.0/me/sendMail"

# Send the email using the Outlook REST API
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers @{
    Authorization = "Bearer $($graphApiToken.Token)"
    "Content-Type" = "application/json"
} -Body $jsonContent

# Check the response status
if ($response.StatusCode -eq 202) {
    Write-Output "Email sent successfully."
} else {
    Write-Output "Failed to send email."
    Write-Output "Response: $($response | ConvertTo-Json -Depth 4)"
}

This script is to get notified of expired app secrets. Please let me know what can be done to address this issue.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,368 questions
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. K Sheahan Kularathne 436 Reputation points
    2023-09-04T15:59:02.2333333+00:00

    Hi @Jatin Lingwal

    Can you try using Browse Gallery option to install the module, this way it will give you more information when it's failed.

    Please do not forget to "Accept the answer" wherever the information provided helps you to help others in the community.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.