Explore PowerShell (10) Statement Loop

Source: Internet
Author: User

As a programmable language, PowerShell has the following loop statements.

Note: The content to be discussed in this section is more substantive in favor of programming. Therefore, we will not explain it in detail here, but will explain it in detail only for the applications in PowerShell.

• For (initial value; expression; Value assignment statement) {code} use variable value to control the number of executions
• Foreach (member variable in array) {code} uses iteration to execute code
• Foreach-object performs operations on each object of a group of inputs
• When the while (expression) {code} expression is true, the code is executed cyclically.
• Do {code} while (expression) is similar to while. It only executes the code first and then judges whether the expression is true or false.
• Do {code} until (expression) executes the code until the expression is false.

Application of cyclic statements in PowerShell

Use foreach to query hardware information

Example 1:Copy codeThe Code is 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
}

Example 2:

Copy codeThe Code is 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
}

Running result:

Use while to monitor Process statusCopy codeThe Code is 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 example, if the notepad process does not respond, screen output is generated.

Use do while expression:Copy codeThe Code is as follows: notepad
Do {}
While (get-process-name notepad | select-Property Responding)
$ Time = get-date
Write-Host "The Notepad failed to respond on: $ time"

Use do until for InteractionCopy codeThe Code is as follows: do
{
"Quit Now? (Y/N )"
$ Input = Read-Host
}
Until ($ input-eq "Y ")

Running result:

Use foreach-object for formatting output

Operate on the following data,

D00454798425276425326471 Li dejian 829.51
Q00136282133715856294375 Zhang Chunsheng 712.65
H00374967692981018226574 Liu Ximing 891.31
R00759861215965098103878 Zhao Zilong 898.21
J00741245626115645970139 Yang gaoyuan-13.21
K00142545764587219409172 Zhou Ming 647.41
P00102131828756182786938 Zhang Long-27.51

The output format is as follows:

1 | 454798106276487326471 | Li dejian | 829.51
2 | 136284503715856294375 | Zhang Chunsheng | 712.65
3 | 374967692981018226574 | Liu Ximing | 891.31
4 | 759861215965098103878 | Zhao Zilong | 898.21
5 | 741245626115645970139 | Yang gaoyuan | 0.00
6 | 142545764587219409172 | Zhou Ming | 647.41
7 | 103851828756182786938 | Zhang Long | 0.00
Subtotal | 3979.09

Use foreach-object to use a regular expression for each data member and format the output:

Copy codeThe Code is as follows: $ {C: \ test.txt} |'
Foreach-object {$ total = 0; $ id = 1 }'
{
[Void] ($ _-match '^. {3 }(? <Id> \ d + )(? <Name> [\ p {iscjkuniiedideographs}] + )(? <Salary> [\ d.] *) ');
$ Ofs = '| ';
"$ ($ Id; $ id ++; $ matches. id; $ matches. name; '{0: f2}'-f [float] $ matches. salary )";
$ Total + = $ matches. salary
}'
{"'T subtotal't | $ total "}

Running result:

Comments and suggestions are welcome!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.