Command/More/GetProcessInfo.ps1



# function WriteProcessChain()
# {
# $processId = $PID
# $ids = GetPidChain($processId)
# $procs = Get-Process -id $ids
# $processNames = $procs | Select-Object ProcessName, Description, Id
# $processNames | Format-Table
# }

# function WriteScriptInfo([string]$argumentA, [string]$argumentB)
# {
# return @{
# "`$MyInvocation.PSScriptRoot" = $MyInvocation.PSScriptRoot;
# "`$MyInvocation.PSCommandPath" = $MyInvocation.PSCommandPath;
# "`$MyInvocation.InvocationName" = $MyInvocation.InvocationName;
# "`$MyInvocation.BoundParameters" = $MyInvocation.BoundParameters;
# "`$MyInvocation.UnboundArguments" = $MyInvocation.UnboundArguments;
# "`$MyInvocation.MyCommand.Path" = $MyInvocation.MyCommand.Path;
# "`$MyInvocation.MyCommand.InvocationName" = $MyInvocation.MyCommand.InvocationName;
# "`$MyInvocation.MyCommand.Name" = $MyInvocation.MyCommand.Name;
# "`$MyInvocation.MyCommand.Definition" = $MyInvocation.MyCommand.Definition;
# }
# }



function GetPidChain($processId)
{
    $list = New-Object Collections.Generic.List[Int]
    while ($null -ne $processId) {
        # The root pid has no process and should not be listed.
        if (Get-Process -id $processId -ErrorAction SilentlyContinue) {
            $list.Add($processId)
        }
        $processId = (Get-WmiObject win32_process |
            Where-Object processid -eq  $processId).parentprocessid
    }
    return $list
}

function GetProcessTable()
{
    $processId = $PID
    $ids = GetPidChain($processId)
    $processes = Get-Process -id $ids
    $processTable = ""
    foreach($p in $processes)
    {
        $processTable+= "$($p.Id) $($p.ProcessName) $($p.Description) `r`n"
    }
    return $processTable
}

function WaitForDebugger()
{
    $processTable  = GetProcessTable   
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [System.Windows.Forms.MessageBox]::Show(
        "Please, attach the debugger to the host process. When done, press OK. `r`n ProcessTable `r`n $processTable",
        "Attach to process", [System.Windows.Forms.MessageBoxButtons]::OK)
}

#WaitForDebugger

# function WritePromptLevel()
# {
# return @{
# PromptLevel = $NestedPromptLevel
# }
# }

# $host.UI.RawUI.WindowTitle = $MyInvocation.MyCommand.Name

# Write-Output "Script Info:"
# $htScript = @{
# "`$MyInvocation.PSScriptRoot" = $MyInvocation.PSScriptRoot;
# "`$MyInvocation.PSCommandPath" = $MyInvocation.PSCommandPath;
# "`$MyInvocation.InvocationName" = $MyInvocation.InvocationName;
# "`$MyInvocation.BoundParameters" = $MyInvocation.BoundParameters;
# "`$MyInvocation.UnboundArguments" = $MyInvocation.UnboundArguments;
# "`$MyInvocation.MyCommand.Path" = $MyInvocation.MyCommand.Path;
# "`$MyInvocation.MyCommand.InvocationName" = $MyInvocation.MyCommand.InvocationName;
# "`$MyInvocation.MyCommand.Name" = $MyInvocation.MyCommand.Name;
# "`$MyInvocation.MyCommand.Definition" = $MyInvocation.MyCommand.Definition;
# }
# $htScript

# Write-Output "Script Function Info:"
# $htFunction = WriteScriptInfo -ArgumentA "ValueA" -ArgumentC "ValueC"
# $htFunction

# $h = WritePromptLevel
# $h

# function GetPsCommandPath()
# {
# return @{
# PsCommandPath = $PsCommandPath
# }
# }

# $ht = GetPsCommandPath
# $ht

# "PsCommandPath" + $PsCommandPath

#"PsScriptRoot" + $PSScriptRoot

#$rdpPath = "C:\Users\z0012stm\Desktop\WORK.rdp"
#Start-Process "$env:windir\system32\mstsc.exe" -ArgumentList $rdpPath

Start-Process "$env:windir\system32\mstsc.exe" -ArgumentList "/v:192.168.2.123"


Read-Host "Waiting . . . "