Learn How We Can Help You Keep Teleworkers Protected During the COVID-19 Crisis

API & Integration

Reply
Highlighted

NetMRI Powershell Authentication and Simple GET Request

Techie
Posts: 1
742     0

Hello,

 

I'm trying to get my foot into the door with the NetMRI RESTAPI using PowerShell. I've found very few examples of users using it, so I thought I would ask here. My goal is to pull a config of a device, but I havn't authenticated correctly yet. It seems I have a formatting issue, is there any chance someone could point me in the right direction?

 

$username = "USERNAME"
$secure_pw = ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($username, $secure_pw)
$uri = "https://netmriurl/api/authenticate"
Invoke-RestMethod -Uri $uri -Method GET -Credential $cred -ContentType "application/json"

 

 

Ideally, it would be nice not to have to specificy my credentials and use a token going forward.

 

 

Highlighted

Re: NetMRI Powershell Authentication and Simple GET Request

Adviser
Posts: 136
742     0

Hi,

 

I didn't have expirience with "Invoke-RestMethod" but I think that you use it incorrectly (-Credential switch). NetMRI expects username and password as parameters.

So here is an example using curl:

curl https://MY.NETMRI/api/authenticate.json -X GET -d '{"username":"MYUSER","password":"MYPASSWORD"}' -k -H "Content-Type: application/json" -c tmp.cookie

 

After that you should use the cookie-file

 

curl https://MY.NETMRI/api/server_info -X GET -k -H "Content-Type: application/json" -b tmp.cookie

 

 

BR,

Vadim

Highlighted

Re: NetMRI Powershell Authentication and Simple GET Request

Techie
Posts: 1
742     0

Is there someone that would be able to help with the orginal question and provide an example using powershell on how to authenticate to the NetMRI rest api endpoint?  https://<<SERVER_NAME>>/api/authenticate.json

Highlighted

Re: NetMRI Powershell Authentication and Simple GET Request

Authority
Posts: 43
742     0

The problem is that Powershell 5.1 and earlier only sends the supplied credentials via Basic authentication when prompted to by the web server (NetMRI). And NetMRI doesn't ever send that request. It expects you to send the credentials in the initial request or you're denied. If you're on Powershell 6 or later, there's an "-Authentication Basic" parameter you can use which will fix this. But if not, you have to do the work yourself like this.

 

# assume we're starting with your $cred variable
$toEncode = "$($cred.Username):$($cred.GetNetworkCredential().Password)"
$basicString = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($toEncode))
$authHeader = @{ Authorization = "Basic $basicString" }

# now you can authenticate by sending the explicit Authorization header
$uri = "https://netmriurl/api/authenticate"
Invoke-RestMethod $uri -Headers $authHeader

# if you want to add some efficiency, save the resulting session and use that for subsequent requests
Invoke-RestMethod $uri -Headers $authHeader -SessionVariable mysession

$uri = "https://netmriurl/api/3/server_info.json"
Invoke-RestMethod $uri -WebSession $mysession
Highlighted

Re: NetMRI Powershell Authentication and Simple GET Request

[ Edited ]
Techie
Posts: 2
743     0

@rmbolgerI have tried the approach you mentioned above and still get this error

 


Invoke-webrequest : {"error": "general\/validation-failed", "message": "The action failed because the request was not valid.", "fields": {"username": ["is required"], "password": ["is required"]}}

 

I have copied your exact code and I am using Get-StoredCredentia to pass them to the variable $cred will this work or am I doing something wrong?

Highlighted

Re: NetMRI Powershell Authentication and Simple GET Request

Authority
Posts: 43
743     0

In reviewing my own code, it looks like I made a poor assumption about the `/authenticate` endpoint specifically. It doesn't appear to care about the Basic authentication header.

 

Instead, try establishing a session to something else like `/server_info.json`. So something like this:

 

 

$uri = "https://netmriurl/api/3/server_info.json"
Invoke-RestMethod $uri -Headers $authHeader -SessionVariable mysession

 

This is what I do at the beginning of my scripts and then make all subsequent requests using `-WebSession $mysession`

 

 

 

Highlighted

Re: NetMRI Powershell Authentication and Simple GET Request

Techie
Posts: 2
743     0

That worked thank you for the help. 

Showing results for 
Search instead for 
Do you mean 

Recommended for You