-
Notifications
You must be signed in to change notification settings - Fork 14
/
QueryFormsiteData.ps1
283 lines (197 loc) · 11.1 KB
/
QueryFormsiteData.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# The below script can be used to query Formsite's API.
# This was initially created to view employees who filled out a COVID Symptom Tracker form whenever they went into the office
# It then checks to see if the Windows device that was logged into was accessed in the office (based on IP address)
# It finally sends an email to remind the employee to fill out the form if they did not already
$Obj1 = @()
$APIKey = "1aaaaaaaa1111aAaAAaa1aAAAAa1AaAAa" # https://www.formsite.com/blog/api-basics/#:~:text=The%20API%20key%20can%20be%20found%20on%20the,the%20end%20and%20separating%20with%20a%20%E2%80%98%26%E2%80%99%20symbol.
[datetime]$Today = Get-Date -Format MM/dd/yyyy
[System.Uri]$Uri = "https://yourformsitelink.formsite.com/api/v2/CompanyName/forms/FormIdentifierCharacters/results"
# Obtaining the Results of the form located at the URL specified above
$Results = Invoke-WebRequest -Method GET -Uri $Uri -Headers @{Authorization = "bearer $APIKey"} | ConvertFrom-Json | Select-Object -ExpandProperty Results
# Filtering those results to obtain the Name, Date, and Time which helps determine whether or not the employee needs to fill out the form today
$Results | ForEach-Object {
[datetime]$D,$T = ($_.date_finish).Split("T")
$Date = $D.ToShortDateString()
If ($Today -eq $Date) {
$Obj1 += New-Object -TypeName PSObject -Property @{Name=$_.items[0].value; Date=$D; Time=$T }
} # End If
} # End ForEach
# The below function is used to query the windows event logs to determine how an employee signed in
# (This ensures RDP access does not cause any false positives)
Function Get-LastLoginInfo {
[CmdletBinding(DefaultParameterSetName="Default")]
param(
[Parameter(
Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
Position=0)] # End Parameter
[String[]]$ComputerName=$env:COMPUTERNAME,
[Parameter(
Position=1,
Mandatory=$False,
ParameterSetName="Include")] # End Parameter
[String]$SamAccountName,
[Parameter(
Position=1,
Mandatory=$False,
ParameterSetName="Exclude")] # End Parameter
[String]$ExcludeSamAccountName,
[Parameter(
Mandatory=$False)] # End Parameter
[ValidateSet("SuccessfulLogin", "FailedLogin", "Logoff", "DisconnectFromRDP")]
[String]$LoginEvent = "SuccessfulLogin",
[Parameter(
Mandatory=$False)] # End Parameter
[Int]$PreviousHours = 13,
[Parameter(
Mandatory = $False)]
[Int]$MaxEvents = 1024,
[System.Management.Automation.PSCredential]$Credential
) # End param
BEGIN {
$StartDate = (Get-Date).AddHours(-$PreviousHours)
Switch ($LoginEvent) {
SuccessfulLogin {$EventID = 4624}
FailedLogin {$EventID = 4625}
Logoff {$EventID = 4647}
DisconnectFromRDP {$EventID = 4779}
} # End Switch
} # End BEGIN
PROCESS {
ForEach ($Computer in $ComputerName) {
Try {
$Computer = $Computer.ToUpper()
$Time = "{0:F0}" -f (New-TimeSpan -Start $StartDate -End (Get-Date) | Select-Object -ExpandProperty TotalMilliseconds) -as [int64]
If ($PSBoundParameters.ContainsKey("SamAccountName")) {
$EventData = "
*[EventData[
Data[@Name='TargetUserName'] != 'SYSTEM' and
Data[@Name='TargetUserName'] != '$($Computer)$' and
Data[@Name='TargetUserName'] = '$($SamAccountName)'
]
]
"
} # End If
If ($PSBoundParameters.ContainsKey("ExcludeSamAccountName")) {
$EventData = "
*[EventData[
Data[@Name='TargetUserName'] != 'SYSTEM' and
Data[@Name='TargetUserName'] != '$($Computer)$' and
Data[@Name='TargetUserName'] != '$($ExcludeSamAccountName)'
]
]
"
} # End If
If ((-not $PSBoundParameters.ContainsKey("SamAccountName")) -and (-not $PSBoundParameters.ContainsKey("ExcludeSamAccountName"))) {
$EventData = "
*[EventData[
Data[@Name='TargetUserName'] != 'SYSTEM' and
Data[@Name='TargetUserName'] != '$($Computer)$'
]
]
"
} # End If
$Filter = @"
<QueryList>
<Query Id="0">
<Select Path="Security">
*[System[
Provider[@Name='Microsoft-Windows-Security-Auditing'] and
EventID=$EventID and
TimeCreated[timediff(@SystemTime) <= $($Time)]
]
]
and
$EventData
</Select>
</Query>
</QueryList>
"@
If ($PSBoundParameters.ContainsKey("Credential")) {
$EventLogList = Get-WinEvent -ComputerName $Computer -FilterXml $Filter -Credential $Credential -ErrorAction Stop
} # End If
Else {
$EventLogList = Get-WinEvent -ComputerName $Computer -FilterXml $Filter -ErrorAction Stop
} # End Else
$Output = ForEach ($Log in $EventLogList) {
$TimeStamp = $Log.timeCReated.ToString('MM/dd/yyyy hh:mm tt') -as [DateTime]
Switch ($Log.Properties[8].Value) {
2 {$LoginType = 'Interactive'}
3 {$LoginType = 'Network'}
4 {$LoginType = 'Batch'}
5 {$LoginType = 'Service'}
7 {$LoginType = 'Unlock'}
8 {$LoginType = 'NetworkCleartext'}
9 {$LoginType = 'NewCredentials'}
10 {$LoginType = 'RemoteInteractive'}
11 {$LoginType = 'CachedInteractive'}
} # End Switch
If ($LoginEvent -eq 'FailedLogin') {
$LoginType = 'FailedLogin'
} # End If
If ($LoginEvent -eq 'DisconnectFromRDP') {
$LoginType = 'DisconnectFromRDP'
} # End If
If ($LoginEvent -eq 'Logoff') {
$LoginType = 'Logoff'
$UserName = $Log.Properties[1].Value.toLower()
} # End If
Else {
$UserName = $Log.Properties[5].Value.toLower()
} # End Else
[PSCustomObject]@{
ComputerName = $Computer
TimeStamp = $TimeStamp
UserName = $UserName
LoginType = $LoginType
} # End Custom Object
} # End $Output ForEach
$Output | Select-Object -Property ComputerName, TimeStamp, UserName, LoginType -Unique | Select-Object -First $MaxEvents
} # End Try
Catch {
Write-Error $_.Exception.Message
} # End Catch
} # End ForEach
} # End PROCESS
} # End Function Get-LastLoginInfo
# The below checks for the user currently logged into a device and ensures RDP access is excluded
$Console = qwinsta
ForEach ($Line in $Console) {
If ($Line[1] -like "c") {
$Tmp = $Line.Split(" ") | Where-Object { $_.Length -gt 0 }
If (($Line[19] -ne " ") -and ($Line[48] -eq "A")) {
$Object = New-Object -TypeName PSObject -Property @{ComputerName="$env:COMPUTERNAME";SessionName=$Tmp[0].Replace('>','');Username=$Tmp[1];ID=$Tmp[2];State=$Tmp[3];Type=$Tmp[4]}
} # End If
$Object = $Object | Select-Object -First 1
$Email = $Object.Username + "@$env:USERDNSDOMAIN"
$Session = $Object.SessionName
} # End If
} # End ForEach
# This translates a username into a name. This may be more involved for you depending on how email addresses are created
If ($Object) {
$FullName = $Object.Username.Replace("."," ") # EXAMPLE: rob.osborne becomes rob osborne
} # End If
Else {
Throw "[x] No users are locally signed into $env:COMPUTERNAME"
} # End Else
$LastLoginInfo = Get-LastLoginInfo -SamAccountName $Object.Username -PreviousHours 13 -MaxEvents 1024 -LoginEvent SuccessfulLogin
If (($LastLoginInfo) -and ($Obj1.Name.Trim() -NotContains $FullName.Replace(".","").Split("-")[0]) + " ") {
# Used to authenticate email sending. You can use another one of my scripts to not display a clear text password using https://github.com/tobor88/PowerShell/blob/master/Hide-PowerShellScriptPassword.ps1
$User = "$env:USERNAME@$env:USERDNSDOMAIN"
$String = "asdf...."
$Key = ("111", "11", "111", "111", "111", "111", "111", "111", "111", "111", "111", "111", "11", "111", "111", "111", "111", "111", "111", "111", "111", "111", "111", "11", "111", "111", "111", "111", "111", "111", "111")
$Argument2 = ($String | ConvertTo-SecureString -Key $Key)
# Get a list of IP addresses the device is connected too
$IPAddresses = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $Null } | Select-Object -ExpandProperty IPAddress
# Below we are defining subnets that exist inside your network. This tells us if the person filled out he form while at home or in the office
If ($IPAddresses -like "10.0.3.*" -or $IPAddresses -like "10.0.4.*" -or $IPAddresses -like "10.0.5.*" -or $IPAddresses -like "10.0.6.*" -or $IPAddresses -like "10.0.7.*" -or $IPAddresses -like "10.0.8.*" -or $IPAddresses -like "10.0.9.*") {
If (($Session -eq 'console') -and ($FullName -notlike "Exclude UserbyName")) {
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Argument2
Send-MailMessage -Credential $Cred -UseSsl -Priority Normal -Port 587 -SmtpServer smtp.office365.com -From $User -To $Email -Subject "ACTION REQUIRED: Fill Out Health Tracker" -Body "Hello, $FullName`n`nThis is a friendly reminder to complete the health tracker form. `n`nOur records indicate you are in the office and have not completed the Heath Tracker form at $($Uri | Out-String) Please fill it out for our compliance with health standards. Thank you"
} # End If
} # End If
Else {
Write-Output "[x] $env:COMPUTERNAME is not assigned an IP address in our Subnets"
} # End Else
} # End If