Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用格式表中的PowerShell表达式可以获得多大的表现力?_Powershell - Fatal编程技术网

使用格式表中的PowerShell表达式可以获得多大的表现力?

使用格式表中的PowerShell表达式可以获得多大的表现力?,powershell,Powershell,我有以下脚本,用于输出用户Exchange邮箱的颜色编码文件夹层次结构。如果超过某个阈值(本例中为20 MB),则以红色输出该行,否则以灰色输出 #Get Folder Size Breakdown to Table with Color Coding get-mailbox $username | Get-MailboxFolderStatistics | ft @{ Name="Name" Expression= { $

我有以下脚本,用于输出用户Exchange邮箱的颜色编码文件夹层次结构。如果超过某个阈值(本例中为20 MB),则以红色输出该行,否则以灰色输出

#Get Folder Size Breakdown to Table with Color Coding
get-mailbox $username |
Get-MailboxFolderStatistics |
ft @{
        Name="Name"
        Expression=
        {
            $prefix=""
            foreach($c in $_.FolderPath.ToCharArray())
            {
                if($c -eq '/'){$prefix+='-'}
            }
            if($_.FolderSize -gt 20MB)
            {
                $Host.UI.RawUI.ForegroundColor = "Red"
            } else
            { 
                $Host.UI.RawUI.ForegroundColor = "Gray"
            }
            $prefix + $_.Name
        }
    },
    FolderSize,
    FolderandSubfolderSize
这个脚本有一些问题

如果最后处理的文件夹大于20 MB,则“我的控制台”文本在运行后仍保持红色。 此脚本假定原始控制台文本为灰色。如果不是灰色,那么我已经更改了用户的控制台文本。 如果不在
格式表
表达式的上下文中,这两个问题都很容易解决,但我无法确定在这种特殊情况下是否有可能解决这些问题。以下是我尝试过的要点,但它不起作用。(实际上,我试过20种不同的变体)


注:其目的是最终将其压缩为一个衬垫。我知道我可以在启动管道之前存储变量,并在管道完成后恢复颜色,但这会让它失去乐趣/烦恼。我更好奇的是,我是否能够在不改变管道基本结构的情况下完成这项工作。

我认为这是不可能的。基本上,每次
格式表
读取
名称
的表达式时,前景色都会改变。但是
Format Table
可能不会立即写出该表达式中的值,因此无法重置表达式中的颜色

我想你得把管道包装起来:

$originalColor = $Host.UI.RawUI.ForegroundColor

get-mailbox $username |
Get-MailboxFolderStatistics |
ft @{
        Name="Name"
        Expression=
        {
            $prefix = " " * (($_.FolderPath -split '/').Length)
            $Host.UI.RawUI.ForegroundColor = if($_.FolderSize -gt 20MB) { "Red" } else { $originalColor }
            $prefix + $_.Name
        }
    },
    FolderSize,
    FolderandSubfolderSize

$Host.UI.RawUI.ForegroundColor = $originalColor
另一种选择是编写自己的格式代码,找到每列的最大大小,然后使用
write Host
写出内容:

$stats = get-mailbox $username |
    Get-MailboxFolderStatistics |

$nameMaxWidth = 0
$sizeMaxWidth = 0
$subFolderSizeMaxWidth = 0
$stats | ForEach-Object {

    if( $_.Name.Length -gt $nameMaxWidth )
    {
        $nameMaxWidth = $_.Name.Length + (($_.FolderPath -split '/').Length - 1)
    }

    $sizeWidth = $_.FolderSize.ToString().Length
    if( $sizeWidth -gt $sizeMaxWidth )
    {
        $sizeMaxWidth = $sizeWidth
    }

    $subSizeWidth = $_.FolderAndSubFolderSize.ToString().Length
    if( $subSizeWidth -gt $subFolderSizeMaxWidth )
    {
        $subFolderSizeMaxWidth = $subSizeWidth
    }
}

$stats | ForEach-Object {
    $colorParam = @{ }
    if( $_.FolderSize -gt 20MB )
    {
        $colorParam.ForegroundColor = 'Red'
    }

    $prefix = ' ' * (($_.FolderPath -split '/').Length - 1)
    Write-Host ("{0}{1,$nameMaxWidth}" -f $prefix,$_.Name) -NoNewLine @colorParam
    Write-Host "  " -NoNewline
    Write-Host ("{0,-$sizeMaxWidth}" -f $_.FolderSize) -NoNewLine
    Write-Host "  " -NoNewLine
    Write-Host ("{0,-$subFolderSizeMaxWidth}" -f $_.FolderAndSubFolderSize)
}

我认为这是不可能的。基本上,每次
格式表
读取
名称
的表达式时,前景色都会改变。但是
Format Table
可能不会立即写出该表达式中的值,因此无法重置表达式中的颜色

我想你得把管道包装起来:

$originalColor = $Host.UI.RawUI.ForegroundColor

get-mailbox $username |
Get-MailboxFolderStatistics |
ft @{
        Name="Name"
        Expression=
        {
            $prefix = " " * (($_.FolderPath -split '/').Length)
            $Host.UI.RawUI.ForegroundColor = if($_.FolderSize -gt 20MB) { "Red" } else { $originalColor }
            $prefix + $_.Name
        }
    },
    FolderSize,
    FolderandSubfolderSize

$Host.UI.RawUI.ForegroundColor = $originalColor
另一种选择是编写自己的格式代码,找到每列的最大大小,然后使用
write Host
写出内容:

$stats = get-mailbox $username |
    Get-MailboxFolderStatistics |

$nameMaxWidth = 0
$sizeMaxWidth = 0
$subFolderSizeMaxWidth = 0
$stats | ForEach-Object {

    if( $_.Name.Length -gt $nameMaxWidth )
    {
        $nameMaxWidth = $_.Name.Length + (($_.FolderPath -split '/').Length - 1)
    }

    $sizeWidth = $_.FolderSize.ToString().Length
    if( $sizeWidth -gt $sizeMaxWidth )
    {
        $sizeMaxWidth = $sizeWidth
    }

    $subSizeWidth = $_.FolderAndSubFolderSize.ToString().Length
    if( $subSizeWidth -gt $subFolderSizeMaxWidth )
    {
        $subFolderSizeMaxWidth = $subSizeWidth
    }
}

$stats | ForEach-Object {
    $colorParam = @{ }
    if( $_.FolderSize -gt 20MB )
    {
        $colorParam.ForegroundColor = 'Red'
    }

    $prefix = ' ' * (($_.FolderPath -split '/').Length - 1)
    Write-Host ("{0}{1,$nameMaxWidth}" -f $prefix,$_.Name) -NoNewLine @colorParam
    Write-Host "  " -NoNewline
    Write-Host ("{0,-$sizeMaxWidth}" -f $_.FolderSize) -NoNewLine
    Write-Host "  " -NoNewLine
    Write-Host ("{0,-$subFolderSizeMaxWidth}" -f $_.FolderAndSubFolderSize)
}

谢谢我很感激你清理了我原来的表达方式——现在更简洁了。谢谢。我很感激你清理了我原来的表达方式——现在更简洁了。