Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active August 29, 2021 12:23
Show Gist options
  • Save quonic/b0a6775f0be69baee9d793e2cac89dfc to your computer and use it in GitHub Desktop.
Save quonic/b0a6775f0be69baee9d793e2cac89dfc to your computer and use it in GitHub Desktop.
An example of AD auth with
Import-Module -Name UniversalDashboard
$ADDomain = "MyDomainHere.consto.com"
$ADGroup = "GroupNameHere"
$PageHome = New-UDPage -Name "Home" -Content {
New-UDLayout -Columns 3 -Content {
New-UDCard -Title "Scheduled" -Content {
"Links"
} -Links @(
New-UDlink -Text "Jobs" -Url "/jobs/"
New-UDlink -Text "Tasks" -Url "/tasks/"
)
}
}
$PageJobList = New-UDPage -Name "Jobs" -Content {
New-UdGrid -Title "Jobs" -Headers @(
"Name", "ID", "Enabled"
) -Properties @(
"Name", "Id", "Enabled"
) -AutoRefresh -RefreshInterval 60 -Endpoint {
Get-ScheduledJob | Select-Object -Property Name, Id, Enabled | Out-UDGridData
}
}
$PageTaskList = New-UDPage -Name "Tasks" -Content {
New-UdGrid -Title "Tasks" -Headers @(
"TaskName", "State", "TaskPath"
) -Properties @(
"TaskName", "State", "TaskPath"
) -AutoRefresh -RefreshInterval 60 -Endpoint {
Get-ScheduledTask -TaskPath "\*" |
Where-Object {$_.TaskPath -notlike "*Microsoft*"} |
Select-Object -Property TaskName, State, TaskPath | Out-UDGridData
}
}
$FormLogin = New-UDAuthenticationMethod -Endpoint {
param([PSCredential]$Credentials)
Function Test-Credential {
[OutputType([Bool])]
Param (
[Parameter(
Mandatory = $true,
ValueFromPipeLine = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias(
'PSCredential'
)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[Parameter()]
[String]
$Domain = $Credential.GetNetworkCredential().Domain
)
Begin {
[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement") |
Out-Null
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext(
[System.DirectoryServices.AccountManagement.ContextType]::Domain, $Domain
)
}
Process {
foreach ($item in $Credential) {
$networkCredential = $Credential.GetNetworkCredential()
Write-Output -InputObject $(
$principalContext.ValidateCredentials(
$networkCredential.UserName, $networkCredential.Password
)
)
}
}
End {
$principalContext.Dispose()
}
}
# if ( ( Test-Credential -Credential $Credentials ) ) {
if ( ( Test-Credential -Credential $Credentials -Domain $ADDomain ) -and ( Get-ADGroupMember -Identity ADGroup | Where-Object {$Credentials.UserName -like $_.SamAccountName} ) ) {
New-UDAuthenticationResult -Success -UserName $Credentials.UserName
}
New-UDAuthenticationResult -ErrorMessage "Invalid Credentials, please try again."
}
$LoginPage = New-UDLoginPage -AuthenticationMethod $FormLogin
$PageArray = @($PageHome, $PageJobs, $PageJobList, $PageTaskList, $PageTasks)
$MyDashboard = New-UDDashboard -Title "Hello, World" -Pages $PageArray -LoginPage $LoginPage
Start-UDDashboard -Port 1000 -Dashboard $MyDashboard -AllowHttpForLogin
# -CertificateFile $MyCertHereForProd # Remove -AllowHttpForLogin in prod
@quonic
Copy link
Author

quonic commented Mar 9, 2021

Updated this to allow specifying a domain and group to restrict to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment