PowerShell, as a programmable language, has the following loop statement.
Note: This section of the content to discuss the substance more biased in the design aspect, so do not do too much detail in this, only for the application of PowerShell specific explanation.
for (initial value; expression; assignment statement) {code} controlling execution times with variable values
foreach (member variable in array) {code} execute code with iteration
Foreach-object performs an operation on each object of a set of inputs
while (expression) {code} expression is true loop code execution
do {code} while (expression) is similar to while, only executes the code first, then judges the expression true and false
do {code} until (expression) executes code until the expression is false
The application of circular statement in PowerShell
Use foreach to query hardware information
Example one:
Copy Code code as follows:
$DiskDrive =get-wmiobject-class win32_diskdrive-namespace root\cimv2
foreach ($item in $DiskDrive)
{
Write-host "Description:" $item. Description
Write-host "Device ID:" $item. DeviceID
Write-host "Interface Type:" $item. InterfaceType
Write-host "Media Type:" $item. MediaType
Write-host "Model:" $item. Model
Write-host "Partitions:" $item. Partitions
Write-host "Size:" $item. Size
Write-host "Status:" $item. Status
}
Case TWO:
Copy Code code as follows:
$Processor =get-wmiobject-class win32_processor-namespace root\cimv2
foreach ($item in $Processor)
{
Write-host "Caption:" $item. Caption
Write-host "CPU Status:" $item. Cpustatus
Write-host "Current Clock Speed:" $item. CurrentClockSpeed
Write-host "Device ID:" $item. DeviceID
Write-host "L2 Cache Size:" $item. L2cachesize
Write-host "L2 Cache Speed:" $item. L2cachespeed
Write-host "Name:" $item. Name
}
Run Result:
Monitoring process status with while
Copy Code code as follows:
Notepad
while (Get-process-name Notepad | Select-property responding) {}
$time = Get-date
Write-host "The Notepad failed to respond on: $time"
In this case, if the process Notepad appears unresponsive, the screen output is generated.
Use do-while expression:
Copy Code code as follows:
Notepad
do{}
while (Get-process-name Notepad | Select-property responding)
$time = Get-date
Write-host "The Notepad failed to respond on: $time"
Interacting with Do Until
Copy Code code as follows:
Todo
{
"Quit now?" (y/n) "
$input =read-host
}
Until ($input-eq "Y")
Run Result:
format output using Foreach-object
The following data is manipulated,
D00454798106276487326471 Reed built 829.51
Q00136284503715856294375 Zhang 712.65
H00374967692981018226574 Liu Ximing 891.31
R00759861215965098103878 Zhaozilong 898.21
J00741245626115645970139 Yang Gaoyuan-13.21
K00142545764587219409172 Zhou Ming 647.41
P00103851828756182786938 Zhang Long-27.51
The output is formatted as follows:
1|454798106276487326471| Li Dejian |829.51
2|136284503715856294375| Zhang |712.65
3|374967692981018226574| Liu Ximing |891.31
4|759861215965098103878| Zhaozilong |898.21
5|741245626115645970139| Yang Gaoyuan |0.00
6|142545764587219409172| Zhou Ming |647.41
7|103851828756182786938| Zhang Long |0.00
Subtotal |3979.09
Use a regular expression for each data member using Foreach-object, and finally format the output:
Copy Code code as follows:
${c:\test.txt} | `
foreach-object{$total =0 $id = 1} '
{
[void] ($_-match ' ^. {3} (? <id>\d+) (? <name>[\p{iscjkunifiedideographs}]+) (? <salary>[\d.] *)');
$ofs = ' | ';
"$ ($id; $id + +; $matches. id; $matches. Name; {0:F2} '-f [float] $matches. salary) ';
$total + + $matches. Salary
}`
{' t subtotal ' t ' t| $total}
Run Result:
Welcome to make comments and suggestions!