-
Notifications
You must be signed in to change notification settings - Fork 4
/
AdvancedFunctions.ps1
194 lines (146 loc) · 4.72 KB
/
AdvancedFunctions.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<#
.SYNOPSIS
Adds member to database
.DESCRIPTION
Adds all members to database one by one really slowly with a long help text
.PARAMETER GivenName
Given name of member
.PARAMETER Surname
Surname of member
.PARAMETER BirthYear
Year of birth for member
.PARAMETER Force
Skip confirmation per member by using -Force
.EXAMPLE
Add-SWMember -GivenName John -Surname Smith -BirthYear 1921
Adds a member named John Smith born in 1921.
.NOTES
This function was written by Simon
#>
function Add-SWMember {
[cmdletbinding(SupportsShouldProcess=$true)]
[OutputType('SimonWahlinUserClass')]
param (
[Parameter(Mandatory,Position = 2, ValueFromPipelineByPropertyName=$true)]
[string]
$GivenName,
[Parameter(Mandatory,Position = 1,ValueFromPipelineByPropertyName=$true)]
$Surname,
[Parameter(Position = 3,ValueFromPipelineByPropertyName=$true)]
[int]
$BirthYear,
[switch]
$Force
)
begin {
Write-Verbose -Message 'Connecting to members database'
}
process {
Write-Verbose -Message "Processing $GivenName..."
# $Result = [pscustomobject]@{
# GivenName = $GivenName
# Surname = $Surname
# BirthYear = $BirthYear
# FullName = '{0} {1}' -f $GivenName, $Surname
# }
if($PSBoundParameters.ContainsKey('BirthYear')){
$Result = [SimonWahlinUserClass]::new($GivenName,$Surname,$BirthYear)
} else {
$Result = [SimonWahlinUserClass]::new($GivenName,$Surname)
}
Set-DatabaseRow -InputObject $Result -Force:$Force
}
end {
Write-Verbose -Message 'Closing connection.'
}
}
function Set-DatabaseRow {
[cmdletbinding(SupportsShouldProcess=$true)]
param (
[SimonWahlinUserClass]
$InputObject,
[switch]
$Force
)
# Check WhatIf
if($PSCmdlet.ShouldProcess($Result.FullName,'Add member')) {
# Check if Force, or Confirm
if($Force -or $PSCmdlet.ShouldContinue($Result.FullName,'Confirm adding member:')) {
Write-Warning -Message 'Commiting to database!'
Write-Output -InputObject $Result
}
}
}
<#
.SYNOPSIS
Short description
.DESCRIPTION
Long description
.PARAMETER User
Parameter description
.PARAMETER GivenName
Parameter description
.PARAMETER Surname
Parameter description
.PARAMETER BirthYear
Parameter description
.PARAMETER Force
Parameter description
.EXAMPLE
An example
.NOTES
General notes
#>
function Update-SWMember {
[CmdletBinding()]
param (
# Parameter help description
[Parameter(Mandatory, ParameterSetName='ByUser')]
[SimonWahlinUserClass]
$User,
[Parameter(Mandatory,Position = 2, ValueFromPipelineByPropertyName=$true,ParameterSetName='Default')]
[string]
$GivenName,
[Parameter(Mandatory,Position = 1,ValueFromPipelineByPropertyName=$true,ParameterSetName='Default')]
$Surname,
[Parameter(Position = 3,ValueFromPipelineByPropertyName=$true,ParameterSetName='Default')]
[int]
$BirthYear,
[Parameter(ParameterSetName='Default')]
[Parameter(ParameterSetName='ByUser')]
[switch]
$Force
)
process {
if($PSCmdlet.ParameterSetName -eq 'Default') {
if($PSBoundParameters.ContainsKey('BirthYear')){
$User = [SimonWahlinUserClass]::new($GivenName,$Surname,$BirthYear)
} else {
$User = [SimonWahlinUserClass]::new($GivenName,$Surname)
}
} elseif($PSCmdlet.ParameterSetName -eq 'ByUser') {
# User already exsist
} else {
throw 'Not implented!'
}
return $User
}
}
# Add-SWMember Simon Wahlin 1983 -Verbose
# 'Simon' | Add-SWMember
# Import-Csv -Path .\Memberlist.csv -Delimiter ';' | Add-SWMember -Verbose
# $InputDat = Import-Csv -Path .\Memberlist.csv -Delimiter ';'
# foreach ($Entry in $InputDat) {
# Add-SWMember -GivenName $Entry.GivenName -Surname $Entry.Surname -BirthYear $Entry.BirthYear -Verbose
# }
# Import-Csv -Path .\Memberlist.csv -Delimiter ';' | ForEach-Object -Begin {} -Process {
# Add-SWMember -GivenName $_.GivenName -Surname $_.Surname -BirthYear $_.BirthYear -Verbose
# } -End {}
# DO NOT USE foreach as an alias for ForEach-Object
# Import-Csv -Path .\Memberlist.csv -Delimiter ';' | foreach -Process {
# Add-SWMember -GivenName $_.GivenName -Surname $_.Surname -BirthYear $_.BirthYear -Verbose
# }
# $PSDefaultParameterValues.Add('*:Surname','Wahlin')
# $PSDefaultParameterValues.Add('Add-SWMember:Surname','Wahlin')
# Add-SWMember -GivenName asdf -BirthYear 1923
# Add-SWMember -GivenName asdf -BirthYear 1923 -Surname Sigvardsson