fix: WinRM RDP update via NETLOGON Deploy, git fallback (0.20.12)
This commit is contained in:
@@ -289,27 +289,73 @@ def _wrap_powershell_body(body: str) -> str:
|
||||
)
|
||||
|
||||
|
||||
def _rdp_update_powershell_script(script_path: str) -> str:
|
||||
def _rdp_update_powershell_script(
|
||||
script_path: str,
|
||||
*,
|
||||
repo_url: str = "",
|
||||
git_branch: str = "main",
|
||||
) -> str:
|
||||
script = script_path.strip()
|
||||
if script:
|
||||
literal = _powershell_literal_path(script)
|
||||
body = (
|
||||
f"$p = '{literal}'\n"
|
||||
"if (-not (Test-Path -LiteralPath $p)) { throw \"Deploy script not found: $p\" }\n"
|
||||
"& $p"
|
||||
)
|
||||
else:
|
||||
body = (
|
||||
"$candidates = @(\n"
|
||||
" (Join-Path $env:ProgramData 'RDP-login-monitor\\Deploy-LoginMonitor.ps1'),\n"
|
||||
" (Join-Path $env:ProgramData 'LoginMonitor\\Deploy-LoginMonitor.ps1'),\n"
|
||||
" (Join-Path $env:USERPROFILE 'Deploy-LoginMonitor.ps1')\n"
|
||||
")\n"
|
||||
"$p = $candidates | Where-Object { Test-Path -LiteralPath $_ } | Select-Object -First 1\n"
|
||||
"if (-not $p) { throw 'Deploy-LoginMonitor.ps1 not found on client PC' }\n"
|
||||
"Write-Output \"Running: $p\"\n"
|
||||
"& $p"
|
||||
)
|
||||
return _wrap_powershell_body(body)
|
||||
|
||||
safe_repo = _powershell_literal_path(
|
||||
(repo_url or "https://git.kalinamall.ru/PapaTramp/RDP-login-monitor.git").strip()
|
||||
)
|
||||
safe_branch = _powershell_literal_path((git_branch or "main").strip() or "main")
|
||||
body = (
|
||||
"function Invoke-RdpDeploy {\n"
|
||||
" param([string]$Path)\n"
|
||||
" Write-Output \"Running: $Path\"\n"
|
||||
" & $Path\n"
|
||||
"}\n"
|
||||
"$deployCandidates = [System.Collections.Generic.List[string]]::new()\n"
|
||||
"if ($env:USERDOMAIN) {\n"
|
||||
" $deployCandidates.Add((Join-Path \"\\\\$($env:USERDOMAIN)\\NETLOGON\\RDP-login-monitor\" 'Deploy-LoginMonitor.ps1'))\n"
|
||||
"}\n"
|
||||
"try {\n"
|
||||
" $dom = (Get-CimInstance Win32_ComputerSystem -ErrorAction Stop).Domain\n"
|
||||
" if ($dom) {\n"
|
||||
" $deployCandidates.Add((Join-Path \"\\\\$dom\\NETLOGON\\RDP-login-monitor\" 'Deploy-LoginMonitor.ps1'))\n"
|
||||
" }\n"
|
||||
"} catch { }\n"
|
||||
"foreach ($p in $deployCandidates) {\n"
|
||||
" if (Test-Path -LiteralPath $p) {\n"
|
||||
" Invoke-RdpDeploy $p\n"
|
||||
" exit 0\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
f"$gitUrl = '{safe_repo}'\n"
|
||||
f"$branch = '{safe_branch}'\n"
|
||||
"$repoDir = Join-Path $env:ProgramData 'RDP-login-monitor\\_sac_git'\n"
|
||||
"$gitCmd = Get-Command git.exe -ErrorAction SilentlyContinue\n"
|
||||
"if (-not $gitCmd) {\n"
|
||||
" throw 'Deploy-LoginMonitor.ps1 not on NETLOGON and git.exe missing; set win_agent_update_script in SAC or install Git'\n"
|
||||
"}\n"
|
||||
"if (-not (Test-Path -LiteralPath (Join-Path $repoDir '.git'))) {\n"
|
||||
" if (Test-Path -LiteralPath $repoDir) { Remove-Item -LiteralPath $repoDir -Recurse -Force }\n"
|
||||
" New-Item -ItemType Directory -Path (Split-Path $repoDir -Parent) -Force | Out-Null\n"
|
||||
" & git.exe clone -b $branch $gitUrl $repoDir\n"
|
||||
" if ($LASTEXITCODE -ne 0) { throw \"git clone failed (exit $LASTEXITCODE)\" }\n"
|
||||
"} else {\n"
|
||||
" Push-Location $repoDir\n"
|
||||
" & git.exe fetch origin\n"
|
||||
" & git.exe reset --hard \"origin/$branch\"\n"
|
||||
" $code = $LASTEXITCODE\n"
|
||||
" Pop-Location\n"
|
||||
" if ($code -ne 0) { throw \"git reset failed (exit $code)\" }\n"
|
||||
"}\n"
|
||||
"$deploy = Join-Path $repoDir 'Deploy-LoginMonitor.ps1'\n"
|
||||
"if (-not (Test-Path -LiteralPath $deploy)) { throw \"Deploy-LoginMonitor.ps1 missing in git checkout: $repoDir\" }\n"
|
||||
"& $deploy -SourceShareRoot $repoDir\n"
|
||||
)
|
||||
return _wrap_powershell_body(body)
|
||||
|
||||
|
||||
@@ -319,12 +365,18 @@ def run_winrm_rdp_monitor_update(
|
||||
user: str,
|
||||
password: str,
|
||||
script_path: str = "",
|
||||
repo_url: str = "",
|
||||
git_branch: str = "main",
|
||||
) -> WinRmCmdResult:
|
||||
return run_winrm_ps(
|
||||
target=target,
|
||||
user=user,
|
||||
password=password,
|
||||
script=_rdp_update_powershell_script(script_path),
|
||||
script=_rdp_update_powershell_script(
|
||||
script_path,
|
||||
repo_url=repo_url,
|
||||
git_branch=git_branch,
|
||||
),
|
||||
timeout_sec=900,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user