Icinga2 Powershell Framework [Feedback]

Hi and thanks for your reply and fast fixing of these problems.

I though about a -silent switch where every arguments that is not given is set to “false” silently.

I would like a more secure way :slight_smile: I have the same problem with the password for the Agent-Service. I haven’t found a way yet, to encrypt the password in a way, that the adminuser can decrypt it at runtime on every Machine.

My quick and dirty function for adding the Hosts with the Director:

#API URL
[string]$DirectorUrl      = "https://$($DirectorServer):10443/icingaweb2/director"

$directorUser = "User"
$directorPass = "Passwor[d|t]"

#Convert Credentials to Base64 for URL Header
$bytes = [System.Text.Encoding]::UTF8.GetBytes(('{0}:{1}' -f $directorUser , $directorPass))
$authorization = 'Basic {0}' -f ([Convert]::ToBase64String($bytes))

#Create a new Header
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  
#Add converted Authorization
$headers.Add("Authorization", "$authorization")
#Add Application type
$headers.Add('Accept','application/json')


function Add-IcingaHost($hostname, $address, [array]$templates, $displayname, [hashtable]$vars, [switch]$simulate, [switch]$skipError) {
  if (-not $address -or -not $hostname -or -not $templates) {
    write-host "At least these fields are required: -templates $templates -address $address -hostname $hostname"
    break
  }
  $hostObj = New-Object -TypeName psobject
  $hostObj | Add-Member -MemberType NoteProperty -Name object_name -Value "$hostname"
  $hostObj | Add-Member -MemberType NoteProperty -Name object_type -Value "object"
  $hostObj | Add-Member -MemberType NoteProperty -Name address -Value "$address"
  $hostObj | Add-Member -MemberType NoteProperty -Name imports -Value $templates
  if ($displayname) { $hostObject | Add-Member -MemberType NoteProperty -Name displayname -Value "$displayname" }
  if ($vars) {
    $hostObj | Add-Member -MemberType NoteProperty -Name vars -Value $vars
  }

  $body = $hostObj | ConvertTo-Json 
  if ($simulate) {
    write-host "Body:" -ForegroundColor Green
    write-host $body 
  } else {
    try {
      Invoke-RestMethod -Method POST -Headers $headers  -Uri "$DirectorURL/host" -Body $body
    } catch {
      #$_.Exception #.Message
      write-host $_ -ForegroundColor Red
      write-host "URI: $BaseURL/host"
      write-host "body:`n$body"
      if (!($skipError)) {
        $answer = read-host "Continue? [y/n]?"
        if ( $answer -ne "y" ) { exit }
      }
    }
  }
}

$templates =   @( "WINDOWS SERVER AGENT", "$SatelliteTemplate" )
$vars = @{type="vm"}
$vars.Add("os", $os)
$vars.Add("windows_disks", $disklist)

Add-IcingaHost  -templates $templates -hostname "$hostname" -address "$hostname"  -vars $vars #-simulate

1 Like