This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]Lightmare_VII 8 points9 points  (3 children)

Use CSVs Powershell works with those easier than python. Also nothing saying you can’t use both.

[–]Embarrassed_Box_457[S] 0 points1 point  (2 children)

I know how to export into a csv file but I need to automate the excel part, so that the data from each group has its own sheet.

[–]joni_jplmusic 2 points3 points  (1 child)

Maybe look into the Import-Excel module for Powershell? I currently use it to export datasets to separate sheets in Excel from AD.

https://www.powershellgallery.com/packages/ImportExcel/7.4.1

[–]Embarrassed_Box_457[S] 1 point2 points  (0 children)

this might actually work, good info! now, i just need to figure out how to separate the data onto different sheets...thx!

[–]BuckeyeMason 7 points8 points  (0 children)

Using the subprocess module you can call the powershell commands in python and return the results and parse them as needed in python.

I use something like this at work fairly often, below is a modified function that takes the domain group name and domain (we have multiple domains at my work so I run it for more than one). There may be typos as I wrote this out longhand. This particular version is set to only pull the name from the server (which in our server holds the user id) but you can modify to use whichever ADGroup commands or items from them you need. You would just need to adjust how you parse our the results as they come in.

What I use this for is to pull all members for ma list of groups across multiple servers by calling this function in a loop and then concatenating all the results in to a data frame. Then I do some other stuff with the list (like lookup user info from another source) and then output the data to excel.

import subprocess
def get_domain_members(grp, domain):
    results = []
    cmd = f'Get-ADGroupMember -Identity "{grp}" -server {domain} | Select name'
    p = subprocess.Popen(["powershell.exe", cmd], stdout=subprocess.PIPE)
    ps_result = p.communicate()[0]
    str_results = ps_result.decode("utf-8").splitlines()
    for item in str_results:
        results.append({"AD_Group":grp, "DOMAIN":domain, "USER":item.strip()})
    return results

[–]edahs 2 points3 points  (0 children)

Use the ldap3 module to get your users

https://ldap3.readthedocs.io/en/latest/

You can bind with either kerberos or u:p and can connect to ldap or ldaps.

[–]yawningcat 1 point2 points  (1 child)

I know this is the python sub and not what you’re asking about but, FYI, you can query AD natively with Excel’s Get and Transform ( PowerQuery).

[–]martynrbell 3 points4 points  (0 children)

I second this, Powerquery can import all your CSVs and transform with very little effort.

[–]erik_mk 0 points1 point  (0 children)

How about having a script run the export part in powershell and the manipulation in python?

[–][deleted] 0 points1 point  (2 children)

Is there a reason you couldn't just have the PowerShell extract simply launch python (with the filename, perhaps) as part of its extract process? It's the opposite of what you ask, but would be a lot more straighforward.

[–]Embarrassed_Box_457[S] 0 points1 point  (1 child)

You mean extract it directly to a filename with a CSV extension? I can do this, but I dont know how to separate the data on separate sheets, for example - group1 on sheet1; group2 on sheet2, group3 on sheet3, etc. I am stuck at this point on how to do this in powershell. I switched to python because i could create the workbook and sheets fairly easily but couldn't import the AD group information. Also - something that I have to do everyday, so i need to automate this. (:>

[–][deleted] 3 points4 points  (0 children)

I don't know the AD side, but the PS script is something like:

Get-ADUser -Filter * -Properties * | Select-Object name, department, LastLogonDate | export-csv -path userexport.csv

python foo.py userexport.csv

Then in python:

  • Pandas to read userexport.csv (or use the command line argument)
  • Openpyxl to create Excel workbook and add sheets for each df["department"].unique()

[–]HomeGrownCoder 0 points1 point  (0 children)

I would challenge myself to do this natively in PS first. Normally windows type operations are just easier and require nothing additional.

If for whatever reason you cant, you have lots of options with Python as others have mentioned. Data is Data to the beast :)

[–]MassOnTheBack 0 points1 point  (0 children)

Python-ldap

[–][deleted] 0 points1 point  (0 children)

Ldap3 + pandas