Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jbilinski/ef5234fdba85289a68f32a5ecaafc4d0 to your computer and use it in GitHub Desktop.
Save jbilinski/ef5234fdba85289a68f32a5ecaafc4d0 to your computer and use it in GitHub Desktop.
bl-new-daily-password-and-a-cisco-guest-wireless-portal
#requires -Version 3
function Check-GuestEnvironment
{
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)][string]$kiwiServer,
[Parameter(Mandatory = $true)][string]$webServer,
[Parameter(Mandatory = $true)][string]$webURL
)
$kiwiService = Get-Service -ComputerName $kiwiServer -Name CatTools
$kiwiService.ToString() | Update-GuestWirelessTaskLog -EventID 8056
If ($($kiwiService.Status) -ne 'Running')
{
Write-Verbose -Message "ERROR: $($kiwiService.displayname) not detected running"
Update-GuestWirelessTaskLog -EventID 8056 -LogMessage "WARNING! $($kiwiService.displayname) not detected running on $kiwiServer ."
}
else{Update-GuestWirelessTaskLog -EventID 8056 -LogMessage "Verified $($kiwiService.displayname) is $($kiwiService.Status) "}
$httpService = Get-Service -ComputerName $webServer -Name W3SVC
$httpService.ToString() | Update-GuestWirelessTaskLog -EventID 8057
If ($($httpService.Status) -ne 'Running')
{
Write-Verbose -Message "ERROR: $($httpService.displayname) Service not detected running"
Update-GuestWirelessTaskLog -EventID 8057 -LogMessage "WARNING! $($httpService.displayname) not detected running on $webServer ."
}
else{Update-GuestWirelessTaskLog -EventID 8057 -LogMessage "Verified $($httpService.displayname) is $($httpService.Status) "}
$httpContent = Invoke-WebRequest -Uri $webURL
if ($($httpContent.StatusCode) -ne '200')
{
Write-Verbose -Message "ERROR: $webURL returned unexpected StatusCode $($httpContent.StatusCode)"
Update-GuestWirelessTaskLog -EventID 8057 -LogMessage "WARNING! $webURL returned unexpected StatusCode $($httpContent.StatusCode) with content $($httpContent.RawContent) "
}
else{Update-GuestWirelessTaskLog -EventID 8057 -LogMessage "Website $($httpContent.StatusDescription). $webURL returned StatusCode $($httpContent.StatusCode)"}
}
function Create-GuestPassKey
{
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)][string]$keyWordsCSVpath
)
$keyWords = Import-Csv -Path $keyWordsCSVpath
Set-Variable -Scope 1 -Name passkey -Value "$(Get-Random -InputObject $($keyWords.KeyA) -Count 1)-$(Get-Random -InputObject $($keyWords.KeyB) -Count 1)"
[string]$passkey = "$(([string]$passkey[0]).ToUpper())$($passkey.Substring(1))" #add upper for mixed-case requirement
Return $passkey
}
function New-GuestKeyConfig
{
Param(
[Parameter(Mandatory = $true)][string]$GuestPassword,
[Parameter(Mandatory = $true)][string]$GuestUsername,
[Parameter(Mandatory = $false)][string]$configstring = 'config netuser password $GUESTUSERNAME $GUESTPASSWORD', #single quotes to prevent var exp
[Parameter(Mandatory = $true)][string]$configTXTpath
)
$configstring = $configstring.Replace('$GUESTUSERNAME', "$GuestUsername")
$configstring = $configstring.Replace('$GUESTPASSWORD', "$GuestPassword")
Update-GuestWirelessTaskLog -EventID 8054 -LogMessage "config=$configstring"
$configstring | Out-File -FilePath $configTXTpath -Encoding ascii -Force
}
#optional requirement - verify-guestkeyconfig
function New-GuestKeyWebpage
{
Param(
[Parameter(Mandatory = $true)][string]$GuestPassword,
[Parameter(Mandatory = $true)][string]$WebPagepath
)
[string]$title = 'Contoso Guest WiFi Credentials'
$cssuri = 'http://guestwireless.contoso.com/cotosostyle.css'
$body = "<H1>Contoso Guest Wireless Credentials</H1><div>Username: Guest<br />Password: <span>$($GuestPassword)</span></div>"
$postcontent = "<p>Updated:$(Get-Date)</p>"
Update-GuestWirelessTaskLog -EventID 8055 -LogMessage "Saving http to path $WebPagepath"
Update-GuestWirelessTaskLog -EventID 8055 -LogMessage "Saving http content $body"
ConvertTo-Html -Title $title -CssUri $cssuri -Body $body -PostContent $postcontent | Out-File -Encoding utf8 -FilePath $WebPagepath -Force
#return $body
}
function Verify-WirelessKeyWebpage
{
Param(
[Parameter(Mandatory = $true,ValueFromPipeline = $true)][string]$matchBodyHTML,
[Parameter(Mandatory = $true)][string]$WirelessKeyURL
)
Update-GuestWirelessTaskLog -EventID 8058 -LogMessage "Verifying Web Page at $WirelessKeyURL returns $matchBodyHTML"
[string]$guestPageBody = (Invoke-WebRequest -Uri $WirelessKeyURL).content -replace '[^a-zA-Z0-9-\<\>\s\.]', ''
if ($guestPageBody -notlike "*$($matchBodyHTML)*")
{
Write-Verbose -Message "ERROR: Updated HTTP content not found at $WirelessKeyURL"
Update-GuestWirelessTaskLog -EventID 8058 "WARNING! Updated HTTP content not found at $WirelessKeyURL . $matchBodyHTML --NOT IN-- $guestPageBody"
}
else{Update-GuestWirelessTaskLog -EventID 8058 -LogMessage "PASS. Verified $WirelessKeyURL is updated ." }
}
function Update-GuestWirelessTaskLog
{
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)][int]$EventID = 8051,
[Parameter(Mandatory = $true,ValueFromPipeline = $true)][string]$LogMessage = 'Undefined Log Entry'
)
try{New-EventLog -LogName JBScriptingLog -Source PSScript -ComputerName . -ErrorAction Stop}
catch{Write-EventLog -LogName JBScriptingLog -Source PSScript -EventId $EventID -Message "GuestWifiPW: $($LogMessage)" -EntryType Information}
}
function Start-DailyGuestWirelessTasks
{
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)][string]$kiwiServer = 'netutil5.contoso.com',
[Parameter(Mandatory = $false)][string]$webServer = '.',
[Parameter(Mandatory = $false)][string]$webURL = 'http://guestwifi.contoso.com',
[Parameter(Mandatory = $false)][string]$WebPagepath = 'C:\inetpub\wwwroot\guest\default.htm',
[Parameter(Mandatory = $false)][string]$KeyWordsCSV = 'VAR:\IndustryKeywords.csv',
[Parameter(Mandatory = $false)][string]$configTXTpath = 'OUT:\Kiwi\guestwifi\ConfigGuestPassword.txt',
[Parameter(Mandatory = $false)][string]$VARpath = '\\devutility2.contoso.com\svn\var\',
[Parameter(Mandatory = $false)][string]$OUTpath = '\\devutility2.contoso.com\svn\out\',
[Parameter(Mandatory = $false)][string]$GuestUsername = 'Guest',
[Parameter(Mandatory = $false)][switch]$setEnvironment
)
Update-GuestWirelessTaskLog -EventID 8052 -LogMessage "Starting Daily Task ($kiwiServer,$webServer,$webURL,$WebPagepath,$KeyWordsCSV,$configTXTpath,$VARpath,$OUTpath,$setEnvironment)"
if ($setEnvironment)
{
$null = New-PSDrive -Name VAR -PSProvider FileSystem -Root $VARpath
$null = New-PSDrive -Name OUT -PSProvider FileSystem -Root $OUTpath
}
Check-GuestEnvironment -kiwiServer $kiwiServer -webServer $webServer -webURL $webURL
$DailyPassKey = Create-GuestPassKey -keyWordsCSVpath $KeyWordsCSV
Update-GuestWirelessTaskLog -EventID 8052 -LogMessage "Using new guest PassKey $DailyPassKey"
New-GuestKeyConfig -GuestUsername $GuestUsername -GuestPassword $DailyPassKey -configTXTpath $configTXTpath
New-GuestKeyWebpage -GuestPassword $DailyPassKey -WebPagepath $WebPagepath
Verify-WirelessKeyWebpage -matchBodyHTML $DailyPassKey -WirelessKeyURL $webURL
}
Start-DailyGuestWirelessTasks -setEnvironment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment