Skip to content

Commit 808a937

Browse files
Add test-cred, fix join-object bug
handle funky props in join-object
1 parent e4c4183 commit 808a937

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

Join-Object.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@
429429
Write-Verbose "Transforming property $RightProp to $Prefix$RightProp$Suffix"
430430
@{
431431
Name="$Prefix$RightProp$Suffix"
432-
Expression=[scriptblock]::create("param(`$_) `$_.$RightProp")
432+
Expression=[scriptblock]::create("param(`$_) `$_.'$RightProp'")
433433
}
434434
$AllProps += "$Prefix$RightProp$Suffix"
435435
}

Test-Credential.ps1

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
function Test-Credential {
2+
<#
3+
.SYNOPSIS
4+
Takes a PSCredential object and validates it
5+
6+
.DESCRIPTION
7+
Takes a PSCredential object and validates it against a domain or local machine
8+
9+
Borrows from a variety of sources online, don't recall which - apologies!
10+
11+
.PARAMETER Credential
12+
A PScredential object with the username/password you wish to test. Typically this is generated using the Get-Credential cmdlet. Accepts pipeline input.
13+
14+
.PARAMETER Context
15+
An optional parameter specifying what type of credential this is. Possible values are 'Domain','Machine',and 'ApplicationDirectory.' The default is 'Domain.'
16+
17+
.PARAMETER ComputerName
18+
If Context is machine, test local credential against this computer.
19+
20+
.PARAMETER Domain
21+
If context is domain (default), test local credential against this domain. Default is current user's
22+
23+
.OUTPUTS
24+
A boolean, indicating whether the credentials were successfully validated.
25+
26+
.EXAMPLE
27+
#I provide my AD account credentials
28+
$cred = get-credential
29+
30+
#Test credential for an active directory account
31+
Test-Credential $cred
32+
33+
.EXAMPLE
34+
#I provide local credentials here
35+
$cred = get-credential
36+
37+
#Test credential for a local account
38+
Test-Credential -ComputerName SomeComputer -Credential $cred
39+
40+
.FUNCTIONALITY
41+
Active Directory
42+
43+
#>
44+
[cmdletbinding(DefaultParameterSetName = 'Domain')]
45+
param(
46+
[parameter(ValueFromPipeline=$true)]
47+
[System.Management.Automation.PSCredential]$Credential = $( Get-Credential -Message "Please provide credentials to test" ),
48+
49+
[validateset('Domain','Machine', 'ApplicationDirectory')]
50+
[string]$Context = 'Domain',
51+
52+
[parameter(ParameterSetName = 'Machine')]
53+
[string]$ComputerName,
54+
55+
[parameter(ParameterSetName = 'Domain')]
56+
[string]$Domain = $null
57+
)
58+
Begin
59+
{
60+
Write-Verbose "ParameterSetName: $($PSCmdlet.ParameterSetName)`nPSBoundParameters: $($PSBoundParameters | Out-String)"
61+
Try
62+
{
63+
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
64+
}
65+
Catch
66+
{
67+
Throw "Could not load assembly: $_"
68+
}
69+
70+
#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
71+
if ($PSCmdlet.ParameterSetName -eq 'Domain')
72+
{
73+
$Context = $PSCmdlet.ParameterSetName
74+
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$Context, $Domain)
75+
}
76+
elseif ($PSCmdlet.ParameterSetName -eq 'Machine')
77+
{
78+
$Context = $PSCmdlet.ParameterSetName
79+
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$Context, $ComputerName)
80+
}
81+
elseif ($Context -eq 'ApplicationDirectory' )
82+
{
83+
#Name=$null works for machine/domain, not applicationdirectory
84+
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$Context)
85+
}
86+
}
87+
Process
88+
{
89+
#Validate provided credential
90+
$DS.ValidateCredentials($Credential.UserName, $Credential.GetNetworkCredential().password)
91+
}
92+
End
93+
{
94+
$DS.Dispose()
95+
}
96+
}

0 commit comments

Comments
 (0)