Error Running Script in Runbook with System Assigned Managed Identity

Aran Billen 556 Reputation points
2024-04-19T11:46:46.9233333+00:00

Hello everyone,

I could use some assistance, please. I'm encountering an error when trying to run a script within a runbook. I'm using PowerShell 5.1 with a system-assigned managed identity.

The script works find without using the managed identiy via powershell outside of azure.

Error:

System.Management.Automation.ParameterBindingException: Cannot process command because of one or more missing mandatory parameters: Credential. at System.Management.Automation.CmdletParameterBinderController.PromptForMissingMandatoryParameters(Collection1 fieldDescriptionList, Collection1 missingMandatoryParameters) at System.Management.Automation.CmdletParameterBinderController.HandleUnboundMandatoryParameters

I am using this script

Connect-ExchangeOnline -ManagedIdentity -Organization domain removed for privacy reasons

# Specify the user's mailbox identity
$mailboxIdentity = "email address removed for privacy reasons"

# Get mailbox configuration and statistics for the specified mailbox
$mailboxConfig = Get-Mailbox -Identity $mailboxIdentity
$mailboxStats = Get-MailboxStatistics -Identity $mailboxIdentity

# Check if TotalItemSize and ProhibitSendQuota are not null and extract the sizes
if ($mailboxStats.TotalItemSize -and $mailboxConfig.ProhibitSendQuota) {
    $totalSizeBytes = $mailboxStats.TotalItemSize.Value.ToString().Split("(")[1].Split(" ")[0].Replace(",", "") -as [double]
    $prohibitQuotaBytes = $mailboxConfig.ProhibitSendQuota.ToString().Split("(")[1].Split(" ")[0].Replace(",", "") -as [double]

    # Convert sizes from bytes to gigabytes
    $totalMailboxSize = $totalSizeBytes / 1GB
    $mailboxWarningQuota = $prohibitQuotaBytes / 1GB

    # Check if the mailbox size exceeds 90% of the warning quota
    if ($totalMailboxSize -ge ($mailboxWarningQuota * 0.0)) {
        # Send an email notification
        $emailBody = "The mailbox $($mailboxIdentity) has reached $($totalMailboxSize) GB, which exceeds 90% of the warning quota."

        Send-MailMessage -To "email address removed for privacy reasons" -From "email address removed for privacy reasons" -Subject "Mailbox Size Warning" -Body $emailBody -SmtpServer "smtp.office365.com" -Port 587 -UseSsl -Credential (Get-Credential)
    }
} else {
    Write-Host "The required values(TotalItemSize or ProhibitSendQuota) are not available."
}

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,087 questions
0 comments No comments
{count} votes