Skip to main content
Tweeted twitter.com/StackCodeReview/status/768042933656383492
added 1 character in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

First Powershell cmdlet authenticating to Aerohive API

After dipping my toes in PowerShell for over a year i think it is time for creating a real cmdlet. My first mission was creating a regular PS script to take in a .csv and create a wifi account using the Aerohive REST api. Although

Although it works, i find the script bloated and ugly, and want to swap large code block for proper cmdlets that can also be used by my colleagues.

First Powershell cmdlet

After dipping my toes in PowerShell for over a year i think it is time for creating a real cmdlet. My first mission was creating a regular PS script to take in a .csv and create a wifi account using the Aerohive REST api. Although it works, i find the script bloated and ugly, and want to swap large code block for proper cmdlets that can also be used by my colleagues.

Powershell cmdlet authenticating to Aerohive API

After dipping my toes in PowerShell for over a year i think it is time for creating a real cmdlet. My first mission was creating a regular PS script to take in a .csv and create a wifi account using the Aerohive REST api.

Although it works, i find the script bloated and ugly, and want to swap large code block for proper cmdlets that can also be used by my colleagues.

Source Link
Adamar
  • 246
  • 2
  • 7

First Powershell cmdlet

After dipping my toes in PowerShell for over a year i think it is time for creating a real cmdlet. My first mission was creating a regular PS script to take in a .csv and create a wifi account using the Aerohive REST api. Although it works, i find the script bloated and ugly, and want to swap large code block for proper cmdlets that can also be used by my colleagues.

This is the first step, authenticating against the api. At first i'm only trying to get the basic functions to work. In a later phase i'll add error handling and stuff.

For now i'm also using restsharp. If i ever complete this, i want to swap it for the builtin .net classes so there are no external dependencies.

The next cmdlets will be for creating and deleting wifi accounts. But at first i wanna know if i'm using the correct syntax, proper use of variables, and if everything is secure enough.

using System;
using System.Management.Automation;
using RestSharp;

namespace Aerohive
{
    [Cmdlet(VerbsCommon.New, "Token")]
    [OutputType(typeof(Token))]
    public class NewTokenCmdlet : PSCmdlet
    {
        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "The api key, as provided by the IDManager web interface.")]
        public string apiKey { get; set; }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "The username you use to login on the web interace of the IDManager")]
        public string username { get; set; }

        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            HelpMessage = "The password you use to login on the web interface of the IDManager")]
        public string password { get; set; }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }

        protected override void ProcessRecord()
        {
            WriteVerbose("Authenticating ...");

            var client = new RestClient();
            client.BaseUrl = new Uri("very.long.url/to/authenticate/against");

            var request = new RestRequest("authenticate", Method.POST);
            request.AddHeader("content-type", "application/json");
            request.AddJsonBody(new
            {
                token = apiKey,
                userName = username,
                password = password
            });

            WriteVerbose("response");
            IRestResponse<Token> response = client.Execute<Token>(request);
            WriteObject(response.Data);
        }

        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }
    public class Token
    {
        public string token { get; set; }
        public int errorCode { get; set; }
        public string errorMessage { get; set; }
    }
}