【问题标题】:Different output by Write-Host and Write-Output to console by PowerShellWrite-Host 的不同输出和 PowerShell 到控制台的 Write-Output
【发布时间】:2015-10-12 22:29:45
【问题描述】:

下面是一个小的 PowerShell 脚本。

function test() {
    $paramstring = "name=vindhya 
    id=182122"
    $hash_params = convertfrom-stringdata -stringdata $paramstring
    Write-Host $hash_params
    callee $hash_params
}


function callee() {
    param($hash_params)
    #$hash_params
}

test

输出是 System.Collections.Hashtable

但是如果Write-HostWrite-Output替换了,那么,

function test() {
    $paramstring="name=vindhya 
    id=182122"
    $hash_params = convertfrom-stringdata -stringdata $paramstring
    Write-Output $hash_params
    callee $hash_params
}


function callee() {
    param($hash_params)
    #$hash_params
}

test

输出是

Name                           Value

----                           -----

name                           vindhya

id                             182122

为什么 Write-Host 和 Write-Output 的行为不同?

【问题讨论】:

    标签: powershell io


    【解决方案1】:

    Write-Output 会将输出传递到管道的下一步。如果你在管道的末端,那么它将输出到控制台。

    Write-Host 将输出到控制台。如果输出目标是一个对象,它会调用toString()方法将对象转换成字符串然后输出。通常字符串是对象的类型名。

    您可以在代码中添加另一个 cmdlet Out-String,然后 Write-Host 将输出与 Write-Output 类似的内容:

    Write-Host ($hash_params | Out-String)
    

    我的测试如下:

    function test() {
        $paramstring = "name=vindhya
        id=18250"
        $hash_params = convertfrom-stringdata -stringdata $paramstring
        Write-Output $hash_params.toString()
        Write-Host ($hash_params | Out-String)
        callee $hash_params
    }
    
    
    function callee() {
        param($hash_params)
        #$hash_params
    }
    
    test
    

    输出是:

    System.Collections.Hashtable
    
    Name        Value
    ----        ----
    id          18250
    name        vindhya
    

    【讨论】:

      【解决方案2】:

      Write-Object 通过 PowerShell 的格式化引擎发送对象。这涉及检查是否存在带有对象类型格式说明的格式数据文件。格式化文件可以选择各种不同的默认显示格式:表格、列表、宽等。如果对象没有格式化数据,PowerShell 使用其他标准(如公共属性的数量)来确定是使用表格视图还是列表视图。作为最后的手段,它会尝试强制转换为通常使用对象的 ToString() 方法的字符串。

      Write-Host 什么都不做(除了关于强制字符串的最后一部分)。它只显示您提供的字符串。如果您提供的不是字符串,它会尝试对字符串进行简单的强制转换,仅此而已。通常这只会产生对象的类型名称。

      【讨论】:

        【解决方案3】:

        我在使用 PowerShell 时遇到过这种情况。我相信使用 Write-Host 编写对象通常不能很好地完成 - 它似乎更适合字符串。如果你想写对象,Write-Output 就是你需要使用的。

        From the Powershell documentation,Write-Output 用于“将对象通过管道传递到管道中的下一个命令”。由于没有“下一条命令”,Write-Output 正在控制台上打印对象。

        另外,简单地写$objName 似乎在幕后调用Write-Output。因此,如果你想返回你的哈希表,你会这样做

        function Foo {
            # Generate $hash_params
            $hash_params
            return
        }
        

        这会将$hash_params 传递给调用Foo 的任何对象。

        【讨论】:

        • 这个答案回答了我的另一个问题,即如果只写一个变量就可以使用写输出..
        • 谢谢! Powershell 有点令人生畏,特别是如果你是从 Bash 进来的;但它与 Windows 的挂钩可以提供大量的自动化功能。
        猜你喜欢
        • 2023-01-25
        • 2020-03-08
        • 1970-01-01
        • 1970-01-01
        • 2012-02-04
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多