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"