For anyone still looking for help on this - I have mashed together the solutions provided by nealgs and by werowance. Sanitised version below. This will scramble the password and send an email as nealgs's did previously. param([string]$site="",[string]$ssid="",[string]$action="")
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11
#Email settings. Copied from old v0 script.
function sendMail([string]$txtbody)
{
#SMTP server name
$smtpServer = "add your o365 mail server (domainame.mail.protection.outlook.com)"
#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 = "enter your FROM address"
$msg.Bcc.Add("enter your BCC address")
$msg.To.Add("enter your TO address")
$msg.subject = "enter your SUBJECT"
$msg.body = $txtbody
$msg.IsBodyHTML=$true
#Sending email
$smtp.Send($msg)
}
#Create a random password for the SSID. Copied from old v0 script.
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_uri2 + $networks_uri + $s_id + "/wireless/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)
{
#Copied the next 3 lines from old v0 script
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_uri2 + $networks_uri + $s_id + "/wireless/ssids/"
$r = Invoke-WebRequest $request_uri -Method:Get -Headers @{"X-Cisco-Meraki-API-Key"="ADD YOUR API KEY HERE"} -ContentType "application/json"
$z = $r | ConvertFrom-Json
for($i=0;$i -lt $z.count;$i++)
{
If ($z[$i].name -eq $ssid)
{
#Copied the next 3 lines from old v0 script
Write-host "SSID Name : " $z[$i].name
Write-host "SSID# : " $z[$i].number
Write-host "Current PSK : " $z[$i].psk
#write-output $z[$i].psk > c:\trash.txt
$w_id = $z[$i].number
}
}
}
return $w_id
}
#Copied from old v0 script
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 = "ADD URI HERE"
## E.G. n345.meraki.com/api/v1/organizations/xxxxxx/
$base_uri2 = "ADD URI2 HERE"
## E.G. https://n345.meraki.com/api/v1//
$networks_uri = "networks/"
#Meraki API KEY
$api_key = "ADD API KEY HERE (This is the second time it needs to be added)"
$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 $_ }
{
$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
#Get the current date and save it as a variable
$curDate = get-date -format "dddd dd/MM/yyyy"
# build email message body and send it
if ($result.StatusCode -eq 200)
{
Write-Host "Sending Email"
$txtbody = "<html><body>"
$txtbody = $txtbody + "The new daily password for " + $ssid + " at " + $site + " is:<br><br><b><font size=30 color=green>"
$txtbody = $txtbody + $newpassword + "</font></b>"
$txtbody = $txtbody + "<br><br>Here is some body text that you can change if you want."
$txtbody = $txtbody + "<br><br>Today's date is " + $curDate + "."
$txtbody = $txtbody + "<br><br>This is another line of body text."
$txtbody = $txtbody + "</body></html>"
#Write-Host $txtbody
# send the email
sendMail $txtbody
}
else
{
Write-Host "Password change failed for " + $ssid + " at " + $site
}
}
... View more