powershell-intune-samples icon indicating copy to clipboard operation
powershell-intune-samples copied to clipboard

Update PowerShell Intune samples to use Microsoft Graph SDK authentication

Open DickTracyII opened this issue 4 months ago โ€ข 0 comments

๐Ÿ—ฃ Description

This pull request updates the entire PowerShell Intune samples repository to use the official Microsoft.Graph.Authentication module instead of legacy custom authentication functions. The update includes multi-cloud environment support, URL standardization, character encoding fixes, code style improvements, and comprehensive documentation updates.

Key Changes:

  • Replaced legacy Get-AuthToken functions with Connect-GraphAPI using Microsoft.Graph.Authentication module
  • Added support for 5 Microsoft cloud environments (Global, USGov, USGovDod, China, Germany)
  • Implemented environment-aware Graph endpoint management with $global:GraphEndpoint variable
  • Fixed character encoding issues (en-dash to hyphen conversion)
  • Converted null comparisons to Yoda conditions for improved code safety
  • Cleaned trailing whitespace across entire codebase
  • Updated README.md with comprehensive migration documentation

๐Ÿ’ญ Motivation and context

Problem: The repository contained legacy authentication patterns that are deprecated and don't support multi-cloud Microsoft environments. Additionally, there were character encoding issues, hardcoded URLs, and inconsistent coding practices throughout the codebase.

Solution:

  1. Graph Authentication Update: Migrated from custom Get-AuthToken functions to the official Microsoft.Graph.Authentication module
  2. Multi-Cloud Support: Added environment parameter support for different Microsoft cloud instances
  3. URL Standardization: Replaced hardcoded https://graph.microsoft.com URLs with environment-aware variables
  4. Character Encoding: Fixed en-dash characters (โ€“) that caused syntax errors when copied from documentation
  5. Code Safety: Implemented Yoda conditions to prevent accidental assignments in null comparisons
  6. Code Quality: Cleaned whitespace and improved overall code consistency

Files Affected:

  • 140+ PowerShell files across all sample categories
  • Authentication: All scripts now use Connect-GraphAPI with environment support
  • URLs: 280+ hardcoded Graph URLs replaced with $global:GraphEndpoint
  • Character Encoding: 96 en-dash characters fixed across 18 files
  • Null Comparisons: 344 comparisons converted to Yoda conditions across 93 files
  • Whitespace: 4,593 lines cleaned of trailing whitespace
  • Documentation: README.md updated with migration guide and best practices

๐Ÿงช Testing

Testing Methodology:

  1. Syntax Validation: All PowerShell scripts validated for syntax correctness using PowerShell parser
  2. Pattern Verification: Comprehensive regex searches confirmed complete removal of legacy patterns
  3. Environment Testing: Scripts validated for multi-cloud environment support
  4. Authentication Testing: Connect-GraphAPI functionality verified across different environments
  5. Encoding Verification: Character encoding issues resolved and validated

Test Results:

  • โœ… All 140+ PowerShell files parse without syntax errors
  • โœ… Zero remaining Get-AuthToken functions found
  • โœ… Zero hardcoded Graph URLs remaining
  • โœ… All en-dash characters successfully converted
  • โœ… All null comparisons follow Yoda condition pattern
  • โœ… No trailing whitespace detected

Validation Commands Used:

# Authentication validation
Get-ChildItem -Recurse -Filter "*.ps1" | ForEach-Object { 
    $content = Get-Content $_.FullName -Raw
    if($content -match 'function\s+Get-AuthToken|^\s*Get-AuthToken') { $_.FullName }
}

# URL validation  
Get-ChildItem -Recurse -Filter "*.ps1" | ForEach-Object {
    $content = Get-Content $_.FullName -Raw
    if($content -match '\$uri\s*=\s*"https://graph\.microsoft\.com/') { $_.FullName }
}

# Character encoding validation
Get-ChildItem -Recurse -Filter "*.ps1" | ForEach-Object {
    $content = Get-Content $_.FullName -Raw
    if($content -match 'โ€“') { $_.FullName }
}

DickTracyII avatar Aug 27 '25 17:08 DickTracyII