|
57 | 57 |
|
58 | 58 | .PARAMETER OutputDirectory |
59 | 59 | Directory where output files will be saved. |
60 | | - Default: C:\Reports\AccountCreation |
| 60 | + Default: C:\Reports\AccountCreation (Windows) |
| 61 | + Default: ~/clouddrive/AccountCreation (Cloud Shell) |
| 62 | + Auto-detected based on environment. |
61 | 63 |
|
62 | 64 | .PARAMETER GeneratePasswords |
63 | 65 | Force password generation even if passwords are provided in CSV. |
|
115 | 117 | |
116 | 118 | Creates accounts and automatically provisions OneDrive for each user. |
117 | 119 |
|
| 120 | +.EXAMPLE |
| 121 | + # From Azure Cloud Shell |
| 122 | + .\New-Office365Accounts.ps1 -UserArray $users |
| 123 | + |
| 124 | + Automatically detects Cloud Shell and saves output to ~/clouddrive/AccountCreation/ |
| 125 | +
|
118 | 126 | .NOTES |
119 | 127 | Author: W. Ford |
120 | 128 | Date: 2026-01-22 |
|
123 | 131 | Requirements: |
124 | 132 | - For Microsoft 365: Microsoft.Graph.Users module |
125 | 133 | - For OneDrive initialization: Microsoft.Graph.Files and Microsoft.Graph.Sites modules |
126 | | - - For Active Directory: ActiveDirectory module |
127 | | - - PowerShell 5.1 or later |
| 134 | + - For Active Directory: ActiveDirectory module (not available in Cloud Shell) |
| 135 | + - PowerShell 5.1 or later (Cloud Shell supported) |
128 | 136 | - Appropriate permissions: |
129 | 137 | * M365: User.ReadWrite.All permission in Microsoft Graph |
130 | 138 | * AD: Account creation rights in target OU |
131 | 139 | |
| 140 | + Cloud Shell: |
| 141 | + - Automatically detects Azure Cloud Shell environment |
| 142 | + - Saves output to ~/clouddrive/ for persistent storage |
| 143 | + - Provides download instructions for exported files |
| 144 | + - Works seamlessly with pre-installed Microsoft.Graph modules |
| 145 | + |
132 | 146 | Output: |
133 | 147 | - CSV file with created accounts and passwords (timestamp in filename) |
134 | 148 | - Detailed console logging with color-coded status messages |
@@ -190,7 +204,7 @@ param( |
190 | 204 | [string]$AccountType = "Microsoft365", |
191 | 205 |
|
192 | 206 | [Parameter(Mandatory=$false)] |
193 | | - [string]$OutputDirectory = "C:\Reports\AccountCreation", |
| 207 | + [string]$OutputDirectory, # Default set in code based on environment detection |
194 | 208 |
|
195 | 209 | [Parameter(Mandatory=$false)] |
196 | 210 | [switch]$GeneratePasswords, |
@@ -226,11 +240,72 @@ Write-Host "`n$Separator" -ForegroundColor Cyan |
226 | 240 | Write-Host "ACCOUNT CREATION UTILITY" -ForegroundColor Cyan |
227 | 241 | Write-Host "Started: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor Cyan |
228 | 242 | Write-Host $Separator -ForegroundColor Cyan |
| 243 | +# Detect environment and set default output directory if not specified |
| 244 | +if ([string]::IsNullOrWhiteSpace($OutputDirectory)) { |
| 245 | + $OutputDirectory = Get-DefaultOutputDirectory |
| 246 | +} |
229 | 247 |
|
| 248 | +# Display environment info |
| 249 | +if (Test-CloudShell) { |
| 250 | + Write-Host "Environment: Azure Cloud Shell" -ForegroundColor Cyan |
| 251 | + Write-Host "PowerShell Version: $($PSVersionTable.PSVersion)" -ForegroundColor Gray |
| 252 | +} |
| 253 | +else { |
| 254 | + Write-Host "Environment: $([System.Environment]::OSVersion.Platform)" -ForegroundColor Cyan |
| 255 | +} |
230 | 256 | #endregion |
231 | 257 |
|
232 | 258 | #region Functions |
233 | 259 |
|
| 260 | +function Test-CloudShell { |
| 261 | + <# |
| 262 | + .SYNOPSIS |
| 263 | + Detects if running in Azure Cloud Shell environment |
| 264 | + #> |
| 265 | + |
| 266 | + # Check for Cloud Shell specific environment variables |
| 267 | + $cloudShellIndicators = @( |
| 268 | + $env:ACC_CLOUD, |
| 269 | + $env:AZURE_HTTP_USER_AGENT, |
| 270 | + $env:POWERSHELL_DISTRIBUTION_CHANNEL |
| 271 | + ) |
| 272 | + |
| 273 | + # Check if any Cloud Shell indicators are present |
| 274 | + $isCloudShell = $cloudShellIndicators | Where-Object { |
| 275 | + $_ -and ($_ -like "*CloudShell*" -or $_ -like "*Azure*") |
| 276 | + } |
| 277 | + |
| 278 | + # Additional check: Cloud Shell typically has clouddrive mounted |
| 279 | + if (-not $isCloudShell -and (Test-Path "$HOME/clouddrive" -ErrorAction SilentlyContinue)) { |
| 280 | + $isCloudShell = $true |
| 281 | + } |
| 282 | + |
| 283 | + return [bool]$isCloudShell |
| 284 | +} |
| 285 | + |
| 286 | +function Get-DefaultOutputDirectory { |
| 287 | + <# |
| 288 | + .SYNOPSIS |
| 289 | + Returns appropriate default output directory based on environment |
| 290 | + #> |
| 291 | + |
| 292 | + if (Test-CloudShell) { |
| 293 | + # Cloud Shell - use persistent clouddrive |
| 294 | + $cloudDrivePath = "$HOME/clouddrive/AccountCreation" |
| 295 | + Write-Host "🌥️ Azure Cloud Shell detected" -ForegroundColor Cyan |
| 296 | + Write-Host " Output will be saved to persistent clouddrive: $cloudDrivePath" -ForegroundColor Cyan |
| 297 | + return $cloudDrivePath |
| 298 | + } |
| 299 | + elseif ($IsLinux -or $IsMacOS) { |
| 300 | + # Linux/Mac |
| 301 | + return "$HOME/Reports/AccountCreation" |
| 302 | + } |
| 303 | + else { |
| 304 | + # Windows |
| 305 | + return "C:\Reports\AccountCreation" |
| 306 | + } |
| 307 | +} |
| 308 | + |
234 | 309 | function Write-StatusMessage { |
235 | 310 | param( |
236 | 311 | [string]$Message, |
@@ -527,8 +602,28 @@ function Export-AccountResults { |
527 | 602 | try { |
528 | 603 | $Accounts | Export-Csv -Path $outputFile -NoTypeInformation -Encoding UTF8 |
529 | 604 | Write-Host "`n✅ Exported $($Accounts.Count) account(s) to: $outputFile" -ForegroundColor Green |
530 | | - Write-Host " ⚠️ IMPORTANT: Secure this file - it contains passwords!" -ForegroundColor Yellow |
531 | | - } |
| 605 | + Write-Host " ⚠️ IMPORTANT: Secure this file - it contains passwords!" -ForegroundColor Yellow |
| 606 | + # Provide Cloud Shell specific instructions |
| 607 | + if (Test-CloudShell) { |
| 608 | + Write-Host "\n📥 Cloud Shell File Access:" -ForegroundColor Cyan |
| 609 | + Write-Host " • File saved to persistent clouddrive storage" -ForegroundColor White |
| 610 | + Write-Host " • Download via CLI: " -ForegroundColor White -NoNewline |
| 611 | + Write-Host "download `"$outputFile`"" -ForegroundColor Yellow |
| 612 | + Write-Host " • Download via UI: Click 'Upload/Download files' button (folder icon) in toolbar" -ForegroundColor White |
| 613 | + Write-Host " • Or access via Azure Storage Explorer in your Azure Portal" -ForegroundColor White |
| 614 | + Write-Host " • File persists across Cloud Shell sessions" -ForegroundColor White |
| 615 | + |
| 616 | + # Try to get the Azure Storage account info |
| 617 | + try { |
| 618 | + $storageAccount = (Get-CloudDrive).FileShareName -split '\.' | Select-Object -First 1 |
| 619 | + if ($storageAccount) { |
| 620 | + Write-Host " • Storage Account: $storageAccount" -ForegroundColor Gray |
| 621 | + } |
| 622 | + } |
| 623 | + catch { |
| 624 | + # Ignore if Get-CloudDrive is not available |
| 625 | + } |
| 626 | + } } |
532 | 627 | catch { |
533 | 628 | Write-StatusMessage "Failed to export results: $($_.Exception.Message)" -Type Error |
534 | 629 | } |
|
0 commit comments