"""Create scheduled task on AD2 to swap agent binary and restart service.""" import requests, time # Auth token_r = requests.post('http://172.16.3.30:3001/api/auth/login', json={ 'email': 'claude-api@azcomputerguru.com', 'password': 'ClaudeAPI2026!@#' }) token = token_r.json()['token'] headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'} agent_id = 'd28a1c90-47d7-448f-a287-197bc8892234' # Create a scheduled task to swap the binary and restart # Uses schtasks for simplicity - runs a PowerShell script that: # 1. Stops the agent service # 2. Backs up old binary # 3. Copies new binary in place # 4. Starts the service # 5. Writes result to file cmd = ( "$script = @'\n" "Stop-Service GuruRMMAgent -Force\n" "Start-Sleep -Seconds 3\n" "Copy-Item \"C:\\Program Files\\GuruRMM\\gururmm-agent.exe\" \"C:\\Program Files\\GuruRMM\\gururmm-agent.exe.bak\" -Force\n" "Copy-Item \"C:\\Temp\\gururmm-agent-new.exe\" \"C:\\Program Files\\GuruRMM\\gururmm-agent.exe\" -Force\n" "Start-Sleep -Seconds 1\n" "Start-Service GuruRMMAgent\n" "\"Agent updated at $(Get-Date)\" | Out-File C:\\Temp\\update_result.txt\n" "'@\n" "$script | Out-File C:/Temp/update_agent.ps1 -Encoding UTF8\n" "Write-Output ('Script written: ' + (Get-Item C:/Temp/update_agent.ps1).Length.ToString() + ' bytes')" ) print("Step 1: Writing update script to AD2...") r = requests.post( 'http://172.16.3.30:3001/api/agents/' + agent_id + '/command', headers=headers, json={'command_type': 'powershell', 'command': cmd, 'timeout_seconds': 30} ) print('Send:', r.json()) cmd_id = r.json()['command_id'] time.sleep(10) r2 = requests.get('http://172.16.3.30:3001/api/commands/' + cmd_id, headers=headers) d = r2.json() print('Status:', d['status']) print('stdout:', d.get('stdout', '')) if d.get('stderr'): print('stderr:', (d.get('stderr', '') or '')[:300]) if d['status'] != 'completed': print("FAILED to write script") exit(1) # Step 2: Create and run scheduled task print("\nStep 2: Creating scheduled task to run update...") cmd2 = ( "schtasks /create /tn AgentBinaryUpdate /tr " "\"powershell.exe -ExecutionPolicy Bypass -File C:\\Temp\\update_agent.ps1\" " "/sc ONCE /st 00:00 /sd 01/01/2030 /ru SYSTEM /rl HIGHEST /f; " "schtasks /run /tn AgentBinaryUpdate; " "Write-Output 'Update task started - agent will restart momentarily'" ) r = requests.post( 'http://172.16.3.30:3001/api/agents/' + agent_id + '/command', headers=headers, json={'command_type': 'powershell', 'command': cmd2, 'timeout_seconds': 30} ) print('Send:', r.json()) cmd_id = r.json()['command_id'] time.sleep(10) r2 = requests.get('http://172.16.3.30:3001/api/commands/' + cmd_id, headers=headers) d = r2.json() print('Status:', d['status']) print('stdout:', d.get('stdout', '')) if d.get('stderr'): print('stderr:', (d.get('stderr', '') or '')[:300]) print("\nAgent will stop, binary will be swapped, then service restarts.") print("Wait ~30 seconds then check if agent reconnects.")