Hi Steve, Below is the powershell script - As it stands, it is hard coded for a single organisation as the base URI contains the Org ID You will just need to populate the necessary areas with your particular details etc. I.E. smpt server and recipient info in function sendMail API key in function Get-WifiSSID base_uri & api_key in #setup some global static stuff section email message body in section # build email message body and send it You can change password length (currently 10 chars) and complexity by changing the code in function createPassword param([string]$site="",[string]$ssid="",[string]$action="")
function sendMail([string]$txtbody)
{
#SMTP server name
$smtpServer = "<InsertYourSMTPMailServerInfoHere>"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "merakiapi@<YourMailDomainHere>"
$msg.To.Add("<RecipientMailAddressHere>")
#$msg.To.Add("<OtherRecipientAddressHere")
$msg.subject = "New password for guest Wifi"
$msg.body = $txtbody
$msg.IsBodyHTML=$true
#Sending email
$smtp.Send($msg)
}
function Get-RandomCharacters($length, $characters)
{
$random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
$private:ofs=""
return [String]$characters[$random]
}
function Scramble-String([string]$inputString)
{
$characterArray = $inputString.ToCharArray()
$scrambledStringArray = $characterArray | Get-Random -Count $characterArray.Length
$outputString = -join $scrambledStringArray
return $outputString
}
function updateWiFiPSK ([string]$s_id, [string]$w_id, [string]$newpassword)
{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# PSK = New password
$data = @{
"psk" = $newpassword
}
#Convert data to Json format
$jbody = ConvertTo-Json -InputObject $data
#Combine base URL and ssid
$request_uri = $base_uri + $networks_uri + $s_id + "/ssids/" + $w_id
$r = Invoke-WebRequest $request_uri -Method:Put -Headers $header_org -Body $jbody
return $r
}
function Get-SiteID
{
#get site id
$request_uri = $base_uri + $networks_uri
$r = Invoke-WebRequest $request_uri -Method:Get -Headers $header_org
$json = $r | ConvertFrom-Json
for($i=0;$i -lt $json.count;$i++)
{
if ($site -eq $json[$i].name)
{
Write-host "Network Name : " $json[$i].name
Write-host "id : " $json[$i].id
Write-host "Type : " $json[$i].type
$s_id = $json[$i].id
}
}
return $s_id
}
function Get-WiFiSSID ([string]$s_id)
{
#get wifi network ID from site requested
if ($s_id -ne "")
{
$request_uri = $base_uri + $networks_uri + $s_id + "/ssids/"
$r = Invoke-WebRequest $request_uri -Method:Get -Headers @{"X-Cisco-Meraki-API-Key"="<ReplaceWithYouAPIKeyHere>"} -ContentType "application/json"
$z = $r | ConvertFrom-Json
for($i=0;$i -lt $z.count;$i++)
{
If ($z[$i].name -eq $ssid)
{
Write-host "SSID Name : " $z[$i].name
Write-host "SSID# : " $z[$i].number
Write-host "Current PSK : " $z[$i].psk
$w_id = $z[$i].number
}
}
}
return $w_id
}
function createPassword
{
$password = Get-RandomCharacters -length 4 -characters 'abcdefghiklmnoprstuvwxyz'
$password += Get-RandomCharacters -length 2 -characters 'ABCDEFGHKLMNOPRSTUVWXYZ'
$password += Get-RandomCharacters -length 2 -characters '1234567890'
$password += Get-RandomCharacters -length 2 -characters '!$%&()?}][{@#+'
#Write-Host $password
return Scramble-String $password
}
# setup some global static stuff
$base_uri = "https://<InsertYourMerakiAPIBaseURLHere>"
## E.G. n34.meraki.com/api/v0/organizations/<OrgID>/
$networks_uri = "networks/"
#Meraki API KEY
$api_key = "<InsertYourAPIKeyHere>"
$header_org = @{"X-Cisco-Meraki-API-KEY" = $api_key;"Content-Type" = 'application/json'}
$s_id = ""
$w_id = ""
$mode = ""
If ($site -eq "" -or $ssid -eq "")
{
Write-Host "MerakiPSKTool - (c) 2019"
Write-Host ""
Write-Host "Site/SSID parameter is missing"
Write-Host "Usage: MerakiPSKTool.ps1 -site <sitename> -ssid <ssidName> -action [Change | Display]"
Write-Host ""
exit
}
# if action not passed or is blank, set default mode to Display
if ($action -eq "")
{
$action = "Display"
}
switch ($action)
{
{@("Display", "display") -contains $_ }
{
"Displaying Wifi details"
$mode = "display"
}
{@("Change", "change") -contains $_ }
{
"Change Wifi password"
$mode = "change"
}
default { "MerakiPSKTool.ps1" }
}
# get ID of the site passed in params (set a default value if no site passed)
$s_id = Get-SiteID
If ($s_id -ne "")
{
# get id of Wifi network that password is to be changed for
$w_id = Get-WifiSSID($s_id)
#Write-Host "Site ID : " $s_id " | Wifi #: " $w_id
}
if ($mode -eq "change")
{
# generate a new complex password
$newpassword = createPassword
$result = updateWiFiPSK $s_id $w_id $newpassword
#Write-Host $result
# build email message body and send it
if ($result.StatusCode -eq 200)
{
Write-Host "Sending Email"
$txtbody = "<html><body>"
$txtbody = $txtbody + "The new password for " + $ssid + " at " + $site + " is<br><br><b><font size=30 color=green>"
$txtbody = $txtbody + $newpassword + "</font></b>"
$txtbody = $txtbody + "<br><br>If you have any problems please contact <SomeContactDetailsHere>"
$txtbody = $txtbody + "<br><br>regards<br>SignoffInfo"
$txtbody = $txtbody + "</body></html>"
#Write-Host $txtbody
# send the email
sendMail $txtbody
}
else
{
Write-Host "Password change failed for " + $ssid + " at " + $site
}
} Copy and paste the above into text editor, make the necessary changes and save as MerakiPSKTool.ps1 to display current PSK for site, use MerakiPSKTool.ps1 -site <sitename> -ssid <SSIDName> -action Display to change psk for site use MerakiPSKTool.ps1 -site <sitename> -ssid <SSIDName> -action Change if no Action value provided it will default to Display Current email body looks like this: The new password for [ssidName] at [SiteName] is z$ys$63mPX If you have any problems please contact [ContactName] on [ContactTel] or [ContactEmail] regards [Contact] Subject line is New password for guest Wifi I'm not a pro programmer so use at own risk, i've had it running now for a good couple or months or so now and it's worked fine since. rgds Gary
... View more