-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_Writing and Using Functions.ps1
250 lines (216 loc) · 7.8 KB
/
9_Writing and Using Functions.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# -------------------------------------------------------
# Writing and Using Functions
# -------------------------------------------------------
# Simple Function
Function BoolToReadable {
"No"
}
BoolToReadable
# Simple Function with Arguments
Function BoolToReadable($current, $test) {
if($current){
return "Yes"
}
$test
"No"
}
BoolToReadable $false "bob"
# Function with Parameters
Function BoolToReadable {
param(
[bool] $Current,
[bool] $Test
)
if($Current) {
return "Yes"
}
"No"
}
BoolToReadable -Current $true
# Function with Advanced Parameters
Function BoolToReadable {
param(
[Parameter(Mandatory = $true)]
[bool] $Current = $false
)
Write-Verbose "Current Value is: $($Current.ToString())" -Verbose
if($Current) {
return "Yes"
}
"No"
}
BoolToReadable
BoolToReadable -Current $false
#Function with Advanced Parameter + Common Parameters
Function BoolToReadable {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[bool] $Current
)
Write-Host "Verbose Var: $($VerbosePreference)"
Write-Verbose "Current Value is: $($Current.ToString())"
if($Current) {
return "Yes"
}
"No"
}
BoolToReadable
BoolToReadable -Current $false
BoolToReadable -Current $true -Verbose
# -------------------------------------------------------
# Example of Using
# -------------------------------------------------------
$userData = '[
{
"AccountExpires": null,
"Description": "Built-in account for administering the computer/domain",
"Enabled": true,
"FullName": "",
"PasswordChangeableDate": null,
"PasswordExpires": null,
"UserMayChangePassword": true,
"PasswordRequired": true,
"PasswordLastSet": null,
"LastLogon": "\/Date(1605771872777)\/",
"Name": "Administrator",
"SID": {
"BinaryLength": 28,
"AccountDomainSid": "S-1-5-21-1627568633-850217724-3435047281",
"Value": "S-1-5-21-1627568633-850217724-3435047281-500"
},
"PrincipalSource": 1,
"ObjectClass": "User"
},
{
"AccountExpires": null,
"Description": "A user account managed by the system.",
"Enabled": false,
"FullName": "",
"PasswordChangeableDate": null,
"PasswordExpires": null,
"UserMayChangePassword": true,
"PasswordRequired": false,
"PasswordLastSet": null,
"LastLogon": null,
"Name": "DefaultAccount",
"SID": {
"BinaryLength": 28,
"AccountDomainSid": "S-1-5-21-1627568633-850217724-3435047281",
"Value": "S-1-5-21-1627568633-850217724-3435047281-503"
},
"PrincipalSource": 1,
"ObjectClass": "User"
},
{
"AccountExpires": null,
"Description": "Built-in account for guest access to the computer/domain",
"Enabled": false,
"FullName": "",
"PasswordChangeableDate": null,
"PasswordExpires": null,
"UserMayChangePassword": false,
"PasswordRequired": false,
"PasswordLastSet": null,
"LastLogon": null,
"Name": "Guest",
"SID": {
"BinaryLength": 28,
"AccountDomainSid": "S-1-5-21-1627568633-850217724-3435047281",
"Value": "S-1-5-21-1627568633-850217724-3435047281-501"
},
"PrincipalSource": 1,
"ObjectClass": "User"
},
{
"AccountExpires": null,
"Description": "shared",
"Enabled": false,
"FullName": "shared",
"PasswordChangeableDate": "\/Date(-11644387200000)\/",
"PasswordExpires": null,
"UserMayChangePassword": false,
"PasswordRequired": true,
"PasswordLastSet": null,
"LastLogon": null,
"Name": "shared",
"SID": {
"BinaryLength": 28,
"AccountDomainSid": "S-1-5-21-1627568633-850217724-3435047281",
"Value": "S-1-5-21-1627568633-850217724-3435047281-1002"
},
"PrincipalSource": 1,
"ObjectClass": "User"
},
{
"AccountExpires": null,
"Description": "A user account managed and used by the system for Windows Defender Application Guard scenarios.",
"Enabled": false,
"FullName": "",
"PasswordChangeableDate": "\/Date(1605858056395)\/",
"PasswordExpires": "\/Date(1609400456395)\/",
"UserMayChangePassword": true,
"PasswordRequired": true,
"PasswordLastSet": "\/Date(1605771656395)\/",
"LastLogon": null,
"Name": "WDAGUtilityAccount",
"SID": {
"BinaryLength": 28,
"AccountDomainSid": "S-1-5-21-1627568633-850217724-3435047281",
"Value": "S-1-5-21-1627568633-850217724-3435047281-504"
},
"PrincipalSource": 1,
"ObjectClass": "User"
}
]'
$users = $userData | ConvertFrom-Json
$adminGroup = "Administrator", "Tim Davis"
# Step 1, run lines 1-104 to populate $users with User data (Local User Accounts from a PC)
# and Administrators Group data.
# Using user data, generate a collection of custom user objects with the following properties
# Name
# Enabled (Yes|No)
# PasswordRequired (Yes|No)
# UserCanChangePassword (Yes|No)
# IsAdmin (Yes|No) (Can compare to users in $adminGroup)
# IsBuiltInAdmin (Yes|No)
# One a collection of custom objects have been created, display them by piping to Format-Table
# Results will look like
#
#Name PasswordRequired IsBuiltInAdmin IsAdmin UserMayChangePassword Enabled
#---- ---------------- -------------- ------- --------------------- -------
#Administrator Yes Yes Yes Yes Yes
#DefaultAccount No No No Yes No
#Guest No No No No No
#shared Yes No No No No
#WDAGUtilityAccount Yes No No Yes No
# In this exercise you will use Loops, Conditionals, could potentially use a Function,
# HashTable, as well as creaing an object from a Hashtable, and Lists
$results = New-Object -TypeName System.Collections.Generic.List[PSCustomObject]
# Function to convert True/False to Yes/No
Function BoolToReadable {
param(
[bool] $Current
)
if($Current) {
return "Yes"
}
return "No"
}
# Loop through each user
foreach($user in $users) {
# Create a new Hashtable with the properties we want
$hash = @{
Name = $user.Name
Enabled = BoolToReadable -Current $user.Enabled # <- Use function to do conversion over and over again
PasswordRequired = BoolToReadable -Current $user.PasswordRequired
UserMayChangePassword = BoolToReadable -Current $user.UserMayChangePassword
IsAdmin = BoolToReadable -Current ($user.Name -in $adminGroup) # <- Since function is expecting T/F we can use Conditionals
IsBuiltInAdmin = BoolToReadable -Current ($user.Name -eq "Administrator")
}
# Create our new Object from the Hashtable
$obj = New-Object -TypeName PSCustomObject -Property $hash
# Add Object to results colleciton
$results.Add($obj)
}
$results | Format-Table