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 PowerShell. Show all posts
Showing posts with label PowerShell. 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.
backtick for dollar sign
to escape the dollar sign using a backtick
PS> $a = "hello"
PS> $b = "`$a there"
PS> $b
$a there
PS> $a = "hello"
PS> $b = "`$a there"
PS> $b
$a there
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
access the certification store
PS> get-childitem cert:
Location : CurrentUser
StoreNames : {UserDS, AuthRoot, CA, Trust...}
Location : LocalMachine
StoreNames : {AuthRoot, CA, Trust, Disallowed...}
PS> get-childitem cert:\CurrentUser\CA
It shows 'Thumbprint', 'Subject' headers.
The Subject header has 'CN=Root Agency', MS WHC, VeriSign Class 2 etc.
Location : CurrentUser
StoreNames : {UserDS, AuthRoot, CA, Trust...}
Location : LocalMachine
StoreNames : {AuthRoot, CA, Trust, Disallowed...}
PS> get-childitem cert:\CurrentUser\CA
It shows 'Thumbprint', 'Subject' headers.
The Subject header has 'CN=Root Agency', MS WHC, VeriSign Class 2 etc.
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
{
...
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)...}
write-warning = write-verbose = yellow
to display the message
PS> $WarningPreference = "Continue"
Write-Warning "Stopping SQL Service..."
or,
PS> $VerbosePreference = "Continue"
Write-Verbose -message "SQL Service has been started. You may proceed..."
(Yellow on Black if powershell.exe in V1.0 or above)
(Black on White if gpowershell.exe in V2CTP2)
(Black on White if powershell_ise.exe in V2CTP3)
PS> $WarningPreference = "Continue"
Write-Warning "Stopping SQL Service..."
or,
PS> $VerbosePreference = "Continue"
Write-Verbose -message "SQL Service has been started. You may proceed..."
(Yellow on Black if powershell.exe in V1.0 or above)
(Black on White if gpowershell.exe in V2CTP2)
(Black on White if powershell_ise.exe in V2CTP3)
Labels:
gpowershell,
Graphical Windows PowerShell,
PowerShell,
powershell_ise,
V1RTM,
V2CTP2,
V2CTP3
Monday, January 19, 2009
two nested for-loops
to display the progress of two nested for-loops
PS> for ($i = 1; $i -lt 101; $i++ )
{for ($j=0;$j -lt 10000;$j++) {} write-progress -activity "Search in Progress" -status "% Complete:" -percentcomplete $i;}
Explanation: The Write-Progress command includes a status bar heading (-activity), a status line, and the variable, $i (the counter in the for-loop), that indicates the relative completeness of the task. The second for-loop is the time counter.
The characters 'Search in Progress', '% Complete:' and repeating 'o' are shown in yellow on a teal progress bar. Graphical Windows PowerShell (V2CTP2) won't see this animation.
PS> for ($i = 1; $i -lt 101; $i++ )
{for ($j=0;$j -lt 10000;$j++) {} write-progress -activity "Search in Progress" -status "% Complete:" -percentcomplete $i;}
Explanation: The Write-Progress command includes a status bar heading (-activity), a status line, and the variable, $i (the counter in the for-loop), that indicates the relative completeness of the task. The second for-loop is the time counter.
The characters 'Search in Progress', '% Complete:' and repeating 'o' are shown in yellow on a teal progress bar. Graphical Windows PowerShell (V2CTP2) won't see this animation.
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.
stop a particular instance of the Notepad process by its ID
PS> stop-process -id 3952 -confirm -passthru
Explanation: ` -confirm ` parameter prompts the user before stopping the process. Because the prompt includes the process name, as well as its ID, this is best practice. ` -passthru ` parameter passes the process object to the formatter for display. (This is not applicable to graphical powershell)
Explanation: ` -confirm ` parameter prompts the user before stopping the process. Because the prompt includes the process name, as well as its ID, this is best practice. ` -passthru ` parameter passes the process object to the formatter for display. (This is not applicable to graphical powershell)
stop all instances of the Notepad process
PS> stop-process -name Notepad
Explanation: (Each instance of Notepad runs in its own process.) It uses the Name parameter to specify the processes, all of which have the same name. If you were to use the ID parameter to stop the same processes, you would have to list the process IDs of each instance of Notepad.
Explanation: (Each instance of Notepad runs in its own process.) It uses the Name parameter to specify the processes, all of which have the same name. If you were to use the ID parameter to stop the same processes, you would have to list the process IDs of each instance of Notepad.
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.
Subscribe to:
Posts (Atom)