Query Foreman REST API from PowerShell?

Ok, this is really kicking me in the backside… I can't seem to get a
Foreman REST API query to work from PowerShell? Here is the query I am
trying to do:

curl --user admin:password -H "Content-Type:application/json" -H

"Accept:application/json" -k https://foreman/api/v2/hosts

Here is the PowerShell script:

#[System.Net.ServicePointManager]::ServerCertificateValidationCallback =
{$true}

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}

"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object
TrustAllCertsPolicy

$dictionary=@{}
$username = "root"
$password = "password"
$uri = "https://foreman/api/v2/hosts"
$dictionary = New-Object
"System.Collections.Generic.Dictionary[[String],[String]]"
#$base64AuthInfo =
[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f
$username,$password)))
$base64AuthInfo =
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(("{0}:{1}" -f
$username,$password)))
$dictionary.Add("Authorization","$base64AuthInfo")
$dictionary.Add("Accept","application/json")
Invoke-RestMethod -Uri $uri -Headers $dictionary -ContentType
"application/json" -Method GET -Verbose
#Invoke-WebRequest -Uri $uri -Headers $dictionary -ContentType
"application/json" -Method GET -Verbose

Invoke-RestMethod and Invoke-WebRequest both fail with the same error…

VERBOSE: GET https://foreman/api/v2/hosts with 0-byte payload

Invoke-RestMethod : The underlying connection was closed: An unexpected
error occurred on a send.At line:38 char:1

  • Invoke-RestMethod -Uri $uri -Headers $dictionary -ContentType
    "application/json" …
··· + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Any help appreciated!

Actually found why this was not working, missing 'basic' parameter in
authorization specification. Working example…

curl --user admin:_P@ssw0rd -H "Content-Type:application/json" -H

"Accept:application/json" -k https://foreman/api/v2/hosts

#[System.Net.ServicePointManager]::ServerCertificateValidationCallback =
{$true}

add-type @"

using System.Net;

using System.Security.Cryptography.X509Certificates;

public class TrustAllCertsPolicy : ICertificatePolicy {

    public bool CheckValidationResult(

        ServicePoint srvPoint, X509Certificate certificate,

        WebRequest request, int certificateProblem) {

        return true;

    }

}

"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object
TrustAllCertsPolicy

··· #

$uri = “https://foreman/api/v2/hosts
$password = “_P@ssw0rd”
$username = “admin”
$dictionary=@{}
$base64AuthInfo =
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(("{0}:{1}" -f
$username,$password)))

$dictionary = New-Object
"System.Collections.Generic.Dictionary[[String],[String]]"
$dictionary.Add(“Authorization”,“Basic $base64AuthInfo”)
$dictionary.Add(“Accept”,“application/json”)

Invoke-RestMethod -Verbose -Uri $uri -Headers $dictionary -Method GET

Why mess with curl? You can install the hammer_cli and foreman_hammer_cli
to communicate with foreman via a cli in powershell.

Its pretty badass and no need to work with curl.

Corey

··· On Tuesday, January 13, 2015 at 11:02:37 AM UTC-8, Schorschi Decker wrote: > > Ok, this is really kicking me in the backside... I can't seem to get a > Foreman REST API query to work from PowerShell? Here is the query I am > trying to do: > > # curl --user admin:password -H "Content-Type:application/json" -H > "Accept:application/json" -k https://foreman/api/v2/hosts > > Here is the PowerShell script: > > #[System.Net.ServicePointManager]::ServerCertificateValidationCallback = > {$true} > > add-type @" > using System.Net; > using System.Security.Cryptography.X509Certificates; > > public class TrustAllCertsPolicy : ICertificatePolicy { > public bool CheckValidationResult( > ServicePoint srvPoint, X509Certificate certificate, > WebRequest request, int certificateProblem) { > return true; > } > } > "@ > > [System.Net.ServicePointManager]::CertificatePolicy = New-Object > TrustAllCertsPolicy > > $dictionary=@{} > $username = "root" > $password = "password" > $uri = "https://foreman/api/v2/hosts" > $dictionary = New-Object > "System.Collections.Generic.Dictionary[[String],[String]]" > #$base64AuthInfo = > [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f > $username,$password))) > $base64AuthInfo = > [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(("{0}:{1}" -f > $username,$password))) > $dictionary.Add("Authorization","$base64AuthInfo") > $dictionary.Add("Accept","application/json") > Invoke-RestMethod -Uri $uri -Headers $dictionary -ContentType > "application/json" -Method GET -Verbose > #Invoke-WebRequest -Uri $uri -Headers $dictionary -ContentType > "application/json" -Method GET -Verbose > > Invoke-RestMethod and Invoke-WebRequest both fail with the same error... > > VERBOSE: GET https://foreman/api/v2/hosts with 0-byte payload > > Invoke-RestMethod : The underlying connection was closed: An unexpected > error occurred on a send.At line:38 char:1 > + Invoke-RestMethod -Uri $uri -Headers $dictionary -ContentType > "application/json" ... > + > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > + CategoryInfo : InvalidOperation: > (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException > + FullyQualifiedErrorId : > WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand > > Any help appreciated! >

