【发布时间】:2021-12-14 19:54:10
【问题描述】:
我正在尝试接收每个排序步骤的详细消息并使用-Verbose 键运行我的脚本,但我没有收到来自条件(循环)的任何详细消息。只有当我在条件句之外调用它们时,我才会收到我的消息。请帮忙
[CmdletBinding()]
param()
set-location "\test folder"
$listOfItems = Get-ChildItem | Select-Object -Property Name, Mode, LastWriteTime, @{label = "FileSize (MB)";expression = {$_.Length/1024/1024}}
$bufferItem = 0
for ($i = $listOfItems.Length - 1; $i -eq 0; $i--) {
for ($j = 0; $i -lt $i; $j++) {
if ($listOfItems[$j]."FileSize (MB)" -gt $listOfItems[$j + 1]."FileSize (MB)") {
Write-Verbose -Message "Transfer the value of the buffer variable to the element below the list"
continue
}
elseif ($listOfItems[$j]."FileSize (MB)" -lt $listOfItems[$j + 1]."FileSize (MB)") {
$bufferItem = $listOfItems[$j]
$listOfItems[$j] = $listOfItems[$j + 1]
$listOfItems[$j + 1] = $bufferItem
}
Write-Verbose -Message "Transfer the value of the buffer variable to the element below the list"
}
}
【问题讨论】:
-
冗长的东西默认是关闭的。你有没有在某个地方打开它? [咧嘴]
-
这里有一个快速的演示来证明它在原则上是有效的:
& { [CmdletBinding()] param() foreach ($i in 1..2) { Write-Verbose $i } } -Verbose
标签: powershell conditional-statements verbose