Last active
October 1, 2018 21:20
-
-
Save SheepReaper/361ef391b412f21be22cfef5dacf7b3f to your computer and use it in GitHub Desktop.
PowerShell implementation of Meraki Dashboard API (for a binary version of this, see here: https://github.com/bryan5989/SheepReaper.Meraki.DashboardApi.PowerShell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Globals | |
[string]$endpoint = "https://api.meraki.com/api/v0" | |
[Hashtable]$headers = @{ | |
"X-Cisco-Meraki-API-Key" = $env:MerakiApiKey | |
"Content-Type" = 'application/json' | |
} | |
# Private functions | |
function persistApiKey { | |
if ($env:MerakiApiKey -eq [System.Environment]::GetEnvironmentVariable("MerakiApiKey", "User")) { | |
Write-Warning -Message $("Session api key matches value stored in User Environment " + | |
"variable. Nothing to update.") | |
} | |
else { | |
[System.Environment]::SetEnvironmentVariable("MerakiApiKey", $env:MerakiApiKey, "User") | |
Write-Host "Persited api key from current session into User Environment variable." | |
} | |
} | |
function updateHeaders { | |
$headers["X-Cisco-Meraki-API-Key"] = $env:MerakiApiKey | |
} | |
<# | |
.SYNOPSIS | |
Set the api key that will be used for Meraki Dashboard Rest API calls. | |
.DESCRIPTION | |
Set the api key that will be used for Meraki Dashboard Rest API calls. Optionally, | |
persist the api key for future sessions with the -Persist parameter. A new Api Key can be | |
generated from your User Profile in the Meraki Dashboard. | |
.PARAMETER ApiKey | |
[string] representation of your Meraki Api Key. | |
.PARAMETER Persist | |
Optional paremeter enables the current or supplied api key to be stored in a user environment | |
variable for future sessions to use | |
.EXAMPLE | |
Set-MerakiApiKey -ApiKey AABBCC11223344 | |
#> | |
function Set-MerakiApiKey { | |
[CmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline = $true, Position = 1)] | |
[string]$ApiKey, | |
[switch]$Persist | |
) | |
if (!($ApiKey -or $Persist)) { | |
Write-Error -Exception [System.ArgumentNotSpecifiedException] ` | |
-Message "Neither an ApiKey or the -Persist parameter were specified." ` | |
-RecommendedAction $("Specify a Meraki ApiKey using -ApiKey, using -Persist to make " + | |
"the change permanent, or specify -Persist alone to store the current " + | |
"session's Api Key permanently.") | |
return | |
} | |
if ($Persist) { | |
if (!$env:MerakiApiKey) { | |
Write-Error -Exception [System.InvalidOperationException] ` | |
-RecommendedAction "Run command: Set-MerakiApiKey, specifying -ApiKey." ` | |
-Message $("The Meraki Dashboard Api Key was not previously specified, there is " + | |
"nothing to persist.") | |
return | |
} | |
if (!$ApiKey) { | |
persistApiKey | |
return | |
} | |
} | |
if ($ApiKey -eq $env:MerakiApiKey) { | |
Write-Warning -Message $("Supplied Api Key is the same as the one in use for this " + | |
"session. `n`nSession Api Key remains unchanged.") | |
} | |
else { | |
if ($env:MerakiApiKey) { | |
Write-Warning -Message "Updating this session's Api Key..." | |
} | |
$env:MerakiApiKey = $ApiKey | |
updateHeaders | |
Write-Host "Session Api Key successfully set" | |
} | |
if ($Persist) { persistApiKey } | |
return | |
} | |
function Get-MerakiOrganization { | |
[CmdletBinding()] | |
param([string]$Id) | |
[string]$uri = "$endpoint/organizations" | |
if ($Id) { $uri = "$uri/$Id" } | |
return Invoke-RestMethod -Method GET -Uri $uri -Headers $headers | |
} | |
function Set-MerakiSessionVariable { | |
[CmdletBinding()] | |
param([string]$ApiKey, $OrganizationId, $NetworkId) | |
if ($ApiKey) { Set-MerakiApiKey -ApiKey $ApiKey } | |
if ($OrganizationId) { | |
$env:MerakiOrganizationId = $OrganizationId | |
Write-Host "Updated Current Session Organization Id. Future commands will use it by default." | |
} | |
if ($NetworkId) { | |
$env:MerakiNetworkId = $NetworkId | |
Write-Host "Updated Current Session Network Id. Future commands will use it by default." | |
} | |
} | |
function Get-MerakiNetwork { | |
[CmdletBinding(DefaultParameterSetName = "organizationQuery")] | |
param( | |
[Parameter(ParameterSetName = "pipelineInput", ValueFromPipeline = $true)] | |
$PipelineInput, | |
[Parameter(ParameterSetName = "organizationQuery")] | |
[string]$OrganizationId = $env:MerakiOrganizationId, | |
[Parameter(ParameterSetName = "directQuery")] | |
[string]$Id = $env:MerakiNetworkId | |
) | |
[string]$uri = $endpoint | |
switch ($PSCmdlet.ParameterSetName) { | |
"pipelineInput" { | |
$uri = "$uri/organizations/$($PipelineInput.id)/networks" | |
} | |
"organizationQuery" { | |
$uri = "$uri/organizations/$OrganizationId/networks" | |
} | |
"directQuery" { | |
$uri = "$uri/networks/$Id" | |
} | |
} | |
return Invoke-RestMethod -Method GET -Uri $uri -Headers $headers | |
} | |
# Run Once on Import | |
# Set TLS version to 1.2 <- required by Meraki | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
# This runs on import to obviate if a Key was successfully read from environment | |
if (!$env:MerakiApiKey) { | |
Write-Warning -Message $("An API Key for Meraki Dashboard was not found in the environment " + | |
"variables. (Did you forget to -Persist, previously? Or perhaps this is your first " + | |
"time importing this module?) You will need to run:") | |
Write-Host "`tSet-MerakiApiKey -ApiKey <string> [-Persist]" | |
Write-Warning "All other commands provided by this module will fail until you do." | |
} | |
else { | |
Write-Host "Using Dashboard API key from environment." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment