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:
$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
}
Run Result: