I was asked to create 365 (1 year) of WiFi (daily) guest access accounts, and export them to CSV.
Of course I didn’t want to create them by hand.. this is where PowerShell came in. I used the following PowerShell script to create random WiFi guest accounts:
# Number of days to generate, default = 365 $NumDays = 365 $AccessGroup = "Gasten" $ExpiryTime = "20:00" $StartTime = "07:00" $OutputCSV = "C:\TEMP\wireless.csv" $SshHost = "127.0.0.1" $SshUser = "someuser" $access_codes = @() # Start SSH session New-SshSession $SshUser $SshHost Invoke-Ssh "z" Invoke-Ssh "conf t" Invoke-Ssh "wireless F" Invoke-Ssh "conf t" Invoke-Ssh "radius-server local" $i = 0; do { # WSEM format: mm/dd/yyyy hh:mm $date = (Get-Date).AddDays($i) $date2 = Get-Date $date -Format "dd-MM-yyyy" $date = Get-Date $date -format "MM:dd:yyyy" $pass = RandomPassword 4 $user = RandomPassword 4 $output = "rad-user $user password 0 $pass group $AccessGroup guest expiry-time $ExpiryTime expiry-date $date start-time $StartTime start-date $date" # add user through SSH Invoke-ssh $output $Response = New-Object PSObject Add-Member -InputObject $Response -MemberType NoteProperty -Name "Datum" -Value $date2 Add-Member -InputObject $Response -MemberType NoteProperty -Name "Gebruikersnaam" -Value $user Add-Member -InputObject $Response -MemberType NoteProperty -Name "Wachtwoord" -Value $pass $access_codes += $Response $i++; } while ( $i -le $NumDays ) # Save and disconnect SSH Invoke-ssh "write mem" Remove-SshSession # Export to CSV $access_codes | Export-Csv $OutputCSV # Helper functions function RandomPassword ([int]$intPasswordLength) { $strNumbers = "1234567890" $strCapitalLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" $strLowerLetters = "abcdefghijklmnopqrstuvwxyz" $rand = new-object random for ($a=1; $a -le $intPasswordLength; $a++) { if ($a -gt 3) { $b = $rand.next(0,3) + $a $b = $b % 3 + 1 } else { $b = $a } switch ($b) { "1" {$b = "$strNumbers"} "2" {$b = "$strCapitalLetters"} "3" {$b = "$strLowerLetters"} } $charset = $($b) $number = $rand.next(0,$charset.Length) $RandomPassword += $charset[$number] } return $RandomPassword }
Special thanks to Joel Bennett’s SharpSSH wrapper for PowerShell (located here: http://huddledmasses.org/scriptable-ssh-from-powershell/) which I used to automate the command line commands for the HP WSEM.
This shows the great power of PowerShell for automating virtually anything!