This blog is a follow-up from blog
PowerShell – Azure AD User last login Report | Tech Wizard
In previous blog we have gathered the Azure AD last login report from Powershell, In this blog we will do the same with Python.
Download the script sample from gitlab repo and modify it as per your needs.
Modules required:
- Requests – For Rest API calls
- DPAPI – For Password encryption/Decryption
- VSADMIN – This I have built same as PowerShell vsadmin module and is uploaded to github.
Rest all modules are already part of Python package so not mentioning those.
Scripts to download:
https://github.com/VikasSukhija/Python
- AzureADLastLoginReport.py
- Vsadmin.py (place it in lib folder)
I have token stored as encrypted XML file and some of the variables in config file from where I am picking it.
Config parser has been utilized to read the ini file and XML has ben used for reading the token from XML file.
DPAPI module is used to decrypt the encrypted token.
If you do not need that much complexity just remove those from script and modify it accordingly.
Edit the Number of days if you need more than 90 days of filtered data.
comparedate = today + timedelta(days=-90)
You can also update if you need Guest report or member report by updating below filter in Graph API uri.
When you will run the script:
Below will be output format of the report generated: (one report will be full report and other is based on the number of days you have added)
Both reports will be saved in report folder.
It is similar format report as generated from PowerShell script that we discussed previously.
I hope you will be able to utilize this script sample or some part of code as per your needs.
Thanks for reading and downloading…
Tech Wizard
I couldn’t agree more
Great blog post! I really appreciate the detailed walkthrough on using Python to gather the Azure AD last login report. It’s impressive how you utilized modules such as Requests and DPAPI to make the script more complex but also customizable. One question I have is, have you considered adding any error handling to the script in case there are missing config files or XML tokens?
Jon
AiRiches.Online
Yes, error checking has been builtin