Meraki API & Powershell Script issues

Solved
CWS184
Conversationalist

Meraki API & Powershell Script issues

Hi All,

 

I'm still very new to THE Meraki API, My predecessor had a script running on our server to automatically change our Guest WiFi password on a weekly basis. Recently the script has stopped working and when i look at the logs the API is returning error 400 Bad Request. Which would be fine, except when i run the script in Powershell ISE is runs flawlessly.

 

What am i missing? I've included the script below, if anyone can help? I'm having to change it manually at the moment which is annoying and i keep forgetting to do it.

 

#Meraki API Params
[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/v1'
}
#API URL for SSID PSK Change XXX
$api_put = @{
    "endpoint" = 'https://api.meraki.com/api/v1'
}

$header_org = @{
    "X-Cisco-Meraki-API-Key" = $api_key
    "Content-Type" = 'application/json'
}
# PSK = New password
$data = @{
    "psk" = $newpsk
}
$currentdate = get-date
$expdate = (get-date).adddays(7)
#Convert data to Json format
$jbody = ConvertTo-Json -InputObject $data
#URL Network_ID and SSID number
$api.ssid = "/networks/$network_id/wireless/ssids/12"
#Combine base api_put URL and $api.ssid
$Merakiuri = $api_put.endpoint + $api.ssid
$changepsk = Invoke-RestMethod -Method Put -Uri $Merakiuri -Headers $header_org -Body $jbody -Verbose | Out-File -FilePath "\\SRV4\IT\Meraki\GuestWiFi.txt"

Add-Type -AssemblyName 'System.Web'
function New-RandomPassword {
    param(
        [Parameter()]
        [int]$MinimumPasswordLength = 12,
        [Parameter()]
        [int]$MaximumPasswordLength = 15,
        [Parameter()]
        [int]$NumberOfAlphaNumericCharacters = 2,
        [Parameter()]
        [switch]$ConvertToSecureString
    )
    
    Add-Type -AssemblyName 'System.Web'
    $length = Get-Random -Minimum $MinimumPasswordLength -Maximum $MaximumPasswordLength
    $password = [System.Web.Security.Membership]::GeneratePassword($length,$NumberOfAlphaNumericCharacters)
    if ($ConvertToSecureString.IsPresent) {
        ConvertTo-SecureString -String $password -AsPlainText -Force
    } else {
        $password
    }
}
#Generate the new PSK
$newpsk = New-RandomPassword -MinimumPasswordLength 12 -MaximumPasswordLength 15 -NumberOfAlphaNumericCharacters 0
#Build and send the email containing the new password
$email = @{
From = "Guest WiFi <GuestWiFi@company.com>"
To = "Company Reception <Reception@company.com>"
Cc = "Company IT <IT-Team@company.com>"
Bcc = "Person 1 <Person1@company.com>; Person2 <Person2@company.com"
Subject = "The Password for Company Guest has been changed"
SMTPServer = "Office 365 MX Endpoint"
Attachments = "\\SRV4\IT\Meraki\GuestWiFi.txt","\\SRV4\IT\Meraki\GuestWiFiTandCs.pdf"
Body = " Hello,<br><br>The password for Company Guest has been changed.<br><br>Please see the attached Text File to obtain the new password.<br><br>It will appear in the collumn 'psk' in the attached file.<br><br>Please remember that this password should only be given out to Visitors of Company and Occupiers of the 4th Floor.<br><br>Please disconnect and reconnect any devices to the network using the new password above.<br><br>If you experienmce any issues, please contact the IT Team on mailto:it-team@company.com<br><br><b>This password will be changed on a weekly basis and will expire on Monday $expdate </b>"
}
send-mailmessage @email -BodyAsHtml
Write-Host "Script Executed Successfully"
1 Accepted Solution
GreenMan
Meraki Employee
Meraki Employee

I'm hopeless with scripts, but have just started playing with chatGPT.   This is what it reckons - interested to hear what folk make of it (it did also include some alternative code):

 

The error 400 indicates a bad request, which means there is an issue with the request you are sending to the Meraki Dashboard API. Looking at your code, there are a few potential problems:

  1. The variable $newpsk is being used before it is defined. You are generating the new password after assigning it to the $data variable. Move the line where you generate the password ($newpsk = New-RandomPassword) above the $data assignment.

  2. The $jbody variable is assigned before the $newpsk is generated. Since you are including the password in the request body, you need to generate the password first and then convert the updated $data to JSON. Move the line $jbody = ConvertTo-Json -InputObject $data below the $newpsk generation.

View solution in original post

3 Replies 3
GreenMan
Meraki Employee
Meraki Employee

I'm hopeless with scripts, but have just started playing with chatGPT.   This is what it reckons - interested to hear what folk make of it (it did also include some alternative code):

 

The error 400 indicates a bad request, which means there is an issue with the request you are sending to the Meraki Dashboard API. Looking at your code, there are a few potential problems:

  1. The variable $newpsk is being used before it is defined. You are generating the new password after assigning it to the $data variable. Move the line where you generate the password ($newpsk = New-RandomPassword) above the $data assignment.

  2. The $jbody variable is assigned before the $newpsk is generated. Since you are including the password in the request body, you need to generate the password first and then convert the updated $data to JSON. Move the line $jbody = ConvertTo-Json -InputObject $data below the $newpsk generation.

CWS184
Conversationalist

That seems to have sorted it. Script seems to rub in powershell just fine now. Thank you!

PhilipDAth
Kind of a big deal
Kind of a big deal

Find which account the script is being run under (such as SYSTEM, Administrator, etc).  Either log in as that user or use psexec and run that script as that user and see what other errors pop up.

 

It must be to do with the account that the script is being run under.

Get notified when there are additional replies to this discussion.