Skip to content

Commit

Permalink
add reregister feature to powershell script (#395)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Münch <[email protected]>
  • Loading branch information
atomic111 authored Dec 6, 2023
1 parent 4b59c02 commit b177c0c
Show file tree
Hide file tree
Showing 4 changed files with 356 additions and 292 deletions.
148 changes: 90 additions & 58 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ function Install-Mondoo {
}

function enable_service() {
info "Set cnspec to run as a service automatically at startup and start the service"
Set-Service -Name mondoo -Status Running -StartupType Automatic
info " * Set cnspec to run as a service automatically at startup and start the service"
If((Get-Service -Name Mondoo).Status -eq 'Running') {
info " * Restarting $Product Service as it is already running"
Restart-Service -Name Mondoo -Force
}
Set-Service -Name Mondoo -Status Running -StartupType Automatic
If(((Get-Service -Name Mondoo).Status -eq 'Running') -and ((Get-Service -Name Mondoo).StartType -eq 'Automatic') ) {
success "* Mondoo Service is running and start type is automatic"
success "* $Product Service is running and start type is automatic"
} Else {
fail "Mondoo service configuration failed"
}
Expand All @@ -129,7 +133,7 @@ function Install-Mondoo {

function CreateAndRegisterMondooUpdaterTask($taskname, $taskpath)
{
info "Create and register the Mondoo update task"
info " * Create and register the Mondoo update task"
NewScheduledTaskFolder $taskpath

$taskArgument = '-NoProfile -WindowStyle Hidden -ExecutionPolicy RemoteSigned -Command &{ [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $wc = New-Object Net.Webclient; '
Expand Down Expand Up @@ -259,17 +263,21 @@ function Install-Mondoo {
$releaseurl = "https://install.mondoo.com/package/${Product}/windows/${arch}/${filetype}/${version}/download"
}

If ($version -ne $installed_version.version) {
# Check if Path exists
$Path = $Path.trim('\')
If (!(Test-Path $Path)) {New-Item -Path $Path -ItemType Directory}
# Check if Path exists
$Path = $Path.trim('\')
If (!(Test-Path $Path)) {New-Item -Path $Path -ItemType Directory}

If ($version -ne $installed_version.version) {
# download windows binary zip/msi
$downloadlocation = "$Path\$Product.$filetype"
info " * Downloading $Product from $releaseurl to $downloadlocation"
download $releaseurl $downloadlocation
} Else {
info " * Do not download $Product as latest version is already installed."
}

If ($filetype -eq 'zip') {
If ($filetype -eq 'zip') {
If ($version -ne $installed_version.version) {
info ' * Extracting zip...'
# remove older version if it is still there
Remove-Item "$Path\$Product.exe" -Force -ErrorAction Ignore
Expand All @@ -278,18 +286,20 @@ function Install-Mondoo {
Remove-Item $downloadlocation -Force

success " * $Product was downloaded successfully! You can find it in $Path\$Product.exe"
}

If ($UpdateTask.ToLower() -eq 'enable') {
# Creating a scheduling task to automatically update the Mondoo package
$taskname = $Product + "Updater"
$taskpath = $Product
If(Get-ScheduledTask -TaskName $taskname -EA 0)
{
Unregister-ScheduledTask -TaskName $taskname -Confirm:$false
}
CreateAndRegisterMondooUpdaterTask $taskname $taskpath
If ($UpdateTask.ToLower() -eq 'enable') {
# Creating a scheduling task to automatically update the Mondoo package
$taskname = $Product + "Updater"
$taskpath = $Product
If(Get-ScheduledTask -TaskName $taskname -EA 0)
{
Unregister-ScheduledTask -TaskName $taskname -Confirm:$false
}
} ElseIf ($filetype -eq 'msi') {
CreateAndRegisterMondooUpdaterTask $taskname $taskpath
}
} ElseIf ($filetype -eq 'msi') {
If ($version -ne $installed_version.version) {
info ' * Installing msi package...'
$file = Get-Item $downloadlocation
$packageName = $Product
Expand All @@ -307,73 +317,95 @@ function Install-Mondoo {
info (' * Run installer {0} and log into {1}' -f $downloadlocation, $logFile)
$process = Start-Process "msiexec.exe" -Wait -NoNewWindow -PassThru -ArgumentList $argsList
# https://docs.microsoft.com/en-us/windows/win32/msi/error-codes
}

If (![string]::IsNullOrEmpty($RegistrationToken)) {
info " * Register $Product Client"
$login_params = @("login", "-t", "$RegistrationToken", "--config", "C:\ProgramData\Mondoo\mondoo.yml")
If (![string]::IsNullOrEmpty($Proxy)) {
$login_params = $login_params + @("--api-proxy", "$Proxy")
}

$program = "$Path\cnspec.exe"
If (![string]::IsNullOrEmpty($RegistrationToken)) {
# Prepare cnspec logout command
$logout_params = @("logout", "--config", "C:\ProgramData\Mondoo\mondoo.yml", "--force")

# Cache the error action preference
$backupErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
# Prepare cnspec login command
$login_params = @("login", "-t", "$RegistrationToken", "--config", "C:\ProgramData\Mondoo\mondoo.yml")
If (![string]::IsNullOrEmpty($Proxy)) {
$login_params = $login_params + @("--api-proxy", "$Proxy")
}

# Capture all output from cnspec
$output = (& $program $login_params 2>&1)
$program = "$Path\cnspec.exe"

# Restore the error action preference
$ErrorActionPreference = $backupErrorActionPreference
# Cache the error action preference
$backupErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"

# Logout if already cnspec client registred in
If ((Test-Path -Path "C:\ProgramData\Mondoo\mondoo.yml")) {
info " * $Product Client is already registered. Logging out and back in again to update the registration"
$output = (& $program $logout_params 2>&1)
if ($output -match "ERROR") {
throw $output
} elseif($output) {
info "$output"
} else {
info "No output"
}
Remove-Item "C:\ProgramData\Mondoo\mondoo.yml" -Force
}

info " * Register $Product Client"

# Login to register cnspec client
$output = (& $program $login_params 2>&1)

# Restore the error action preference
$ErrorActionPreference = $backupErrorActionPreference

if ($output -match "ERROR") {
throw $output
} elseif($output) {
info "$output"
} else {
info "No output"
}
}

If ($version -ne $installed_version.version) {
If (@(0,3010) -contains $process.ExitCode) {
success " * $Product was installed successfully!"
} Else {
fail (" * $Product installation failed with exit code: {0}" -f $process.ExitCode)
}
} Else {
success " * $Product is already installed in the latest version and registered."
}

# Check if Service parameter is set and Parameter Product is set to mondoo
If ($Service.ToLower() -eq 'enable' -and $Product.ToLower() -eq 'mondoo') {
# start Mondoo service
enable_service
}

If ($UpdateTask.ToLower() -eq 'enable') {
# Creating a scheduling task to automatically update the Mondoo package
$taskname = $Product + "Updater"
$taskpath = $Product
If (Get-ScheduledTask -TaskName $taskname -EA 0)
{
Unregister-ScheduledTask -TaskName $taskname -Confirm:$false
}
CreateAndRegisterMondooUpdaterTask $taskname $taskpath
# Check if Service parameter is set and Parameter Product is set to mondoo
If ($Service.ToLower() -eq 'enable' -and $Product.ToLower() -eq 'mondoo') {
# start Mondoo service
enable_service
}

If ($UpdateTask.ToLower() -eq 'enable') {
# Creating a scheduling task to automatically update the Mondoo package
$taskname = $Product + "Updater"
$taskpath = $Product
If (Get-ScheduledTask -TaskName $taskname -EA 0)
{
Unregister-ScheduledTask -TaskName $taskname -Confirm:$false
}
CreateAndRegisterMondooUpdaterTask $taskname $taskpath
}

If ($version -ne $installed_version.version) {
Remove-Item $downloadlocation -Force

} Else {
fail "${filetype} is not supported for download"
}

# Display final message
info "
Thank you for installing $Product!"

} Else {
# Display final message
info "
Latest $Product version alread installed!"
fail "${filetype} is not supported for download"
}

# Display final message
info "
Thank you for installing $Product!"

info "
If you have any questions, please come join us in our Mondoo Community on GitHub Discussions:
Expand Down
2 changes: 1 addition & 1 deletion mdm-scripts/windows/scan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
scan.ps1 -Product cnspec
scan.ps1 -RegistrationToken 'InsertTokenHere'
scan.ps1 -Proxy 'http://proxy:8080'
scan.ps1 -Path 'C:\Users\Administrator\mondoo'
scan.ps1 -ExecutionPath 'C:\Users\Administrator\mondoo'
scan.ps1 -DownloadPath '\\1.1.1.1\share'
scan.ps1 -ConfigFile 'C:\ProgramData\Mondoo\mondoo.yml'
scan.ps1 -LogDir 'C:\Windows\Temp'
Expand Down
2 changes: 1 addition & 1 deletion powershell/Mondoo.Installer/Mondoo.Installer.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
RootModule = './Mondoo.Installer.psm1'

# Version number of this module.
ModuleVersion = '1.3.0'
ModuleVersion = '1.4.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
Loading

0 comments on commit b177c0c

Please sign in to comment.