param([string]$site=" changed according to us ",[string]$ssid="changed according to us",[string]$action="Display") function sendMail([string]$txtbody) { #SMTP server name $smtpServer = " changed according to us " #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@ changed according to us " $msg.To.Add("changed according to us ") $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"=" changed according to us "} -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://nXX.meraki.com/api/v0/organizations/ changed according to us/ " ## E.G. n34.meraki.com/api/v0/organizations/<OrgID>/ $networks_uri = "networks/" #Meraki API KEY $api_key = " changed according to us " $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 changed according to us -ssid changed according to us -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 } }
... View more