Skip to content

thetolkienblackguy/Get-GraphScriptPermissions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Get-GraphScriptPermissions

A PowerShell script analyzer that identifies Microsoft Graph SDK cmdlets and their required permissions in your scripts. Stop the guesswork and trial-and-error when sharing Graph-powered PowerShell scripts with your team.

🎯 Problem This Solves

You've written a PowerShell script using the Microsoft Graph SDK. It works perfectly during testing, but when you share it with teammates, it breaks because they don't have the right Graph API permissions. Sound familiar?

While Find-MgGraphCommand is great for checking individual cmdlets, analyzing entire scripts line-by-line is tedious and time-consuming. Get-GraphScriptPermissions scales this process by automatically parsing your entire script and providing a consolidated permissions report.

πŸš€ Features

  • Automated Discovery: Scans PowerShell scripts and identifies all Microsoft Graph SDK cmdlets
  • Smart Filtering: Excludes authentication-only cmdlets that don't require Graph API permissions
  • Least-Privileged Focus: Highlights the minimum permissions needed for each cmdlet
  • Comprehensive Reporting: Shows all valid permissions, not just the least-privileged ones
  • Scope Validation: Indicates whether your current Graph session has the required permissions
  • Export Capability: Save results as CSV for documentation or sharing
  • Line Number Tracking: Know exactly where each cmdlet appears in your script

πŸ“¦ Installation

From PowerShell Gallery (Recommended)

Install-Script -Name Get-GraphScriptPermissions

From GitHub

# Clone the repository
git clone https://github.com/thetolkienblackguy/Get-GraphScriptPermissions.git

# Or download the script directly
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/thetolkienblackguy/Get-GraphScriptPermissions/main/Get-GraphScriptPermissions.ps1" -OutFile "Get-GraphScriptPermissions.ps1"

πŸ“‹ Prerequisites

  • PowerShell 5.1 or later
  • Microsoft.Graph PowerShell SDK
  • Graph Authentication: Must be connected to Microsoft Graph (Connect-MgGraph) for scope validation

πŸ”§ Usage

Basic Analysis

.\Get-GraphScriptPermissions.ps1 -ScriptPath .\MyScript.ps1

Export Results to CSV

.\Get-GraphScriptPermissions.ps1 -ScriptPath .\MyScript.ps1 -OutputPath .\permissions-report.csv

πŸ“Š Sample Output

Given this input script:

Get-MgUserManager -UserId test.user@contoso.com
Update-MgUser -UserId test.user@contoso.com -AccountEnabled:$false
Revoke-MgUserSignInSession -UserId test.user@contoso.com

The analyzer returns:

Cmdlet                             : Get-MgUserManager
LineNumbers                        : 1
LeastPrivilegedEffectivePermission : User.Read.All
Description                        : Read all users' full profiles
Permissions                        : User.Read.All, User.ReadWrite.All
HasScope                           : True

Cmdlet                             : Update-MgUser
LineNumbers                        : 2
LeastPrivilegedEffectivePermission : User.ReadWrite.All
Description                        : Read and write all users' full profiles
Permissions                        : User.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, [...]
HasScope                           : True

Cmdlet                             : Revoke-MgUserSignInSession
LineNumbers                        : 3
LeastPrivilegedEffectivePermission : User.ReadWrite.All
Description                        : Read and write all users' full profiles
Permissions                        : User.ReadWrite.All
HasScope                           : True

πŸ” Output Fields Explained

Field Description
Cmdlet The Microsoft Graph SDK cmdlet found in your script
LineNumbers Comma-separated list of line numbers where the cmdlet appears
LeastPrivilegedEffectivePermission The minimum permission scope required
Description What the least-privileged permission allows
Permissions All valid permission scopes for this cmdlet
HasScope Whether your current Graph session has any required permissions

βš™οΈ Parameters

-ScriptPath (Required)

  • Type: String
  • Description: Path to the PowerShell script to analyze
  • Validation: Must be a valid file path

-OutputPath (Optional)

  • Type: String
  • Description: Path to export results as CSV
  • Default: Results display in console only

🧠 How It Works

  1. Script Parsing: Reads your PowerShell script line by line
  2. Cmdlet Detection: Uses regex pattern matching with approved PowerShell verbs to find Graph SDK cmdlets (*-Mg*)
  3. Permission Lookup: Leverages Find-MgGraphCommand to get permission information for each unique cmdlet
  4. Smart Filtering: Excludes authentication-only cmdlets and "me-only" permissions
  5. Result Aggregation: Groups identical cmdlets and merges their line numbers
  6. Scope Validation: Compares required permissions against your current Graph session scopes

πŸ“ Advanced Examples

Permission Gap Analysis

# Connect with limited scopes
Connect-MgGraph -Scopes "User.Read.All"

# Analyze script to see what's missing
$results = .\Get-GraphScriptPermissions.ps1 -ScriptPath .\MyScript.ps1
$missingPermissions = $results | Where-Object { $_.HasScope -eq $false }
$missingPermissions | Select-Object Cmdlet, LeastPrivilegedEffectivePermission

⚠️ Important Notes

  • Authentication Required: You must be authenticated with Microsoft Graph (Connect-MgGraph) for the HasScope field to be accurate
  • Comment Handling: Single-line comments are ignored; block comments are not currently supported
  • Performance: Large scripts with many unique cmdlets may take longer to analyze due to individual Find-MgGraphCommand lookups

πŸ› Troubleshooting

"This session is not authenticated with Microsoft Graph"

# Connect to Microsoft Graph first
Connect-MgGraph

🀝 Contributing

Contributions are welcome! Areas for improvement:

  • Block Comment Support: Currently only handles single-line comments
  • Performance Optimization: Batch permission lookups
  • Enhanced Filtering: More sophisticated permission categorization
  • Integration: PowerShell module packaging

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Gabriel Delaney

πŸ”— Related Resources


Made with ❀️ to simplify Microsoft Graph PowerShell development

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published