cancel
Showing results for 
Search instead for 
Did you mean: 

Attempting to Change PSK of SSID with Meraki API via Powershell

SOLVED
Conversationalist

Attempting to Change PSK of SSID with Meraki API via Powershell

I am trying to execute a PUT script in PS, below is the function.  When I run the script I'm receiving a 308 error.  

 

[Net.ServicePointManager]:: SecurityProtocol = [Net.SecurityProtocolType]::Tls12

 

#Change Guest Wifi Password
$MerakiApiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
$NETWORK_ID = "XXXXXXXXXXXXXXXX"
$MerakiUrl = "https://dashboard.meraki.com/api/v0/networks/$NETWORK_ID/ssids/6"
$headers = @{}
$headers.Add("X-Cisco-Meraki-API-KEY",$MerakiApiKey)
$headers.Add("ContentType",'application/json')
$jbody = '{"psk": "NewPassword"}'

Invoke-RestMethod -Method put -Uri $MerakiUrl -Headers $headers -Body $jbody

 

When I run the above script I receive the below.

 

Invoke-RestMethod : The remote server returned an error: (308).
At line:9 char:1
+ Invoke-RestMethod -Method put -Uri "https://dashboard.meraki.com/api/ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

I have tried testing the functionality of the PUT command in POSTMAN and it works flawlessly. I know the documentation says to use https://api.meraki.com/api/v0/ as the base url but when i change the url to that, the script still fails with the same error.

 

I tried capturing the response I get back using the below code:

$resp = try { Invoke-RestMethod -Method put -Uri $MerakiUrl -Headers $headers -Body $jbody } catch { $_.Exception.Response }
$resp

 

Here is the output of the above:

Output.PNG

 

Any insight would great.

1 ACCEPTED SOLUTION

Accepted Solutions
New here

Re: Attempting to Change PSK of SSID with Meraki API via Powershell

 

Hi,

 

Try to change the base url to your tenant URL. You can get this url when you login to https://dashboard.meraki.com. It's like https://nXXX.meraki.com. Powershell doesn't seem to like HTTP 308 permanent redirect.

 

I'm using this code to update the PSK:

 

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11
#Meraki API KEY
$api_key = "API_KEY"
#Meraki Network URL
$network_id = "NETWORK_ID"
#Base API URL
$api = @{
    "endpoint" = 'https://api.meraki.com/api/v0'
}
#API URL for SSID PSK Change XXX
$api_put = @{
    "endpoint" = 'https://nXXX.meraki.com/api/v0'
}

$header_org = @{
    "X-Cisco-Meraki-API-Key" = $api_key
    "Content-Type" = 'application/json'
}
# PSK = New password
$data = @{
    "psk" = 'NewPassword12345'
}
#Convert data to Json format
$jbody = ConvertTo-Json -InputObject $data
#URL Network_ID and SSID number
$api.ssid = "/networks/$network_id/ssids/2"
#Combine base api_put URL and $api.ssid
$Merakiuri = $api_put.endpoint + $api.ssid
Invoke-RestMethod -Method Put -Uri $Merakiuri -Headers $header_org -Body $jbody -Verbose

 

4 REPLIES
New here

Re: Attempting to Change PSK of SSID with Meraki API via Powershell

 

Hi,

 

Try to change the base url to your tenant URL. You can get this url when you login to https://dashboard.meraki.com. It's like https://nXXX.meraki.com. Powershell doesn't seem to like HTTP 308 permanent redirect.

 

I'm using this code to update the PSK:

 

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11
#Meraki API KEY
$api_key = "API_KEY"
#Meraki Network URL
$network_id = "NETWORK_ID"
#Base API URL
$api = @{
    "endpoint" = 'https://api.meraki.com/api/v0'
}
#API URL for SSID PSK Change XXX
$api_put = @{
    "endpoint" = 'https://nXXX.meraki.com/api/v0'
}

$header_org = @{
    "X-Cisco-Meraki-API-Key" = $api_key
    "Content-Type" = 'application/json'
}
# PSK = New password
$data = @{
    "psk" = 'NewPassword12345'
}
#Convert data to Json format
$jbody = ConvertTo-Json -InputObject $data
#URL Network_ID and SSID number
$api.ssid = "/networks/$network_id/ssids/2"
#Combine base api_put URL and $api.ssid
$Merakiuri = $api_put.endpoint + $api.ssid
Invoke-RestMethod -Method Put -Uri $Merakiuri -Headers $header_org -Body $jbody -Verbose

 

Highlighted
Conversationalist

Re: Attempting to Change PSK of SSID with Meraki API via Powershell

Your example worked great! Thank you so much for the information! I was able to use your script and input my specific network information and it worked.

Here to help

Re: Attempting to Change PSK of SSID with Meraki API via Powershell

Just to add if your using powershell you can tackle redirects in the following manner useful if your updating things accross multiple orgs with different shard names

 

$resource = "https://api.meraki.com/api/v0/networks/$networkId/devices/$serial"

 

$redirect = Invoke-WebRequest -Uri $resource -MaximumRedirection 0 -Method PUT -Header $header -Body $body
if ($redirect.StatusCode -eq 308)
{
$resourceRedirect = $redirect.Headers.Location
     Invoke-WebRequest -Uri $resourceRedirect -MaximumRedirection 0 -Method PUT -Header $header -Body $body  
}
}

Conversationalist

Re: Attempting to Change PSK of SSID with Meraki API via Powershell

I'd like to note that this answer also helped with a Google Apps Script project. The base url had to be the tenant URL there, just as in PowerShell. Apps Script has a UrlFetchApp.fetch() method which does have a 'followRedirects' option, but it's already set by default to true and even including it explicitly in the function still didn't allow the PUT to succeed. Only using the tenant url (e.g. NXXX.meraki.com) for a PUT would work.

Note: I have not attempted to parse out the redirect headers in Apps Script to rebuild a new request, as suggested for PowerShell by MarkD_BT below -- I'm happy it's working as is and can move on.

Thanks very much for sharing this tip!