Error while excuting runbook in Azure Automation account

Chinmayi Bose 0 Reputation points
2025-08-25T08:09:35.43+00:00

Hi Team,

Could you please provide guidance and troubleshooting steps for an authentication issue I am encountering while attempting to automate the process of identifying inactive Azure AD accounts? I am working on creating a runbook to find these accounts and schedule a weekly email report, a task I currently perform manually.

Below is the script I am running manually.

Install-Module Microsoft.Graph -Scope CurrentUser

Connect-MgGraph -Scopes AuditLog.Read.All,Directory.Read.All

 $DaysInactive = 30

$CutoffDate = (Get-Date).AddDays(-$DaysInactive)

 # Retrieve all enabled users and sign-in activity

$Users = Get-MgUser -All:$true -Property UserPrincipalName, DisplayName, AccountEnabled, SignInActivity |

         Where-Object AccountEnabled -eq $true

 # Filter users whose last interactive sign-in was before cutoff

$InactiveUsers = $Users |

    Where-Object {

        $.SignInActivity.LastSignInDateTime -and ($.SignInActivity.LastSignInDateTime -lt $CutoffDate)

    } |

    Select-Object DisplayName, UserPrincipalName,

        @{Name="LastSignIn";Expression={$_.SignInActivity.LastSignInDateTime}}

 $InactiveUsers | Format-Table -AutoSize

Optionally export to CSV

$InactiveUsers | Export-Csv Inactive30Days.csv -NoTypeInformation

 Disconnect-MgGraph

I am utilizing PowerShell 7.4 in the Automation account, and the necessary Graph modules have been successfully updated. I have explored both managed identity and app registration methods for authentication; however, both scripts consistently return an "authentication needed: Call connect-MgGraph" error.

Your assistance in resolving this authentication problem and setting up the runbook and weekly report would be greatly appreciated.

Thank you for your time and support.

Best regards,

Chinmayi Bose

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
{count} votes

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.