From 30c0f1d584296177d8fd11f3e67d78ec9e1d6501 Mon Sep 17 00:00:00 2001 From: bc24fl Date: Wed, 16 Feb 2022 15:01:57 -0500 Subject: [PATCH] added KB_Approve_Install.ps1 community script --- community_scripts.json | 14 +++++ scripts/Kb_Approve_Install.ps1 | 99 ++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 scripts/Kb_Approve_Install.ps1 diff --git a/community_scripts.json b/community_scripts.json index a7cea9b8..9ffc25e3 100644 --- a/community_scripts.json +++ b/community_scripts.json @@ -1004,5 +1004,19 @@ ], "shell": "powershell", "category": "TRMM (Win):3rd Party Software" + }, + { + "guid": "9eda0e93-b4f8-4fb3-8a5d-2636f67b7663", + "filename": "Kb_Approve_Install.ps1", + "submittedBy": "https://github.com/bc24fl/tacticalrmm-scripts/", + "name": "KB Approve Install", + "description": "Toggle approval and installation of a specific Kb across all agents", + "args": [ + "-ApiKeyTactical {{global.ApiKeyTactical}}", + "-ApiUrlTactical {{global.ApiUrlTactical}}", + "-Kb [Microsoft Kb #]" + ], + "shell": "powershell", + "category": "TRMM (Win):Updates" } ] diff --git a/scripts/Kb_Approve_Install.ps1 b/scripts/Kb_Approve_Install.ps1 new file mode 100644 index 00000000..d08ebe2b --- /dev/null +++ b/scripts/Kb_Approve_Install.ps1 @@ -0,0 +1,99 @@ +<# +.SYNOPSIS + Toggle the approval and installation of a specific KB across all agents using the API and no 3rd party libraries. + +.REQUIREMENTS + - You will need an API key from Tactical RMM which should be passed as parameters (DO NOT hard code in script). Do not run this on each agent (see notes). + +.NOTES + - This script is designed to run on a single computer. Ideally, it should be run on the Tactical RMM server or other trusted device. + - This script cycles through each agent toggling the approval and installation of a specific kb. Tactical will do the installation when it's ready. + +.PARAMETERS + - $ApiKeyTactical - Tactical API Key + - $ApiUrlTactical - Tactical API Url + - $Kb - Microsoft Kb number to install + +.EXAMPLE + - KB_Approve_Install.ps1 -ApiKeyTactical 1234567 -ApiUrlTactical api.yourdomain.com -Kb "KB123456789" + +.VERSION + - v1.0 Initial Release by https://github.com/bc24fl/tacticalrmm-scripts/ +#> + +param( + [string] $ApiKeyTactical, + [string] $ApiUrlTactical, + [string] $Kb +) + +if ([string]::IsNullOrEmpty($ApiKeyTactical)) { + throw "ApiKeyTactical must be defined. Use -ApiKeyTactical to pass it." +} + +if ([string]::IsNullOrEmpty($ApiUrlTactical)) { + throw "ApiUrlTactical must be defined. Use -ApiUrlTactical to pass it." +} + +if ([string]::IsNullOrEmpty($Kb)) { + throw "Kb must be defined. Use -Kb to pass it." +} + +$headers= @{ + 'X-API-KEY' = $ApiKeyTactical +} + +# Get all agents +try { + $agentsResult = Invoke-RestMethod -Method 'Get' -Uri "https://$ApiUrlTactical/agents" -Headers $headers -ContentType "application/json" +} +catch { + throw "Error invoking get all agents on Tactical RMM with error: $($PSItem.ToString())" +} + +foreach ($agents in $agentsResult) { + + $agentId = $agents.agent_id + $agentHostname = $agents.hostname + $agentStatus = $agents.status + + # Get agent updates + try { + $agentUpdateResult = Invoke-RestMethod -Method 'Get' -Uri "https://$ApiUrlTactical/winupdate/$agentId/" -Headers $headers -ContentType "application/json" + } + catch { + Write-Error "Error invoking winupdate on agent $agentHostname - $agentId with error: $($PSItem.ToString())" + } + + foreach ($update in $agentUpdateResult){ + $updateId = $update.id + $updateKb = $update.kb + $updateAction = $update.action + + if ($Kb -eq $updateKb -And $updateAction -eq "nothing"){ + Write-Host "KB $updateKB is available for installation on agent $agentHostname" + + # Set Approve KB + $body = @{ + "action" = "approve" + } + try { + $updateApproveKb = Invoke-RestMethod -Method 'Put' -Uri "https://$ApiUrlTactical/winupdate/$updateId/" -Body ($body|ConvertTo-Json) -Headers $headers -ContentType "application/json" + Write-Host "Agent $agentHostname toggling approval of $updateKB" + } + catch { + Write-Error "Error invoking Approve KB on agent $agentHostname - $agentId with error: $($PSItem.ToString())" + } + + # Set Install KB + $body = @{} + try { + $updateInstallKb = Invoke-RestMethod -Method 'Post' -Uri "https://$ApiUrlTactical/winupdate/$agentId/install/" -Body ($body|ConvertTo-Json) -Headers $headers -ContentType "application/json" + Write-Host "Agent $agentHostname toggling installation of $updateKB" + } + catch { + Write-Error "Error invoking Install KB on agent $agentHostname - $agentId with error: $($PSItem.ToString())" + } + } + } +}