patterncsharpMinor
Powershell cmdlet authenticating to Aerohive API
Viewed 0 times
aerohivepowershellcmdletauthenticatingapi
Problem
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()
{
WriteVerbos
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()
{
WriteVerbos
Solution
You are writing a powershell commandlet... are you sure you want to hardcode the url?
I think it would be better to provide a parameter for this one too. At least make it optional with a default value if you don't want to type it everythime but hardcoding it - well, I woudn't.
It doesn't take long and you'll find another use for it but then you'll say if I only made a parameter for this :-)
client.BaseUrl = new Uri("very.long.url/to/authenticate/against");I think it would be better to provide a parameter for this one too. At least make it optional with a default value if you don't want to type it everythime but hardcoding it - well, I woudn't.
It doesn't take long and you'll find another use for it but then you'll say if I only made a parameter for this :-)
Code Snippets
client.BaseUrl = new Uri("very.long.url/to/authenticate/against");Context
StackExchange Code Review Q#139415, answer score: 2
Revisions (0)
No revisions yet.