Powershell to get list of hosts

I kept your coding style but added more error checking and a switch for UseBasicParsing. That code could be incorporated into your other functions

Declaration of a function to get host(s) in check_mk based on RegEx of hostname

function GetCheckmkHost
{
# parameter definition
param(
[Parameter(Position=0, Mandatory=$true)]
[string]$checkmkserver,
[Parameter(Position=1, Mandatory=$true)]
[string]$checkmkinstance,
[Parameter(Position=2, Mandatory=$true)]
[string]$checkmkuser,
[Parameter(Position=3, Mandatory=$true)]
[string]$checkmksecret,
[Parameter(Position=4, mandatory=$true)]
[string]$hostname,
[Parameter(mandatory=$false)]
[bool]$https = $false,
[Parameter(mandatory=$false)]
[switch]$usebasicparsing = $false
)

#######################
# variable definition #
#######################

# declare return variable for the function as hashtable
$return = @{}

# decide wether to use https or not
if ($https)
{
    $URI = "https://"
}
else
{
    $URI = "http://"
}

$URI += "$checkmkserver/$checkmkinstance/check_mk/view.py"

# build the web request for displaying the host with its status
$postParams = @{
_username=$checkmkuser
_secret=$checkmksecret
_transid='-1'
host_regex=$hostname # the hostname to search for
view_name='allhosts'
output_format='json'
}

# execute the web request
try
{
 if ($usebasicparsing)
 {
  $result = Invoke-WebRequest -Uri $URI -Method Post -Body $postParams -UseBasicParsing #DPD 11-24-2020 added -UseBasicParsing
 }
 else
 {
  $result = Invoke-WebRequest -Uri $URI -Method Post -Body $postParams
 }
}
catch
{
 $return.status = 1
 $return.msg = "error while connecting the check_mk api.`r`n$($error[0])"
 # return the error
 Return $return
}

# check if a respones was getting back
if (!$result)
{
    Write-Host "error while connecting the check_mk api."
    Write-Host
    $return.status = 1
    $return.msg = "error while connecting the check_mk api."
    # return the error
    Return $return
}

# check if the web request was successful
if ($result.StatusCode -ne 200 -or $result.Content.StartsWith('ERROR:'))
{
    Write-Host "error in the response from the check_mk api."
    Write-Host
    Write-Host $result
    $return.status = 1
    $return.msg = "error in the response from the check_mk api."
    # return the error
    Return $return
}

# convert the string content of the response into a powershell json object
$json = $result.content | convertfrom-json

# build array of property names
$field = @('')*($json[0].Count)
for ($i=0; $i -lt $json[0].Count; $i++ )
{
 $field[$i] = $json[0][$i]
}

# loop thru all hosts in results to build array of host objects
$hosts = @()
for ($j = 1; $j -lt $json.Count; $j++)
{
 $obj = New-Object PSObject
 for ($i=0; $i -lt $field.Count; $i++)
 {
  Add-Member -InputObject $obj -MemberType NoteProperty -Name $field[$i] -Value $json[$j][$i]
 }
 $hosts += @($obj)
}
$return.status = 0
$return.msg = "found no errors with host $hostname"
$return.host = $hosts
# return the error or the success message
Return $return

}

###########End of function declarations#######################

############

Example

############

connection variables

$mycheckmkserver = “insert checkmk servername”
$mycheckmkinstance = “insert checkmk instance”
$mycheckmkuser = “insert checkmk username using automation secret”
$mycheckmksecret = “insert checkmk automation secret of the username”

host to be checked, set downtime, patched, removed downtime and checked again

$myhost = '.’ # . means all or

execute function to get Checkmk host(s)

$result = GetCheckmkHost -checkmkserver $mycheckmkserver -checkmkinstance $mycheckmkinstance -checkmkuser $mycheckmkuser -checkmksecret $mycheckmksecret -hostname $myhost -usebasicparsing

if host was not healthy, write error and exit

if ($result.status -ne 0)
{
Write-Host “check_mk says the host $myhost is not healthy. Stopping script.”
Write-Output $result.msg
exit
}

#output hostnames to file
foreach ($hostobj in $result.host)
{
Write-Output $hostobj.host
}
exit

1 Like