Skip to content

Instantly share code, notes, and snippets.

@danton721
Last active April 2, 2024 18:15
Show Gist options
  • Save danton721/30c7131646e8e42b8a3877aff6149bca to your computer and use it in GitHub Desktop.
Save danton721/30c7131646e8e42b8a3877aff6149bca to your computer and use it in GitHub Desktop.
Get AD country for given UPN using AZ CLI in Excel using VBA
'Usage as Excel function: =getCountryFromAD("UPN")
Function getCmdlineOutput(cmd As String)
CreateObject("WScript.Shell").Run "cmd /c """ & cmd & "|clip""", 0, True 'output>clipbrd
With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") 'latebound clipbrd obj
.GetFromClipboard 'get cmdline output from clipboard
getCmdlineOutput = .GetText(1) 'return clipboard contents
End With
End Function
Public Function getCountryFromAD(email As String) As String
getCountryFromAD = getCmdlineOutput("az ad user show --query " & Chr(34) & "country" & Chr(34) & " --id " & email)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment