
Centreon through Powershell
May one day you’ll have to request centreon through Powershell, who knows !
I wrote a little function for that.
First, you’ll have to get your authtoken using this :
Function Connect-Centreon{
$params = @{
“username” = “$($Credentials.UserName)”;
“password” = “$($Credentials.GetNetworkCredential().Password)”;
}
$uri=”$($url)action=authenticate”
$auth=Invoke-webrequest -Uri $uri -body $params -Method POST
$authtoken=(($auth.Content | ConvertFrom-Json).authToken).ToString()
$token = @{}
$token.Add(“centreon-auth-token”, “$authToken”)
$Session = @{
server = $server
url = “$url”
token = $token
}
return $Session
}
Your requested variables are :
$url=”https://localhost/centreon/api/index.php?”
$Credentials=get-credential
Then, you can request Centreon !
Example to check a server status :
$authtoken=($session.token).’centreon-auth-token’
$headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers.Add(“centreon_auth_token”, “$authtoken”)
$headers.Add(“Content-Type”, “text/plain”)
$body = “{`”action`”:`”show`”, `”object`”:`”host`”, `”values`”:`”$hostname`”}”
$response =( (Invoke-RestMethod ‘https://localhost/centreon/api/index.php?action=action&object=centreon_clapi’ -Method ‘POST’ -Headers $headers -Body $body)| ConvertTo-Json)|ConvertFrom-Json
Cheers !