- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
NetMRI Powershell Authentication and Simple GET Request
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
02-15-2017 11:50 AM
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.
Re: NetMRI Powershell Authentication and Simple GET Request
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-31-2017 09:13 AM
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
Re: NetMRI Powershell Authentication and Simple GET Request
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
07-23-2018 10:14 AM
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
Re: NetMRI Powershell Authentication and Simple GET Request
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-06-2020 09:47 AM
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
Re: NetMRI Powershell Authentication and Simple GET Request
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
10-05-2020 11:20 AM - edited 10-05-2020 11:42 AM
@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?
Re: NetMRI Powershell Authentication and Simple GET Request
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
10-05-2020 11:44 AM
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`
Re: NetMRI Powershell Authentication and Simple GET Request
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
10-05-2020 01:21 PM
That worked thank you for the help.