add gpo counter variable to powershell script for visbility

This commit is contained in:
2026-02-17 10:28:52 -08:00
parent 5327f865b9
commit 364da0159b
5 changed files with 130 additions and 47 deletions
+58 -6
View File
@@ -1,5 +1,6 @@
import yaml
import re
from datetime import datetime
from flask import Flask, request, jsonify
app = Flask(__name__)
@@ -8,6 +9,23 @@ app = Flask(__name__)
with open('{{ gpo_yaml_path }}', 'r') as file:
data = yaml.safe_load(file)
def strip_dn(dn: str, base_suffix: str = ",OU=Manufacturing,OU=Tesla Systems,DC=teslamotors,DC=com") -> str:
suffix = base_suffix.strip()
dn_clean = dn.strip()
lowered_dn = dn_clean.lower()
lowered_suffix = suffix.lower()
idx = lowered_dn.rfind(lowered_suffix)
if idx != -1:
dn_clean = dn_clean[:idx].rstrip(',')
# Pull all OU=… values (leaf → root order)
ou_values: List[str] = re.findall(r'OU=([^,]+)', dn_clean, flags=re.IGNORECASE)
if not ou_values:
return ''
ou_values = [v.strip() for v in reversed(ou_values)]
return '\\'.join(ou_values)
@app.route('/gpo', methods=['GET'])
def get_groups():
group_name = request.args.get('group')
@@ -37,6 +55,33 @@ def get_groups():
})
return jsonify(gpos)
@app.route("/gpo_links", methods=["GET"])
def gpo_links():
try:
gpo_link_count = data[0]['gpo_links']
except:
gpo_link_count = "whoops"
return jsonify({"gpo_links": gpo_link_count})
@app.route("/timestamp", methods=["GET"])
def timestamp():
try:
yaml_timestamp = data[0]['timestamp']
except:
yaml_timestamp = "whoops"
return jsonify({"timestamp": yaml_timestamp})
@app.route("/index_duration", methods=["GET"])
def index_duration():
fmt="%m/%d/%Y %I:%M:%S %p"
try:
start_timestamp = data[0]['init_timestamp']
end_timestamp = data[0]['timestamp']
duration = (datetime.strptime(end_timestamp, fmt) - datetime.strptime(start_timestamp, fmt)).total_seconds() / 60
return jsonify({"duration": duration, "start_timestamp": start_timestamp, "end_timestamp": end_timestamp})
except:
return jsonify({'duration': "whoops"})
@app.route("/linked_ous", methods=["GET"])
def linked_ous():
gpo_name = request.args.get("gpo")
@@ -55,21 +100,28 @@ def linked_ous():
if gpo_entry is None:
continue
def _add_ous(ous):
if not isinstance(ous, list):
return
for dn in ous:
if isinstance(dn, str):
# 1. strip out OU/ DC components
# 2. keep the *leaf* and its *parent* (e.g. NA\SJC18)
stripped = strip_dn(dn)
if stripped:
linked.append(stripped)
# Case 1: direct dict → look for Linked_OUs key
if isinstance(gpo_entry, dict):
if "Linked_OUs" in gpo_entry:
ous = gpo_entry["Linked_OUs"]
if isinstance(ous, list):
linked.extend(ous)
_add_ous(gpo_entry["Linked_OUs"])
continue
# Case 2: list of dicts → find the dict that has the Linked_OUs key
if isinstance(gpo_entry, list):
for sub in gpo_entry:
if isinstance(sub, dict) and "Linked_OUs" in sub:
ous = sub["Linked_OUs"]
if isinstance(ous, list):
linked.extend(ous)
_add_ous(sub["Linked_OUs"])
return jsonify(linked)