Skip to content

Commit a28a408

Browse files
committed
Add New-Office365Accounts script for bulk user account creation in Office 365
1 parent d533b03 commit a28a408

1 file changed

Lines changed: 101 additions & 6 deletions

File tree

scripts/Office365/New-Office365Accounts.ps1

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
5858
.PARAMETER OutputDirectory
5959
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.
6163
6264
.PARAMETER GeneratePasswords
6365
Force password generation even if passwords are provided in CSV.
@@ -115,6 +117,12 @@
115117
116118
Creates accounts and automatically provisions OneDrive for each user.
117119
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+
118126
.NOTES
119127
Author: W. Ford
120128
Date: 2026-01-22
@@ -123,12 +131,18 @@
123131
Requirements:
124132
- For Microsoft 365: Microsoft.Graph.Users module
125133
- 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)
128136
- Appropriate permissions:
129137
* M365: User.ReadWrite.All permission in Microsoft Graph
130138
* AD: Account creation rights in target OU
131139
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+
132146
Output:
133147
- CSV file with created accounts and passwords (timestamp in filename)
134148
- Detailed console logging with color-coded status messages
@@ -190,7 +204,7 @@ param(
190204
[string]$AccountType = "Microsoft365",
191205

192206
[Parameter(Mandatory=$false)]
193-
[string]$OutputDirectory = "C:\Reports\AccountCreation",
207+
[string]$OutputDirectory, # Default set in code based on environment detection
194208

195209
[Parameter(Mandatory=$false)]
196210
[switch]$GeneratePasswords,
@@ -226,11 +240,72 @@ Write-Host "`n$Separator" -ForegroundColor Cyan
226240
Write-Host "ACCOUNT CREATION UTILITY" -ForegroundColor Cyan
227241
Write-Host "Started: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor Cyan
228242
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+
}
229247

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+
}
230256
#endregion
231257

232258
#region Functions
233259

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+
234309
function Write-StatusMessage {
235310
param(
236311
[string]$Message,
@@ -527,8 +602,28 @@ function Export-AccountResults {
527602
try {
528603
$Accounts | Export-Csv -Path $outputFile -NoTypeInformation -Encoding UTF8
529604
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+
} }
532627
catch {
533628
Write-StatusMessage "Failed to export results: $($_.Exception.Message)" -Type Error
534629
}

0 commit comments

Comments
 (0)