PowerShell helper to connect to Microsoft Graph with multiple authentication modes, including unattended automation-friendly options.
Install-Script -Name Connect-ToMgGraphThis script is designed for automation scenarios where human interaction should be optional or avoided, such as:
- Jenkins pipelines
- Azure Automation runbooks
- Azure DevOps pipelines
- Ansible playbooks
The script supports authentication patterns documented for Connect-MgGraph.
.\Connect-ToMgGraph.ps1 -interactive.\Connect-ToMgGraph.ps1 -devicecode.\Connect-ToMgGraph.ps1 -scopesonly.\Connect-ToMgGraph.ps1 -entraapp -AppId "<app-id>" -AppSecret "<app-secret>" -TenantId "<tenant-id>".\Connect-ToMgGraph.ps1 -usessl -AppId "<app-id>" -TenantId "<tenant-id>" -CertificateThumbprint "<thumbprint>".\Connect-ToMgGraph.ps1 -usessl -AppId "<app-id>" -TenantId "<tenant-id>" -CertificateName "CN=GraphAutomationCert".\Connect-ToMgGraph.ps1 -managedidentity.\Connect-ToMgGraph.ps1 -managedidentity -ManagedIdentityClientId "<managed-identity-client-id>".\Connect-ToMgGraph.ps1 -environmentvariable.\Connect-ToMgGraph.ps1 -accesstokenauth -AccessToken "<jwt-access-token>"Use -Environment with one of:
Global(default)USGovUSGovDoDChina
Example:
.\Connect-ToMgGraph.ps1 -managedidentity -Environment USGov.\Connect-ToMgGraph.ps1 -status
.\Connect-ToMgGraph.ps1 -disconnects
.\Connect-ToMgGraph.ps1 -disconnects -SkipConfirmation- Prefer
-managedidentity,-entraapp, or-usesslfor non-interactive automation. - Use secret stores (Azure Key Vault, Jenkins credentials, Azure DevOps secret variables, Ansible Vault) and avoid plain text secrets in source control.
- Use one authentication mode per invocation.
This repository includes a GitHub Actions workflow to publish to PowerShell Gallery when version is bumped after a full update.
Workflow file:
.github/workflows/publish-psgallery.yml
Required repository secret:
PSGALLERY_API_KEY(PowerShell Gallery API key with publish permission)
Release flow:
- Update code.
- Bump version in both places:
Connect-ToMgGraph.ps1->.VERSIONConnect-ToMgGraph.psd1->ModuleVersion
- Merge to
main. - Workflow validates version consistency, checks duplicate versions in PSGallery, and publishes with
Publish-Script. - If publish succeeds from a branch push, workflow creates a matching tag like
v1.3.1.
Optional tag path:
- Pushing a tag that matches
v*.*.*also triggers the same workflow. - Tag version must match script/manifest version.