代码之家  ›  专栏  ›  技术社区  ›  Garrett

在通过PowerShell消息框显示的变量中添加新行

  •  0
  • Garrett  · 技术社区  · 7 年前

    有人有什么想法吗?

    $missing = Compare-Object $capture.BaseName $output.BaseName | Select-Object -ExpandProperty InputObject
    If($missing -ne $null){
    Write-Host 'Here are the missing file(s):'
        echo $missing
    
    #send pop up alert
    $ButtonType = [System.Windows.MessageBoxButton]::OK
    $MessageboxTitle = “Please Process Files”
    $Messageboxbody = “
    The following are missing:
    
    $missing”
    $MessageIcon = [System.Windows.MessageBoxImage]::Warning
    [System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$messageicon)
    }Else{
    }
    

    ISE中的输出如下所示:

    文件1

    文件2

    文件3

    文件1文件2文件3

    1 回复  |  直到 7 年前
        1
  •  2
  •   Dan Wilson    7 年前

    $missing 是字符串列表,所以 Echo 控制台负责在多行上格式化它们。

    MessageBox 要求使用换行符(ASCII 10)连接字符串。

    $([String]::Join(([Convert]::ToChar(10)).ToString(), $missing)
    

    该行使用 String.Join Method (System) [Convert]::ToChar(10) \n 但是使用它会导致使用文本字符串而不是换行符。我们只是将ASCII代码10转换为一个字符(然后是一个字符串),并使用它连接文件名。

    以下是更新后的脚本:

    $missing = Compare-Object $capture.BaseName $output.BaseName | Select-Object -ExpandProperty InputObject
    
    If($missing -ne $null){
        Write-Host 'Here are the missing file(s):'
            Echo $missing
    
        # Send pop up alert
    
        $missingWithNewlines = $([String]::Join(([Convert]::ToChar(10)).ToString(), $missing))
    
        $ButtonType = [System.Windows.MessageBoxButton]::OK
    
        $MessageboxTitle = “Please Process Files”
    
        $Messageboxbody = “
    The following are missing:
    
    $missingWithNewlines”
    
        $MessageIcon = [System.Windows.MessageBoxImage]::Warning
    
        [System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$messageicon)
    
    }Else{
    
        # Nothing missing
    
    }
    

    结果如下:

    MessageBox

    推荐文章