【问题标题】:Why Write-Verbose messages don't appear from conditions为什么 Write-Verbose 消息不会从条件中出现
【发布时间】: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


【解决方案1】:

您的内部循环条件存在错误:

for ($j = 0; $i -lt $i; $j++) {

由于$i -lt $i 永远不会计算为$true,因此内部循环体将永远不会执行,并且永远不会到达Write-Verbose 语句。

修复循环条件,您会发现Write-Verbose 在条件语句中工作得非常好。

【讨论】:

  • 天哪,对不起)谢谢
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 2023-02-21
  • 2016-08-31
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
相关资源
最近更新 更多