Skip to content

Commit

Permalink
Merge pull request #7 from voytas75/v1.2.0
Browse files Browse the repository at this point in the history
V1.2.0
  • Loading branch information
voytas75 authored Mar 30, 2019
2 parents c12e755 + d2e9031 commit 9c64b67
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
82 changes: 44 additions & 38 deletions Find-TaskServiceUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ https://github.com/voytas75/Find-TaskServiceUser
.LINK
https://www.powershellgallery.com/packages/Find-TaskServiceUser
.NOTES
version 1.2.0, 30.03.2019:
- improved find of tasks
- tasks and services results grouped by computer
- minor changes
version 1.1.0, 30.03.2019:
- change private functions names
- minor fixes.
Expand Down Expand Up @@ -92,53 +96,55 @@ version 1.0, 27.03.2019:
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) {
Write-Verbose "services found"
if ($Log) {
Write-Log "$(get-date): Services:"
}
$output1 = $services | select-object SystemName,Name,DisplayName,StartName,State
$output = $output1 | Format-Table -AutoSize
$output
if ($Log) {
$output1 | ForEach-Object { Write-Log $_ }
}
} else {
if ($Log) {
Write-Log "$(get-date): No services found on computer ""$item"" for user ""$user"""
}
Write-output "No services found on computer ""$item"" for user ""$user"""
if ($services) {
Write-Verbose "System services were found"
if ($Log) {
Write-Log "$(get-date): System services:"
}
}
if ($task) {
Write-output "Finding tasks with user: ""$($user.trim().toupper())"" on machine: ""$($item.trim().toupper())"""
Write-output "Found system service(s) where ""$user"" matches 'Service Logon Account'"
$output1 = $services | select-object SystemName,Name, StartName,State
$output1 | Format-Table -AutoSize
if ($Log) {
Write-Log "$(get-date): Finding tasks with user: ""$($user.trim().toupper())"" on machine: ""$($item.trim().toupper())"""
$output1 | ForEach-Object { Write-Log $_ }
}
$tasks = Find-TaskUser -server $item.trim() -user $user
if ($tasks) {
if ($Log) {
Write-Log "$(get-date): Tasks:"
}
Write-Verbose -Message 'display tasks'
Write-output "Found scheduled tasks where ""$user"" matched task author or 'run as user'"
$tasksdata = $tasks | ConvertFrom-Csv | Select-Object Hostname, Taskname, Author, "Run as user"
$tasksdata
if ($Log) {
$tasksdata | ForEach-Object { Write-Log $_ }
}
} else {
if ($Log) {
Write-Log "$(get-date): No tasks on computer ""$item"" for user ""$user"""
} else {
if ($Log) {
Write-Log "$(get-date): No services found on computer ""$item"" for user ""$user"""
}
Write-output "No tasks foundon computer ""$item"" for user ""$user"""
Write-output "No system services found on computer ""$item"" for user ""$user"""
}
}
}
} # end foreach
foreach ($item in $Computer) {
if ($task) {
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) {
Write-Verbose "Scheduled tasks were found"
if ($Log) {
Write-Log "$(get-date): Scheduled tasks:"
}
Write-output "Found scheduled task(s) where ""$user"" matches task author or 'run as user'"
$tasksdata = $tasks | Select-Object Hostname, Taskname, Author, "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"""
}
Write-output "No scheduled tasks found on computer ""$item"" for user ""$user"""
}
}
} # end foreach
} # end PROCESS block
End {
if ($Log) {
Write-output "`nLog File: $($Logfile)"
Write-output "Log File: $($Logfile)"
}
} # end END block
} # end Find-TaskServiceUser function
Binary file modified Find-TaskServiceUser.psd1
Binary file not shown.
11 changes: 5 additions & 6 deletions Find-TaskUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ Function Find-TaskUser {
)
process {
Write-Verbose -Message 'running system command ''schtasks'''
if ($server -match $env:COMPUTERNAME) {
if ($server -match $env:COMPUTERNAME -or $server -eq "localhost") {
try {
$task_=Invoke-Expression "schtasks /query /fo csv /v" -ErrorAction Stop
$tasks=Invoke-Expression "schtasks /query /fo csv /NH /v" -ErrorAction Stop
}
catch {
Write-Error -Message "Failed to invoke ""schtasks"": $_"
}
} else {
try {
$task_=Invoke-Expression "schtasks /query /s $server /fo csv /v" -ErrorAction Stop
$tasks=Invoke-Expression "schtasks /query /s $server /NH /fo csv /v" -ErrorAction Stop
}
catch {
Write-Error -Message "Failed to invoke ""schtasks"": $_"
}
}
Write-Verbose -Message 'filtering tasks'
$a=$task_ | Where-Object {$_ -match $user}
# join header with data
return $task_[0],$a
$header = "HostName","TaskName","Next Run Time","Status","Logon Mode","Last Run Time","Last Result","Author","Task To Run","Start In","Comment","Scheduled Task State","Idle Time","Power Management","Run As User","Delete Task If Not Rescheduled","Stop Task If Runs X Hours and X Mins","Schedule","Schedule Type","Start Time","Start Date","End Date","Days","Months","Repeat: Every","Repeat: Until: Time","Repeat: Until: Duration","Repeat: Stop If Still Running"
return $tasks | ConvertFrom-Csv -Header $header | Where-Object {$_."Run As User" -match $user -or $_."Author" -match $user}
}
end {

Expand Down

0 comments on commit 9c64b67

Please sign in to comment.