-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfunction.PSCredential-example.ps1
More file actions
149 lines (129 loc) · 3.66 KB
/
function.PSCredential-example.ps1
File metadata and controls
149 lines (129 loc) · 3.66 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#Requires -Version 2.0
Set-StrictMode -Version Latest
Function global:Get-PSCredential
{
<#
.SYNOPSIS
Returns PSCredential object.
.DESCRIPTION
Returns PSCredential object from file. Prompts user for credentials and creates file if not present.
Credential store will be created within the user profile ($env:AppData\PSCredentials)
.PARAMETER UserName
UserName format: [[<hostname or NetBIOS domain name>\]<UserName>]
.EXAMPLE
Get-PSCredential
If no UserName is given, the script promts for credentials.
.EXAMPLE
Get-PSCredential user1
The script promts for the password if no PSCredential file exists.
.EXAMPLE
Get-PSCredential host1\user1
The script promts for the password if no PSCredential file exists.
.EXAMPLE
Get-PSCredential domain1\user1
The script promts for the password if no PSCredential file exists.
.EXAMPLE
Read-Host 'Please enter username' | Get-PSCredential
Pipeline input is accepted.
.INPUTS
UserName as String
.OUTPUTS
PSCredential object
.LINK
German Blog : http://www.powercli.de
English Blog: http://www.thomas-franke.net
.NOTES
NAME: Get-PSCredential.ps1
VERSION: 1.2a
AUTHOR: thomas.franke@sepago.de / sepago GmbH
LASTEDIT: 11.04.2014
#>
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$True)]
[String]$UserName
)
Function Import-PSCredential
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$PSCredentialFile
)
Try
{
$HashTable = Import-Clixml $PSCredentialFile
$PSCredential = New-Object System.Management.Automation.PSCredential $HashTable.UserName, $($HashTable.Password | ConvertTo-SecureString)
}
Catch
{
Throw "Content of Credential file $PSCredentialFile is not valid. Please delete the file and run the script again."
}
Write-Output $PSCredential
}
Function New-PSCredential
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[AllowEmptyString()]
[String]$PSCredentialFile,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$PSCredentialStorePath,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$PSCredentialFileSuffix
)
Try
{
$PSCredential = Get-Credential $UserName -ErrorAction Stop
}
Catch
{
Throw "Get-Credential was canceled by the user."
}
Try
{
$HashTable = @{
UserName = $PSCredential.UserName;
Password = $PSCredential.Password | ConvertFrom-SecureString
}
}
Catch
{
Throw "Password is empty. This is not allowed for security reasons."
}
$UserDomain = $PSCredential.GetNetworkCredential().Domain
$PSCredentialSubPath = "$PSCredentialStorePath\$UserDomain"
$PSCredentialFile = "$PSCredentialStorePath\$($PSCredential.UserName).$PSCredentialFileSuffix"
If ((Test-Path $PSCredentialSubPath) -eq $False)
{
New-Item $PSCredentialSubPath -type directory | Out-Null
}
# Corrects PowerShell 2.0 issue with Get-Content: UserName starts with "\" if no domain is given
If ($PSCredential.GetNetworkCredential().Domain -eq "")
{
$HashTable.UserName = $HashTable.UserName.Split("\")[-1]
}
$HashTable | Export-Clixml $PSCredentialFile
Write-Output $PSCredential
}
$PSCredentialFileSuffix = "PSCredential"
$PSCredentialStorePath = ".\PSCredentials"
If ((Test-Path $PSCredentialStorePath) -eq $False)
{
New-Item $PSCredentialStorePath -type Directory | Out-Null
}
$PSCredentialFile = "$PSCredentialStorePath\$UserName.$PSCredentialFileSuffix"
If ((Test-Path $PSCredentialFile) -eq $True)
{
$PSCredential = Import-PSCredential $PSCredentialFile
}
Else
{
$PSCredential = New-PSCredential $UserName $PSCredentialStorePath $PSCredentialFileSuffix
}
Write-Output $PSCredential
}