-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShareFileShell.psm1
160 lines (153 loc) · 4.92 KB
/
ShareFileShell.psm1
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
150
151
152
153
154
155
156
157
158
159
160
<#
.DESCRIPTION
ShareFile API cmdlets.
.SYNOPSIS
ShareFile API cmdlets.
.NOTES
TODO
.EXAMPLE
TODO
#>
$FunctionsPath = Join-Path -Path $PSScriptRoot -ChildPath 'functions'
Get-ChildItem -File -Path $FunctionsPath -Filter '*.ps1' |
Where-Object { $_.BaseName -notmatch '(^\.|\.dev$|\.test$)' } |
ForEach-Object {
. $_.FullName
}
function Add-Credential {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
# Credential to be added
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[pscredential]
$Credential
,
# Force add. Overwrite if exists. Don't ask for confirmation unless explicitly specified.
[Parameter()]
[switch]
$Force
)
if ($Force -and !$Confirm) {
$ConfirmPreference = 'None'
}
$EnvUser = if ($IsLinux) { 'USER' } else { 'USERNAME' }
$EnvName = if ($IsLinux) { 'NAME' } else { 'COMPUTERNAME' }
$HostUser = [System.Environment]::GetEnvironmentVariable($EnvUser)
$HostName = [System.Environment]::GetEnvironmentVariable($EnvName)
$UserHost = "$($HostUser)@$($HostName)"
Write-Verbose -Message "$($MyInvocation.MyCommand.Name) : Authenticating with user '$($Credential.UserName)' and password."
$Script:Token = Get-SfToken -Credential $Credential
if (!$Script:Token) {
Remove-Variable -Name Credential
Write-Error -Message "$($MyInvocation.MyCommand.Name) : Authentication failed using '$($Credential.UserName)' and password." -ErrorAction Stop
}
$UserHostCredential = New-Object -TypeName PSCustomObject -Property @{
Name = $UserHost
Credential = $Credential
}
if (!($Script:Config.PSObject.Properties | Where-Object { $_.Name -eq 'credentials' })) {
Write-Verbose -Message "$($MyInvocation.MyCommand.Name) : Creating credential array and adding credential."
$Script:Config | Add-Member -MemberType NoteProperty -Name credentials -Value @($UserHostCredential)
return $Script:Config
}
Write-Verbose -Message "$($MyInvocation.MyCommand.Name) : Checking for existing credentials for '$($UserHost)'."
$ExistingCredential = $Script:Config.credentials | Where-Object { $_.Name -eq $UserHost }
if (!$ExistingCredential) {
Write-Verbose -Message "$($MyInvocation.MyCommand.Name) : Adding '$($UserHostCredential.Credential.UserName)' in '$($UserHostCredential.Name)' to the configuration."
$Script:Config.credentials += $UserHostCredential
return $Script:Config
}
else {
Write-Warning -Message "$($MyInvocation.MyCommand.Name) : Credential for '$($UserHost)' already exists."
if ($Force) {
Write-Verbose -Message "$($MyInvocation.MyCommand.Name) : Updating credential for '$($UserHost)'."
$ExistingCredential.Credential = $Credential
}
}
$Script:Config
}
function Update-Credential {
[CmdletBinding()]
param(
# Credential to be updated
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[pscredential]
$Credential
)
Add-Credential -Credential $Credential -Force
}
function Remove-Credential {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
# Credential to be removed
[Parameter()]
[string]
$Name
,
# Force remove. Don't ask for confirmation unless explicitly specified.
[Parameter()]
[switch]
$Force
)
if ($Force -and !$Confirm) {
$ConfirmPreference = 'None'
}
$EnvUser = if ($IsLinux) { 'USER' } else { 'USERNAME' }
$EnvName = if ($IsLinux) { 'NAME' } else { 'COMPUTERNAME' }
$HostUser = [System.Environment]::GetEnvironmentVariable($EnvUser)
$HostName = [System.Environment]::GetEnvironmentVariable($EnvName)
$UserHost = "$($HostUser)@$($HostName)"
if (!$Name) {
Write-Warning -Message "No credential name given. Using '$($UserHost)'."
$Name = $UserHost
}
if ($PSCmdlet.ShouldProcess($Name)) {
Write-Verbose -Message "Removing credential for '$($Name)'."
$Script:Config.credentials = $Script:Config.credentials | Where-Object { $_.Name -ne $Name }
}
$Script:Config
}
function Import-Config {
[CmdletBinding()]
param(
# Path to CliXML config.
[Parameter(Mandatory = $true
, Position = 0
, ValueFromPipeline = $true
, ValueFromPipelineByPropertyName = $true
, HelpMessage="Path to CliXML config")]
[Alias("PSPath")]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
$Path
)
Import-Clixml -Path $Path
}
function Export-Config {
[CmdletBinding()]
param(
# Path to CliXML config.
[Parameter(Mandatory = $true
, Position = 0
, ValueFromPipeline = $true
, ValueFromPipelineByPropertyName = $true
, HelpMessage="Path to CliXML config")]
[Alias("PSPath")]
[ValidateNotNullOrEmpty()]
[string]
$Path
,
# Object containing configuration.
[Parameter(Mandatory = $true
, HelpMessage="Object containing configuration")]
[ValidateNotNullOrEmpty()]
[System.Object]
$Config
)
$Config | Export-Clixml -Path $Path
}
New-Variable -Force -Scope Script -Name ConfigPath -Value (Join-Path -Path $PSScriptRoot -ChildPath '.config.xml')
New-Variable -Force -Scope Script -Name Config -Value (Import-Config -Path $ConfigPath)
New-Variable -Force -Scope Script -Name Token -Value $null