Last active
September 9, 2019 21:22
-
-
Save LawrenceHwang/d7584ac74b8591b0d9da56041466c55c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
A wrapper for the tasklist /v /s COMPUTERNAME | |
.DESCRIPTION | |
This is a wrapper for the tasklist command. In the case PowerShell remoting is not available, this cmdlet would be useful to gather the process infromation. | |
#> | |
function Get-Tasklist | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
[string[]]$ComputerName | |
) | |
Begin | |
{ | |
[string[]]$result = "" | |
} | |
Process | |
{ | |
foreach ($c in $ComputerName) { | |
Write-Verbose -Message "checking $c" | |
$result = tasklist /v /s "$c" | |
if (!$?){ | |
# Better error handling can happen here. | |
Write-Warning 'Tasklist failed.' | |
throw | |
} | |
# Parsing the formatting based on the tasklist output | |
$format = $result[2] | ConvertFrom-String | measure -Property * -Character | select property, characters | |
foreach ($r in $result){ | |
if ($r.length -eq 0){ | |
Write-Verbose 'Hitting a blank line' | |
continue | |
} | |
if ($r -match '^(===)+' -or $r -match '^Image\sName\s+PID'){ | |
Write-Verbose 'Hitting the headers' | |
continue | |
} | |
else{ | |
Write-Verbose -Message "checking $r" | |
[int]$StringPosition = 0 | |
[int]$FieldCount = 0 | |
$ImageName = ($r.Substring($StringPosition,$format[$FieldCount].characters)).Trim() | |
$StringPosition = $StringPosition + $format[$FieldCount].characters + 1 | |
$FieldCount = $FieldCount +1 | |
$ProcessID = ($r.Substring($StringPosition,$format[$FieldCount].characters)).Trim() | |
$StringPosition = $StringPosition + $format[$FieldCount].characters +1 | |
$FieldCount = $FieldCount +1 | |
$SessionName = ($r.Substring($StringPosition,$format[$FieldCount].characters)).Trim() | |
$StringPosition = $StringPosition + $format[$FieldCount].characters +1 | |
$FieldCount = $FieldCount +1 | |
$Session = ($r.Substring($StringPosition,$format[$FieldCount].characters)).Trim() | |
$StringPosition = $StringPosition + $format[$FieldCount].characters+1 | |
$FieldCount = $FieldCount +1 | |
$MemUsage = ($r.Substring($StringPosition,$format[$FieldCount].characters)).Trim() | |
$StringPosition = $StringPosition + $format[$FieldCount].characters+1 | |
$FieldCount = $FieldCount +1 | |
$UserName = ($r.Substring($StringPosition,$format[$FieldCount].characters)).Trim() | |
$StringPosition = $StringPosition + $format[$FieldCount].characters+1 | |
$FieldCount = $FieldCount +1 | |
$CPUTime = ($r.Substring($StringPosition,$format[$FieldCount].characters)).Trim() | |
$StringPosition = $StringPosition + $format[$FieldCount].characters+1 | |
$FieldCount = $FieldCount +1 | |
$prop = [ordered]@{ | |
'ComputerName' = $c | |
'ImageName' = $ImageName | |
'PID' = $ProcessID | |
#'SessionName' = $SessionName | |
'Session#' = $Session | |
'MemUsage' = $MemUsage | |
#'Status' = $Status | |
'UserName' = $UserName | |
'CPUTime' = $CPUTime | |
#'WindowTitle' = $WindowTitle | |
} #End of $prop block | |
$ProcObj = New-Object -TypeName psobject -Property $prop | |
Write-Output $ProcObj | |
} #End of else block | |
} #End of foreach block of result text parsing | |
} #End of foreach block for computers | |
} #End of process block | |
End | |
{ | |
} | |
} #End of function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment