-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSet-OneShellUserProfileCredential.ps1
102 lines (99 loc) · 4.47 KB
/
Set-OneShellUserProfileCredential.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Function Set-OneShellUserProfileCredential
{
[cmdletbinding(DefaultParameterSetName = 'Select')]
param
(
[parameter(Position = 1)]
[ValidateNotNullOrEmpty()]
[string]$ProfileIdentity
,
[parameter(ParameterSetName = 'Identity', Position = 2)]
[ValidateNotNullOrEmpty()]
[string]$Identity
,
[parameter(ParameterSetName = 'UserName', Position = 2)]
[ValidateNotNullOrEmpty()]
[string]$Username
,
[parameter(Position = 3)]
[ValidateNotNullOrEmpty()]
[string]$NewUsername
,
[parameter(Position = 4)]
[ValidateNotNullOrEmpty()]
[securestring]$NewPassword
,
[parameter()]
[ValidateScript( {Test-DirectoryPath -Path $_})]
[string[]]$Path = $Script:OneShellUserProfilePath
)#end param
End
{
#Get/Select the Profile
if ($null -eq $Path -or [string]::IsNullOrEmpty($Path)) {$path = $Script:OneShellUserProfilePath}
$puProfiles = GetPotentialUserProfiles -path $Path
$UserProfile = GetSelectProfile -ProfileType User -Path $path -PotentialProfiles $puProfiles -Identity $ProfileIdentity -Operation Edit
if ($UserProfile.Credentials.Count -eq 0) {throw('There are no credentials to set')}
$SelectedCredential = @(
switch ($PSCmdlet.ParameterSetName)
{
'Select'
{
Select-OneShellUserProfileCredential -Credential $UserProfile.Credentials -Operation Edit
}
'Identity'
{
$UserProfile.Credentials | Where-Object -FilterScript {$_.Identity -eq $Identity}
$UserProfile.Credentials | Where-Object -FilterScript {$_.Username -eq $Identity}
}
'Username'
{
$UserProfile.Credentials | Where-Object -FilterScript {$_.Username -eq $UserName}
}
}
)
switch ($SelectedCredential.Count)
{
0 {throw("Matching credential for value $($Identity;$UserName) not found")}
1 {}
default {throw("Multiple credentials with value $($Identity;$UserName) found.")}
}
$EditedCredential = $(
switch ($SelectedCredential)
{
#Both Username and Password Specified - Update Both
{$PSBoundParameters.ContainsKey('NewUsername') -and $PSBoundParameters.ContainsKey('NewPassword')}
{
New-Object System.Management.Automation.PSCredential ($NewUsername, $NewPassword)
}
#Only Username Specified - Update Username, Preserve Password
{$PSBoundParameters.ContainsKey('NewUsername') -and -not $PSBoundParameters.ContainsKey('NewPassword')}
{
New-Object System.Management.Automation.PSCredential ($NewUsername, $($SelectedCredential.Password | ConvertTo-SecureString))
}
#Only Password Specified - Update Password, Preserve Username
{-not $PSBoundParameters.ContainsKey('NewUsername') -and $PSBoundParameters.ContainsKey('NewPassword')}
{
New-Object System.Management.Automation.PSCredential ($SelectedCredential.Username, $NewPassword)
}
#nothing Specified except Identity - suggest preserving username, prompt to update password
{-not $PSBoundParameters.ContainsKey('NewUsername') -and -not $PSBoundParameters.ContainsKey('NewPassword')}
{
$host.ui.PromptForCredential('Set Credential', 'Specify the Password for the credential', $SelectedCredential.Username, '')
}
}
)
if ($null -ne $EditedCredential)
{
$UserProfileCredential = Convert-CredentialToUserProfileCredential -credential $EditedCredential -Identity $SelectedCredential.Identity
$Index = Get-ArrayIndexForValue -array $UserProfile.Credentials -value $SelectedCredential.Identity -property Identity -ErrorAction Stop
$UserProfile.Credentials[$Index] = $UserProfileCredential
$exportUserProfileParams = @{
profile = $UserProfile
path = $Path
ErrorAction = 'Stop'
}
Export-OneShellUserProfile @exportUserProfileParams
}
}
}