From 1878106c7edbb84316563f0262f3ae876614f876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCnch?= Date: Sun, 29 Dec 2024 13:12:47 +0100 Subject: [PATCH] add a force re-registration option to the scan script (#504) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add a force re-registration option to the scan script Signed-off-by: Patrick Münch * rephrase the description of ForceRegistration parameter in scan.ps1 Signed-off-by: Patrick Münch --------- Signed-off-by: Patrick Münch --- mdm-scripts/windows/scan.ps1 | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/mdm-scripts/windows/scan.ps1 b/mdm-scripts/windows/scan.ps1 index 1c4394f8..c3a5d897 100644 --- a/mdm-scripts/windows/scan.ps1 +++ b/mdm-scripts/windows/scan.ps1 @@ -11,6 +11,8 @@ Set 'cnspec' (default) to download from a share and execute it .PARAMETER RegistrationToken Is required to register the Mondoo Product, if ConfigFile is not existent + .PARAMETER ForceRegistration + Is required to force re-registration the Mondoo Product. The cnspec client will be logged out and re-registered with the provided RegistrationToken. Default: $false .PARAMETER Proxy If provided, the proxy will be used for cnspec backend communication .PARAMETER Path @@ -25,7 +27,7 @@ Random delay in seconds before execution of the script .EXAMPLE scan.ps1 -Product cnspec - scan.ps1 -RegistrationToken 'InsertTokenHere' + scan.ps1 -RegistrationToken 'InsertTokenHere' -ForceRegistration $true scan.ps1 -Proxy 'http://proxy:8080' scan.ps1 -ExecutionPath 'C:\Users\Administrator\mondoo' scan.ps1 -DownloadPath '\\1.1.1.1\share' @@ -36,6 +38,7 @@ Param( [string] $Product = 'cnspec', [string] $RegistrationToken = '', + [bool] $ForceRegistration = $false, [string] $Proxy = '', [string] $ExecutionPath = '', [string] $DownloadPath = '', @@ -127,6 +130,7 @@ Your processor architecture $env:PROCESSOR_ARCHITECTURE is not supported yet. Co info "Arguments:" info (" Product: {0}" -f $Product) info (" RegistrationToken: {0}" -f $RegistrationToken) + info (" ForceRegistration: {0}" -f $ForceRegistration) info (" Proxy: {0}" -f $Proxy) info (" ExecutionPath: {0}" -f $ExecutionPath) info (" DownloadPath: {0}" -f $DownloadPath) @@ -172,6 +176,20 @@ If (![string]::IsNullOrEmpty($DownloadPath)) { fail "DownloadPath is required" } + # Cache the error action preference + $backupErrorActionPreference = $ErrorActionPreference + $ErrorActionPreference = "Continue" + +# Check if re-registration is forced +If ($ForceRegistration -and (Test-Path -Path "$($ConfigFile)")) { + # Prepare cnspec logout command + $logout_params = @("logout", "--config", "$ConfigFile", "--force") + info " * $Product Client is already registered. Logging out and back in again to update the registration" + $output = (& $program $logout_params 2>&1) + info "$output" + Remove-Item -Path "$($ConfigFile)" +} + # Check if cnspec is registered If (-not (Test-Path -Path "$($ConfigFile)")) { If ([string]::IsNullOrEmpty($RegistrationToken)) { @@ -180,10 +198,6 @@ If (-not (Test-Path -Path "$($ConfigFile)")) { info " * Register $Product Client" $login_params = @("login", "-t", "$RegistrationToken", "--config", "$ConfigFile") - # Cache the error action preference - $backupErrorActionPreference = $ErrorActionPreference - $ErrorActionPreference = "Continue" - # Capture all output from cnspec $output = (& $program $login_params 2>&1)