Skip to content

Commit 7d0cb52

Browse files
committed
feat: Add Get-ConnectMgDefault function to retrieve Microsoft Graph authentication record
1 parent 6c3142e commit 7d0cb52

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function Get-ConnectMgDefault {
2+
<#
3+
.SYNOPSIS
4+
Returns the default Microsoft Graph authentication record stored in the current user's .mg folder.
5+
6+
.DESCRIPTION
7+
Loads the Microsoft Graph CLI authentication record from "$HOME/.mg/mg.authrecord.json" and presents it as a custom object. This helps you inspect which account and tenant are used by default when connecting with Microsoft Graph.
8+
9+
.EXAMPLE
10+
Get-ConnectMgDefault
11+
12+
Retrieves the default Microsoft Graph authentication record.
13+
14+
.EXAMPLE
15+
Get-ConnectMgDefault | Select-Object Account, TenantId, Scopes
16+
17+
Returns only the most commonly reviewed fields from the authentication record.
18+
19+
.INPUTS
20+
None. You cannot pipe objects to this cmdlet.
21+
22+
.OUTPUTS
23+
PSCustomObject. Returns the contents of 'mg.authrecord.json' as a custom object, or $null if the file cannot be read.
24+
25+
.NOTES
26+
Author: Sam Erde, Sentinel Technologies Inc.
27+
Modified: 2025 Dec 15
28+
#>
29+
[CmdletBinding()]
30+
[OutputType('PSCustomObject')]
31+
param ()
32+
33+
try {
34+
$DotMgPath = Join-Path -Path (Resolve-Path -Path $HOME).Path -ChildPath '.mg'
35+
$AuthRecordPath = Join-Path -Path $DotMgPath -ChildPath 'mg.authrecord.json'
36+
Get-Content -Path $AuthRecordPath | ConvertFrom-Json
37+
} catch {
38+
Write-Warning "Unable to read '$AuthRecordPath'. $($_.Exception.Message)"
39+
return $null
40+
}
41+
}

0 commit comments

Comments
 (0)