I know this is a very old post, but

  1. how did you get the rest call to work with invoke-restmethod from
    powershell?
    or
  2. how do you communicate with foreman via a cli in powershell?

I have hammer cli and hammer cli foreman installed but not sure how to
access this from powershell?

Thank you, I have tried following the hammer cli docs and various others
but not grasping it.

THank you very much

··· On Thursday, February 5, 2015 at 11:04:53 PM UTC-6, Corey Osman wrote:

Why mess with curl? You can install the hammer_cli and foreman_hammer_cli
to communicate with foreman via a cli in powershell.

Its pretty badass and no need to work with curl.

Corey

On Tuesday, January 13, 2015 at 11:02:37 AM UTC-8, Schorschi Decker wrote:

Ok, this is really kicking me in the backside… I can’t seem to get a
Foreman REST API query to work from PowerShell? Here is the query I am
trying to do:

curl --user admin:password -H “Content-Type:application/json” -H

“Accept:application/json” -k https://foreman/api/v2/hosts

Here is the PowerShell script:

#[System.Net.ServicePointManager]::ServerCertificateValidationCallback =
{$true}

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}

"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object
TrustAllCertsPolicy

$dictionary=@{}
$username = “root”
$password = “password”
$uri = “https://foreman/api/v2/hosts
$dictionary = New-Object
"System.Collections.Generic.Dictionary[[String],[String]]"
#$base64AuthInfo =
[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f
$username,$password)))
$base64AuthInfo =
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(("{0}:{1}" -f
$username,$password)))
$dictionary.Add(“Authorization”,"$base64AuthInfo")
$dictionary.Add(“Accept”,“application/json”)
Invoke-RestMethod -Uri $uri -Headers $dictionary -ContentType
"application/json" -Method GET -Verbose
#Invoke-WebRequest -Uri $uri -Headers $dictionary -ContentType
"application/json" -Method GET -Verbose

Invoke-RestMethod and Invoke-WebRequest both fail with the same error…

VERBOSE: GET https://foreman/api/v2/hosts with 0-byte payload

Invoke-RestMethod : The underlying connection was closed: An unexpected
error occurred on a send.At line:38 char:1

  • Invoke-RestMethod -Uri $uri -Headers $dictionary -ContentType
    "application/json" …
    + CategoryInfo          : InvalidOperation: 
(System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : 
WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Any help appreciated!

As no answer was given since now, i want to show my results by using hammer with powershell on windows.

First you need to install ruby on your windows machine. (https://rubyinstaller.org/downloads/)
Afterwards you can install the hammer cli with “gem install hammer_cli_katello”.

Now the hammer command should be available in cmd and powershell. To enable katello and foreman you need to add some information to your config:

C:\Ruby26-x64\lib\ruby\gems\2.6.0\gems\hammer_cli-0.19.0\config\cli_config.yml

:foreman:
  # Enable/disable foreman commands
  :enable_module: true

  # Your foreman server address
  :host: 'https://foreman'
  :username: 'xxxxxx'
  :password: 'xxxxxx'

:katello:
    :enable_module: true

Now starting hammer with that config and you can query hammer within powershell:

hammer -c "C:\Ruby26-x64\lib\ruby\gems\2.6.0\gems\hammer_cli-0.19.0\config\cli_config.yml" erratum list