Today let's have some fun with DsregCmd.exe and Powershell. DsregCmd is a troubleshooting command-line tool that displays valuable info about your Azure AD tenant, how your device is joined to it, the status of the current user, etc...
Wouldn't it be nice to retrieve all those data into a Powershell object in a painless way? Sure, here is my take on it (also on Github) :
$Dsregcmd=New-Object PSObject ; Dsregcmd /status |Where {$_-match' : '}|ForEach {$Item=$_.Trim() -split'\s:\s'; $Dsregcmd|Add-Member-MemberType NoteProperty -Name $($Item[0] -replace'[:\s]','') -Value $Item[1] -EA SilentlyContinue}
From that one-liner, you'll be able to review all the properties from Dsregcmd like this:
Or simply call the one that you need:
Execution Context
Depending on the account used to gather information, Dsregcmd may report different sets of properties. If it runs from a MEM script or a MEM Application the selected user will be the system account which won't give any info about the current logged on Azure Ad user. To overcome this, I see two options:
- Retrive user info from my Get current Azure Ad user script.
- Running the script in the user context using the excellent module from Kelvin Tegelaar. Of course you'll need to save the powershell object while in user context to grab it later on in you System session. To save the object you can easily convert it to json: $Dsregcmd|ConvertTo-Json|Out-File "$env:TEMP\DsregCmd.json". Once done, load the result in your system session using $DsregStatus = Get-Content "$env:TEMP\DsregCmd.json"|ConvertFrom-Json
As always, some credit to d4rkeagle65 for inspiration. See you next time