Skip to content

Commit

Permalink
🧹 improve the powershell error handling for the cnspec login command (#…
Browse files Browse the repository at this point in the history
…379)

* improve the powershell error handling for the cnspec login command

Signed-off-by: Patrick Münch <[email protected]>

* update powershell gallery module

Signed-off-by: Patrick Münch <[email protected]>

---------

Signed-off-by: Patrick Münch <[email protected]>
  • Loading branch information
atomic111 authored Oct 29, 2023
1 parent d5fa35a commit 0ddb5c1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
20 changes: 19 additions & 1 deletion install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,26 @@ function Install-Mondoo {
If (![string]::IsNullOrEmpty($Proxy)) {
$login_params = $login_params + @("--api-proxy", "$Proxy")
}

$program = "$Path\cnspec.exe"
& $program $login_params

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

# Capture all output from mysql
$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 (@(0,3010) -contains $process.ExitCode) {
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.2.0'
ModuleVersion = '1.3.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
20 changes: 19 additions & 1 deletion powershell/Mondoo.Installer/Mondoo.Installer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,26 @@ function Install-Mondoo {
If (![string]::IsNullOrEmpty($Proxy)) {
$login_params = $login_params + @("--api-proxy", "$Proxy")
}

$program = "$Path\cnspec.exe"
& $program $login_params

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

# Capture all output from mysql
$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 (@(0,3010) -contains $process.ExitCode) {
Expand Down

0 comments on commit 0ddb5c1

Please sign in to comment.