-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNew-OneShellUserProfileCredential.ps1
66 lines (63 loc) · 2.26 KB
/
New-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
Function New-OneShellUserProfileCredential
{
[cmdletbinding()]
param
(
[parameter(Position = 1)]
[ValidateNotNullOrEmpty()]
[string]$ProfileIdentity
,
[Parameter(Position = 2)]
[ValidateNotNullOrEmpty()]
[string]$Username
,
[parameter(Position = 3)]
[ValidateNotNullOrEmpty()]
[securestring]$Password
,
[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
$NewCredential = $(
switch ($PSBoundParameters.ContainsKey('Username'))
{
$true
{
switch ($PSBoundParameters.ContainsKey('Password'))
{
$true
{
New-Object System.Management.Automation.PSCredential ($Username, $Password)
}
$false
{
$host.ui.PromptForCredential('New Credential', 'Specify the Password for the credential', $Username, '')
}
}
}
$false
{
$host.ui.PromptForCredential('New Credential', 'Specify the Username and Password for the credential', '', '')
}
}
)
if ($NewCredential -is [PSCredential])
{
$UserProfileCredential = Convert-CredentialToUserProfileCredential -credential $NewCredential
$UserProfile.Credentials += $UserProfileCredential
$exportUserProfileParams = @{
profile = $UserProfile
path = $Path
ErrorAction = 'Stop'
}
Export-OneShellUserProfile @exportUserProfileParams
}
}
}