PS> 1, 2.5, "oranges", (Get-Process svchost)
1 <--- an integer
2.5 <--- a double precision floating point number
oranges <--- a string
... <--- a Process object
Showing posts with label get-process. Show all posts
Showing posts with label get-process. Show all posts
Tuesday, January 20, 2009
count all running processes
PS> $processCount = (get-process).count
PS> "$processCount processes running in the system."
21 processes running in the system.
PS> "$processCount processes running in the system."
21 processes running in the system.
focus on how the powershell process object has been configured
PS> get-process powershell
| FL PSConfiguration
Name : powershell
Id : 896
PriorityClass : Normal
FileVersion : 6.1.6585.1 (fbl_srv_powershell_ctp.080411-1634)
| FL PSConfiguration
Name : powershell
Id : 896
PriorityClass : Normal
FileVersion : 6.1.6585.1 (fbl_srv_powershell_ctp.080411-1634)
report on the system resources
to report on the system resources that are being used by powershell process object
PS> get-process powershell
| FL PSResources
Name : powershell
Id : 896
HandleCount : 505
WorkingSet : 4820992
PagedMemorySize : 47415296
PrivateMemorySize : 47415296
VirtualMemorySize : 203001856
TotalProcessorTime : 00:00:14.1718750
PS> get-process powershell
| FL PSResources
Name : powershell
Id : 896
HandleCount : 505
WorkingSet : 4820992
PagedMemorySize : 47415296
PrivateMemorySize : 47415296
VirtualMemorySize : 203001856
TotalProcessorTime : 00:00:14.1718750
display processes's alias property
PS> get-process
| get-member -type AliasProperty
TypeName: System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
Handles AliasProperty Handles = Handlecount
Name AliasProperty Name = ProcessName
NPM AliasProperty NPM = NonpagedSystemMemorySize
PM AliasProperty PM = PagedMemorySize
VM AliasProperty VM = VirtualMemorySize
WS AliasProperty WS = WorkingSet or physical memory
| get-member -type AliasProperty
TypeName: System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
Handles AliasProperty Handles = Handlecount
Name AliasProperty Name = ProcessName
NPM AliasProperty NPM = NonpagedSystemMemorySize
PM AliasProperty PM = PagedMemorySize
VM AliasProperty VM = VirtualMemorySize
WS AliasProperty WS = WorkingSet or physical memory
export and import processes by using a variable
step 1) PS> $p = get-process
step 2) Now write the variable to a CSV file and a CliXML file:
PS> $p | export-CSV .\test.csv
PS> $p | export-CliXML .\test.xml
step 3) Now import these values into two new variables:
PS> $p1 = import-csv .\test.csv
PS> $p2 = import-CliXML .\test.xml
step 4) To view their output from these variables:
PS> $p1 <----------- in a list format
PS> $p2 <----------- in a tabular format
step 2) Now write the variable to a CSV file and a CliXML file:
PS> $p | export-CSV .\test.csv
PS> $p | export-CliXML .\test.xml
step 3) Now import these values into two new variables:
PS> $p1 = import-csv .\test.csv
PS> $p2 = import-CliXML .\test.xml
step 4) To view their output from these variables:
PS> $p1 <----------- in a list format
PS> $p2 <----------- in a tabular format
list of processes using $_
to list of processes but display the name and CPU time
PS> get-process
| forEach-object { write-host $_.ProcessName $_.CPU }
PS> get-process
| forEach-object { write-host $_.ProcessName $_.CPU }
store them into a variable
to list all processes and sort these in descending order according to their CPU time, and store them into a variable
PS> $a = get-process
| sort-object CPU -descending
PS> $a
PS> $a = get-process
| sort-object CPU -descending
PS> $a
the "format.ps1xml" files
to display Explorer process object by using the views (tree) defined in the "format.ps1xml" files in PS directory
PS> get-process explorer
| format-custom Modules
class Process
{
Modules =
[
class ProcessModule
{
...
class FileVersionInfo
{
...
PS> get-process explorer
| format-custom Modules
class Process
{
Modules =
[
class ProcessModule
{
...
class FileVersionInfo
{
...
CPU clock cycles
to list all processes running on a local machine that are using more CPU clock cycles than other processes
PS> Get-Process |
ForEach-Object `
{if ($_.cpu -lt 100)
{Write-Host $_.name, $_.cpu -foregroundcolor Yellow}
elseif ($_.cpu -gt 100)
{Write-Host $_.name, $_.cpu -foregroundcolor Cyan}}
We use an if statement to decide on the color of the text to display. If the amount of CPU time is less than 100, then the color of text is yellow. If it is more than 100, then we change the color of the text to cyan.
PS> Get-Process |
ForEach-Object `
{if ($_.cpu -lt 100)
{Write-Host $_.name, $_.cpu -foregroundcolor Yellow}
elseif ($_.cpu -gt 100)
{Write-Host $_.name, $_.cpu -foregroundcolor Cyan}}
We use an if statement to decide on the color of the text to display. If the amount of CPU time is less than 100, then the color of text is yellow. If it is more than 100, then we change the color of the text to cyan.
list detailed information with FL and FT
to list the modules property of Explorer process object
PS> get-process explorer
| fl Modules
or,
PS> get-process explorer
| fl -property name,modules
or,
PS> get-process explorer
| ft modules -wrap
or,
PS> get-process explorer
| fl Modules
| out-file 'test2.htm'
Modules : {System.Diagnostics.ProcessModule (Explorer.EXE), System.Diagnostics.ProcessModule (ntdll.dll), System.Diagnostics.ProcessModule (kernel32.dll), System.Diagnostics.ProcessModule (ADVAPI32.dll)...}
PS> get-process explorer
| fl Modules
or,
PS> get-process explorer
| fl -property name,modules
or,
PS> get-process explorer
| ft modules -wrap
or,
PS> get-process explorer
| fl Modules
| out-file 'test2.htm'
Modules : {System.Diagnostics.ProcessModule (Explorer.EXE), System.Diagnostics.ProcessModule (ntdll.dll), System.Diagnostics.ProcessModule (kernel32.dll), System.Diagnostics.ProcessModule (ADVAPI32.dll)...}
Monday, January 19, 2009
start and stop the Calc process
to start and stop the Calc process and then detects processes that have stopped
PS> calc <---- start the Calc process
PS> $p = get-process calc <---- get an object and store it
PS> stop-process -inputobject $p <----- stop the Calc process
PS> get-process | where-object {$_.HasExited}
` HasExited ` is a property of process object. Because the Calc process has been stopped, the value of HasExited property is TRUE, so there is no output.
PS> calc <---- start the Calc process
PS> $p = get-process calc <---- get an object and store it
PS> stop-process -inputobject $p <----- stop the Calc process
PS> get-process | where-object {$_.HasExited}
` HasExited ` is a property of process object. Because the Calc process has been stopped, the value of HasExited property is TRUE, so there is no output.
list the running processes grouped by priority
PS> $a = get-process
PS> get-process -inputobject $a
| format-table -view priority
Explanation: The first command gets all of the processes on the computer and stores them in the $a variable. The second command uses the InputObject parameter to pass the process objects that are stored in the $a variable to Get-Process. The pipeline operator passes the objects to the Format-Table cmdlet, which formats the processes by using the "Priority" view defined in the PS1XML format files in the Windows PowerShell home directory ($pshome).
PriorityClass: Normal/ High. It shows properties such as ProcessName, Id, HandleCount, WorkingSet
PS> get-process -inputobject $a
| format-table -view priority
Explanation: The first command gets all of the processes on the computer and stores them in the $a variable. The second command uses the InputObject parameter to pass the process objects that are stored in the $a variable to Get-Process. The pipeline operator passes the objects to the Format-Table cmdlet, which formats the processes by using the "Priority" view defined in the PS1XML format files in the Windows PowerShell home directory ($pshome).
PriorityClass: Normal/ High. It shows properties such as ProcessName, Id, HandleCount, WorkingSet
working set
to get all running processes that have a working set greater than 20 MB
PS> get-process
| where-object {$_.WorkingSet -gt 20000000}
` -gt ` stands for 'greater than'.
` 20000000 ` is in bytes.
The pipeline operator ( | ) passes the process objects to the 'Where-Object' cmdlet, which selects only the object with a value greater than 20,000,000 bytes for the WorkingSet property.
PS> get-process
| where-object {$_.WorkingSet -gt 20000000}
` -gt ` stands for 'greater than'.
` 20000000 ` is in bytes.
The pipeline operator ( | ) passes the process objects to the 'Where-Object' cmdlet, which selects only the object with a value greater than 20,000,000 bytes for the WorkingSet property.
display data with DataGrid
to tabulate a list of all services by sorting the services by their state before filling the datagrid, then output the processes
PS>
[void][System.reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$form = new-object System.Windows.Forms.Form
$DataGridView = new-object System.windows.forms.DataGridView
$Form.Text = "My First Datagrid"
$array= new-object System.Collections.ArrayList
$array.AddRange( @( get-service | write-output ) )
// $array.AddRange( @( get-process | sort-object company | write-output ) )
$DataGridView.DataSource = $array
$DataGridView.Dock = "fill"
$DataGridView.AllowUsertoResizeColumns=$True
$form.Controls.Add($DataGridView)
$form.showdialog()
For Get-Service cmdlet, the column headers in datagrid shows (Name, CanPauseAndContinue, CanShutdown, CanStop, DisplayName, ServiceName, Status)
For Get-Process cmdlet, the column headers in datagrid shows ( _NounName, Name, Handles, VM, WS, PM, NPM, Path, Company, CPU, FileVersion, ProductVersion, Description, Product, BasePriority, HandleCount, Id, etc. ) or, get-process | fl *
PS>
[void][System.reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$form = new-object System.Windows.Forms.Form
$DataGridView = new-object System.windows.forms.DataGridView
$Form.Text = "My First Datagrid"
$array= new-object System.Collections.ArrayList
$array.AddRange( @( get-service | write-output ) )
// $array.AddRange( @( get-process | sort-object company | write-output ) )
$DataGridView.DataSource = $array
$DataGridView.Dock = "fill"
$DataGridView.AllowUsertoResizeColumns=$True
$form.Controls.Add($DataGridView)
$form.showdialog()
For Get-Service cmdlet, the column headers in datagrid shows (Name, CanPauseAndContinue, CanShutdown, CanStop, DisplayName, ServiceName, Status)
For Get-Process cmdlet, the column headers in datagrid shows ( _NounName, Name, Handles, VM, WS, PM, NPM, Path, Company, CPU, FileVersion, ProductVersion, Description, Product, BasePriority, HandleCount, Id, etc. ) or, get-process | fl *
terminate sessions of SQL
to terminate sessions of SQL Server with confirming
PS> get-process -name sql* | stop-process -passthru
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
52 2 900 3020 47 0.05 1564 sqlmangr
Warning!
Are you sure you want to perform this action?
Performing operation 'Stop-Process' on Target 'sqlservr(1072)'
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):l <--- stands for 'No to All'
PS> get-process -name sql* | stop-process -passthru
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
52 2 900 3020 47 0.05 1564 sqlmangr
Warning!
Are you sure you want to perform this action?
Performing operation 'Stop-Process' on Target 'sqlservr(1072)'
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):l <--- stands for 'No to All'
Labels:
get-process,
PowerShell,
SQL,
SQL Server,
stop-process
Sunday, January 18, 2009
arrange the list in reverse order
to sort the objects in reverse order by ProcessName
PS> get-process | sort-object -Property Name -descending
The way Sort-Object cmdlet works:
0a <-- Zero first then letter 'a'
0A
A0
a0
F
f
Ff
fF
PS> get-process | sort-object -Property Name -descending
The way Sort-Object cmdlet works:
0a <-- Zero first then letter 'a'
0A
A0
a0
F
f
Ff
fF
print out a list of processes
[] to send a list of processes to the unicode file
PS> get-process
| out-file -filepath d:\temp\sample.txt
[] to send a list of processes to default printer, e.g. doPDF
PS> get-process
| out-printer
[] to send a list of processes to the ASCII file
PS> get-process
| out-file -filepath d:\temp\sample.txt -Encoding ASCII
[] to save output as it would have displayed on the console
PS> get-process
| out-file -filepath d:\temp\sample.txt -Width 80
Maximum is 2147483647. For example, MS Notepad can accomodate a line that ends at position 1000th (when thewordwrap feature is disabled).
PS> get-process
| out-file -filepath d:\temp\sample.txt
[] to send a list of processes to default printer, e.g. doPDF
PS> get-process
| out-printer
[] to send a list of processes to the ASCII file
PS> get-process
| out-file -filepath d:\temp\sample.txt -Encoding ASCII
[] to save output as it would have displayed on the console
PS> get-process
| out-file -filepath d:\temp\sample.txt -Width 80
Maximum is 2147483647. For example, MS Notepad can accomodate a line that ends at position 1000th (when thewordwrap feature is disabled).
group processes
to group processes by name for easier inspection
PS> get-process -name svchost
| ft -Wrap -autosize path,company,id -groupby name
Name: svchost
Path Company Id
---- ------- --
C:\LINDOWS\system32\svchost.exe Microsoft Corporation 704
C:\LINDOWS\system32\svchost.exe Microsoft Corporation 768
C:\LINDOWS\System32\svchost.exe Microsoft Corporation 808
C:\LINDOWS\system32\svchost.exe Microsoft Corporation 856
C:\LINDOWS\system32\svchost.exe Microsoft Corporation 892
PS> get-process -name svchost
| ft -Wrap -autosize path,company,id -groupby name
Name: svchost
Path Company Id
---- ------- --
C:\LINDOWS\system32\svchost.exe Microsoft Corporation 704
C:\LINDOWS\system32\svchost.exe Microsoft Corporation 768
C:\LINDOWS\System32\svchost.exe Microsoft Corporation 808
C:\LINDOWS\system32\svchost.exe Microsoft Corporation 856
C:\LINDOWS\system32\svchost.exe Microsoft Corporation 892
Subscribe to:
Posts (Atom)