#!/usr/bin/env python3
import sys, time, random
sys.path.insert(0, '.')
import ps_driver as P

ip = sys.argv[1] if len(sys.argv) > 1 else '179.1.102.69'
ps = P.ProxyShell(f"https://{ip}", timeout=15)
print("fqdn:", ps.get_fqdn()); ps.get_legacydn(); print("email:", ps.email)
ps.get_sid()
ps.sid = ps.admin_sid
print("token:", "OK" if ps.get_token() else "FAIL")
port = random.randint(20000, 40000)
P.start_server(ps, port); time.sleep(1)

def run(label, script):
    print(f"### {label}")
    try:
        out, err = P.wsman_shell(script, port)
        print("  OUT:", [str(o) for o in out][:60])
        if err: print("  ERR:", [str(e) for e in err][:2])
    except Exception as e:
        print("  ERRX:", repr(e))

run("all cmdlets matching Export", "Get-Command -Name *Export* -CommandType Cmdlet | Select-Object -ExpandProperty Name")
run("all cmdlets matching Import", "Get-Command -Name *Import* -CommandType Cmdlet | Select-Object -ExpandProperty Name")
run("all cmdlets matching Certificate", "Get-Command -Name *Certificate* | Select-Object -ExpandProperty Name")
run("all cmdlets matching Transport", "Get-Command -Name *Transport* | Select-Object -ExpandProperty Name")
run("all cmdlets matching OAB", "Get-Command -Name *OfflineAddressBook* | Select-Object -ExpandProperty Name")
run("all cmdlets matching MailboxExport", "Get-Command -Name *MailboxExport* | Select-Object -ExpandProperty Name")
run("all cmdlets matching Write/Out/Set", "Get-Command -Name *Write* | Select-Object -ExpandProperty Name")
run("Get-Command count", "Get-Command | Measure-Object | Select-Object -ExpandProperty Count")
run("New-MailboxExportRequest exists", "Get-Command New-MailboxExportRequest | Select-Object -ExpandProperty Name")
run("roles on mailbox user", "Get-ManagementRoleAssignment -RoleAssignee subgprod@imprenta.gov.co | Select-Object -ExpandProperty Role")
