Find-TaskServiceUser.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
Function Find-TaskServiceUser {
<#
.SYNOPSIS
Finding scheduled tasks, system services on computer by user name.
.DESCRIPTION
Finding scheduled tasks, system services on local or remote computer by given user name.
'Administrator' and local computer are default values.
The results can be redirected to the log file (see 'log' parameter).
.PARAMETER User
User name to find scheduled tasks or system services. Default value is 'Administrator'.
.PARAMETER Computer
Computer to find tasks/services. Default value is 'localhost' ($env:COMPUTERNAME).
.PARAMETER Task
A Switch to enable finding scheduled tasks.
.PARAMETER Service
A Switch to enable finding system services.
.PARAMETER Minimal
A switch to enable minimalistic results. Object containing the computer name, number of tasks and/or number of services only. With -Log information about log file path is displayed but log file is not minimal. The return value is en object.
.PARAMETER Log
A switch to enable logging of output data to a log file. The log file with the path is defined in the "LogFile" parameter.
.PARAMETER Logfile
Path with file name where logging output. Default value is [$env:TEMP]\find-taskserviceuser.log. Works only with Log switch.
.EXAMPLE
PS> Find-TaskServiceUser -Computer "WSRV00" -User "BobbyK" -Service -Task -Log
 
Description
-----------
Find system services and scheduled tasks on "WSRV00" for user "BobbyK" with logging output to file.
.EXAMPLE
PS> "WSRV01","WSRV02" | Find-TaskServiceUser -Service -Task
 
Description
-----------
Find system services and scheduled tasks on computers "WSRV01", "WSRV02" for user "Administrator"
.LINK
https://github.com/voytas75/Find-TaskServiceUser
.LINK
https://www.powershellgallery.com/packages/Find-TaskServiceUser
.NOTES
VERSIONS:
version 1.3.3, 01.04.2019:
- added a description of the '-minimal' parameter
- minor fix
version 1.3.2, 01.04.2019:
- minor fix for iconuri
version 1.3.1, 01.04.2019:
- minor fix for iconuri
- minor changes
version 1.3.0, 01.04.2019:
- add '-Minimal' parameter for minimalist object as results
- use 'get-scheduletask' if available function
- fix for change in grouping the results of tasks and services
- add module icon (read ICON CREDITS)
- minor changes
version 1.2.0, 30.03.2019:
- improved find of tasks
- change in grouping the results of tasks and services
- minor changes
version 1.1.0, 30.03.2019:
- change private functions names
- minor fixes.
version 1.0.1, 29.03.2019:
- minor bug fixes.
version 1.0, 27.03.2019:
- first build of module created from function.
ICON CREDITS: Module icon made by [Freepik](https://www.freepik.com/) from [Flaticon](https://www.flaticon.com/) is licensed [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/)
 
#>

  [CmdletBinding()]
  Param(
    [parameter(mandatory=$false, position=0, valuefrompipeline = $true, ValueFromPipelineByPropertyName=$true, HelpMessage='Computer NetBIOS, DNS name or IP.')]
    [Alias('MachineName','Server')]
    [string[]]$Computer=$env:COMPUTERNAME,

    [parameter(Mandatory=$false, HelpMessage='User or group name to find scheduled tasks and/or services. Group is used for the security context of the scheduled task only, not system services.')]
    [string]$User='Administrator',

    [parameter(Mandatory=$false, HelpMessage='Switch to find system services.')]
    [switch]$Service,

    [parameter(Mandatory=$false, HelpMessage='Switch to find scheduled tasks.')]
    [switch]$Task,

    [parameter(Mandatory=$false, HelpMessage='Minimalistic results. Object containing the computer name, number of tasks and/or number of services only. with -Log info about log file is displayed but log file is not minimal.')]
    [switch]$Minimal,

    [parameter(Mandatory=$false, HelpMessage='Switch to enable logging.')]
    [switch]$Log,

    [parameter(Mandatory=$false, HelpMessage='Log file path. Default is ''[$env:TEMP]\Find-TaskServiceUser.log''.')]
    [string]$Logfile="$env:TEMP\Find-TaskServiceUser.log"
  )
  Begin {
    if (!$service -and !$task) {
      Write-output "You must provide 'service' or/and 'task' parameter`n"
      Write-output 'Examples:'
      Write-output ' Find-TaskServiceUser -Computer "WSRV00" -User "BobbyK" -Service -Task'
      Write-output ' Find-TaskServiceUser -Computer "WSRV01" -User "BobbyK" -Task -Log' 
      Write-output ' "WSRV00","WSRV03" | Find-TaskServiceUser -Service -Task'
      Write-output ' "WSRV04" | Find-TaskServiceUser -Service'
    } else {
      if (!$Minimal) {
        if ($user -eq "administrator") {
          Write-Output "Set default user: Administrator"
        }
        if ($computer -eq $env:COMPUTERNAME) {
          Write-output "Set default computer: $env:COMPUTERNAME (localhost)"
        }  
      }
    }
    if ($Minimal) {
      Write-Verbose "Initializing minimalistic results."
      $minimal_obj = @()
      $s=0
      $t=0
      $services_count = $s
      $tasks_count = $t
    }
    if ($Log) {
      Write-Log "---------$(get-date)---------"
    }
  } # end BEGIN block
  Process {
    foreach ($item in $Computer) {
      if ($service) {    
        if (!$Minimal) {
          Write-output "Finding system services with user: ""$($user.trim().toupper())"" on machine: ""$($item.trim().toupper())"""
        }
        if ($Log) {
          Write-Log "$(get-date): Finding services with user: ""$($user.trim().toupper())"" on machine: ""$($item.trim().toupper())"""
        }
        $services = Find-ServiceUser -computer $item.Trim() -user $user
        if ($services) {
          if ($Log) {
            Write-Log "$(get-date): System services:"
          }
          $output1 = $services | select-object SystemName,Name, StartName,State
          if ($Minimal) {
            $services_count = ($services | Measure-Object).count
          } else {
            Write-output "Found system service(s) where ""$user"" matches 'Service Logon Account'"
            $output1 | Format-Table -AutoSize
          }
          if ($Log) {
            $output1 | ForEach-Object { Write-Log $_ }            
          }
        } else {
          if ($Log) {
            Write-Log "$(get-date): No services found on computer ""$item"" for user ""$user"""
          }
          if ($Minimal) {
            $services_count = $s
          } else {
            Write-output "No system services found on computer ""$item"" for user ""$user"""          
          }
        }
      }
      if ($task) {
        if (!$Minimal) {
          Write-output "Finding tasks with user: ""$($user.trim().toupper())"" on machine: ""$($item.trim().toupper())"""
        }
        if ($Log) {
          Write-Log "$(get-date): Finding tasks with user: ""$($user.trim().toupper())"" on machine: ""$($item.trim().toupper())"""
        }
        $tasks = Find-TaskUser -server $item.trim() -user $user
        if ($tasks) {
          if ($Log) {
            Write-Log "$(get-date): Scheduled tasks:"
          }
          $tasksdata = $tasks | Select-Object Hostname, Taskname, Author, "Run as user", URI
          if ($Minimal) {
            $tasks_count = ($tasks | Measure-Object).count
          } else {
            Write-output "Found scheduled task(s) where ""$user"" matches task author or 'run as user'"
            $tasksdata | Format-Table -AutoSize
          }
          if ($Log) {
            $tasksdata | ForEach-Object { Write-Log $_ }
          }
          } else {
            if ($Log) {
              Write-Log "$(get-date): No scheduled tasks on computer ""$item"" for user ""$user"""
            }
            if ($Minimal) {
              $tasks_count = $t
            } else {
              Write-output "No scheduled tasks found on computer ""$item"" for user ""$user"""
            }
          }
        }
        if (!$tasks) {
          $tasks_count = $null
        }
        if (!$services) {
          $services_count = $null
        }
        if ($Minimal) {
          $minimal_obj += [PSCustomObject]@{
            ComputerName  = $item
            Services      = $services_count
            Tasks         = $tasks_count
          }
          $services_count = $s
          $tasks_count = $t
        }
    } # end foreach
  } # end PROCESS block
  End {
    if ($Log -and !$Minimal) { 
      Write-output "Log File: $($Logfile)"
    } else {
      Write-Information -MessageData "Log File: $($Logfile)" -InformationAction Continue
      return $minimal_obj
    }
  } # end END block
} # end Find-TaskServiceUser function