Skip to content

Commit

Permalink
Add test-cred, fix join-object bug
Browse files Browse the repository at this point in the history
handle funky props in join-object
  • Loading branch information
RamblingCookieMonster committed Oct 6, 2015
1 parent e4c4183 commit 808a937
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Join-Object.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
Write-Verbose "Transforming property $RightProp to $Prefix$RightProp$Suffix"
@{
Name="$Prefix$RightProp$Suffix"
Expression=[scriptblock]::create("param(`$_) `$_.$RightProp")
Expression=[scriptblock]::create("param(`$_) `$_.'$RightProp'")
}
$AllProps += "$Prefix$RightProp$Suffix"
}
Expand Down
96 changes: 96 additions & 0 deletions Test-Credential.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
function Test-Credential {
<#
.SYNOPSIS
Takes a PSCredential object and validates it
.DESCRIPTION
Takes a PSCredential object and validates it against a domain or local machine
Borrows from a variety of sources online, don't recall which - apologies!
.PARAMETER Credential
A PScredential object with the username/password you wish to test. Typically this is generated using the Get-Credential cmdlet. Accepts pipeline input.
.PARAMETER Context
An optional parameter specifying what type of credential this is. Possible values are 'Domain','Machine',and 'ApplicationDirectory.' The default is 'Domain.'
.PARAMETER ComputerName
If Context is machine, test local credential against this computer.
.PARAMETER Domain
If context is domain (default), test local credential against this domain. Default is current user's
.OUTPUTS
A boolean, indicating whether the credentials were successfully validated.
.EXAMPLE
#I provide my AD account credentials
$cred = get-credential
#Test credential for an active directory account
Test-Credential $cred
.EXAMPLE
#I provide local credentials here
$cred = get-credential
#Test credential for a local account
Test-Credential -ComputerName SomeComputer -Credential $cred
.FUNCTIONALITY
Active Directory
#>
[cmdletbinding(DefaultParameterSetName = 'Domain')]
param(
[parameter(ValueFromPipeline=$true)]
[System.Management.Automation.PSCredential]$Credential = $( Get-Credential -Message "Please provide credentials to test" ),

[validateset('Domain','Machine', 'ApplicationDirectory')]
[string]$Context = 'Domain',

[parameter(ParameterSetName = 'Machine')]
[string]$ComputerName,

[parameter(ParameterSetName = 'Domain')]
[string]$Domain = $null
)
Begin
{
Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)`nPSBoundParameters: $($PSBoundParameters | Out-String)"
Try
{
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
}
Catch
{
Throw "Could not load assembly: $_"
}

#create principal context with appropriate context from param. If either comp or domain is null, thread's user's domain or local machine are used
if ($PSCmdlet.ParameterSetName -eq 'Domain')
{
$Context = $PSCmdlet.ParameterSetName
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$Context, $Domain)
}
elseif ($PSCmdlet.ParameterSetName -eq 'Machine')
{
$Context = $PSCmdlet.ParameterSetName
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$Context, $ComputerName)
}
elseif ($Context -eq 'ApplicationDirectory' )
{
#Name=$null works for machine/domain, not applicationdirectory
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$Context)
}
}
Process
{
#Validate provided credential
$DS.ValidateCredentials($Credential.UserName, $Credential.GetNetworkCredential().password)
}
End
{
$DS.Dispose()
}
}

0 comments on commit 808a937

Please sign in to comment.