Grabbing info from HiveManager via Powershell

  • 2
  • Question
  • Updated 3 years ago
I'm looking for some help with a little script I'm trying to get working.
I would post this on a scripting community site... but there is more knowledge here about HiveManager than there would be elsewhere :)

The script is using powershell to open a web session to HiveManager to get client information.
At the moment, I just want it to login... but I'm stuck with the HTTP request.

The script is as follows...

$user_agent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; MALNJS)"
$url_base = "https://hivemanager";
$urluser = "user"
$urlpass = "pass"

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


$r1 = Invoke-WebRequest -Uri "$url_base/hm/login.action" -SessionVariable "s" -UserAgent $user_agent
$r1.Forms[0].Fields.userName = $urluser
$r1.Forms[0].Fields.authenticateFormID_password = $urlpass
#$r2 = Invoke-WebRequest -Uri ("$url_base/hm/login.action" + $r1.Forms[0].Action) -WebSession $s -Body $r1.Forms[0].Fields -Method Post -UserAgent $user_agent
$r2 = Invoke-WebRequest -Uri ("$url_base/hm/authenticate.action") -WebSession $s -Body $r1.Forms[0].Fields -Method Post -UserAgent $user_agent
$export = invoke-webrequest -uri "https://hivemanager/hm/clientMonitor.action"; -WebSession $s

$export.forms | Out-GridView

If anyone could give me some ideas/help/buttkicks to get it at least logging on, that'd be great!
Photo of James Dodds

James Dodds

  • 13 Posts
  • 2 Reply Likes

Posted 3 years ago

  • 2
Photo of James Dodds

James Dodds

  • 13 Posts
  • 2 Reply Likes
Nevermind!!

The password field was incorrect. It is just password, not authenticateFormID_password.

The guts of the script now read...

$r1 = Invoke-WebRequest -Uri "$url_base/hm/login.action" -SessionVariable "s" -UserAgent $user_agent
$r1.Forms[0].Fields.userName = $urluser
$r1.Forms[0].Fields.password = $urlpass
$r2 = Invoke-WebRequest -Uri ("$url_base/hm/authenticate.action") -WebSession $s -Body $r1.Forms[0].Fields -Method Post -UserAgent $user_agent
$export = invoke-webrequest -uri "https://hivemanager/hm/clientMonitor.action"; -WebSession $s
Photo of Ruwan Indika

Ruwan Indika

  • 66 Posts
  • 22 Reply Likes
Hi James,

Seems like you got the script working, anyway Just did a quick test using powershell to login to HiveManager, 
$url = "https://myhive-auth.aerohive.com" 
$username="user1@aerohive.com" 
$password="password123" 
$ie = New-Object -com internetexplorer.application; 
$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy -eq $true) 
{ 
    Start-Sleep -Milliseconds 1000; 
} 
$ie.Document.getElementById("username").value = $username 
$ie.Document.getElementByID("Password").value=$password 
$ie.Document.getElementById("submit").Click();
(Edited)
Photo of James Dodds

James Dodds

  • 13 Posts
  • 2 Reply Likes
Cheers Ruwan.
Trying to get information from the Client page and parse it with Powershell has been.... tricky.
Trying via the API now.
The line for Powershell is
Invoke-RestMethod -Uri "https://hivemanager/hm/api/v1/clients?q=10.175" -ContentType "application/json" -SessionVariable s -method GET  -headers @{Authorization=("Basic {0}" -f $encodedpassword)}
This will return all the clients on our network. Powershell can filter them out after that.
(Edited)
Photo of Sander

Sander

  • 2 Posts
  • 0 Reply Likes
Hi James, How did the API work out for you? Did you manage to get the client data? Thanks, Sander
Photo of James Dodds

James Dodds

  • 13 Posts
  • 2 Reply Likes

Hey Sander,

Apart from the horrible formatting in my last post, the API worked great. Client data came through without an issue.

Did you need assistance with something?

Photo of Sander

Sander

  • 2 Posts
  • 0 Reply Likes
Thanks for your reply! I am trying out the API within the Aerohive demo-environment (https://demo-useast-001.aerohive.com) it does not seem to work unfortunately. The account seems UNAUTHORIZED. I will fiddle some more ... thanks
Photo of Andrew Garcia

Andrew Garcia, Official Rep

  • 368 Posts
  • 120 Reply Likes
The HiveManager 6 API is only available for on-premises versions of HiveManager.