Search-TaskUser.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
Function Search-TaskUser {
    [CmdletBinding()]
    param(
        [string]$server,

        [string]$user
    )
    process {
        Write-Verbose -Message 'running system command ''schtasks'''
        if ($server -match $env:COMPUTERNAME) {
            try {
                $task_=Invoke-Expression "schtasks /query /fo csv /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
            }
            catch {
                Write-Error -Message "Failed to invoke ""schtasks"": $_"
            }
        } 
        Write-Verbose -Message 'filter tasks'
        $a=$task_ | Where-Object {$_ -match $user} 
        return $task_[0],$a
    }
    end {

    }
}