[英]Different output by Write-Host and Write-Output to console by PowerShell
Below is a small PowerShell script. 下面是一个小的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
Output is System.Collections.Hashtable . 输出为System.Collections.Hashtable 。
But if Write-Host is replaced by Write-Output then, 但是,如果将Write-Host替换为Write-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
Output is 输出是
Name Value
---- -----
name vindhya
id 182122
Why are Write-Host and Write-Output behaving differently? 为什么写主机和写输出的行为不同?
Write-Output
will pass the output to the next step of the pipeline. Write-Output
将把Write-Output
传递到管道的下一步。 If you are at the end of the pipeline, then it will output to the console. 如果您位于管道的末端,那么它将输出到控制台。
Write-Host
will output to the console. Write-Host
将输出到控制台。 If the output target is an object, it will call the toString()
method to convert the object to a string and then output it. 如果输出目标是对象,它将调用toString()
方法将对象转换为字符串,然后将其输出。 Usually the string is the type name of the object. 通常,字符串是对象的类型名称。
You can add another cmdlet, Out-String , in your code and then Write-Host
would output similar content as Write-Output
: 您可以在代码中添加另一个cmdlet Out-String ,然后Write-Host
将输出与Write-Output
类似的内容:
Write-Host ($hash_params | Out-String)
My test as below: 我的测试如下:
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
The output is: 输出为:
System.Collections.Hashtable
Name Value
---- ----
id 18250
name vindhya
Write-Object sends objects thru PowerShell's formatting engine. Write-Object通过PowerShell的格式引擎发送对象。 This involves checking if there is a formatdata file with formatting instructions for the object's type. 这涉及检查是否存在带有针对对象类型的格式化指令的formatdata文件。 The formatting file can choose various different default display formats: table, list, wide, etc. If there is no formatting data for the object, PowerShell uses other criteria like number of public properties to determine whether to use table view or list view. 格式设置文件可以选择各种不同的默认显示格式:表格,列表,宽屏等。如果对象没有格式设置数据,PowerShell将使用其他条件(例如,公共属性数)来确定是使用表格视图还是列表视图。 As a last resort, it will try to coerce to a string usually using the object's ToString() method. 作为最后的手段,它将尝试使用对象的ToString()方法强制转换为字符串。
Write-Host does none of this (except the last part about coercing to a string). Write-Host不执行任何操作(关于强制转换为字符串的最后一部分除外)。 It just displays the strings you provide it. 它只是显示您提供的字符串。 If you provide something that isn't a string, it attempts a simple coercion to string and that is it. 如果您提供的不是字符串,它会尝试对字符串进行简单的强制转换。 Often this results in just the type name of the object. 通常,这只会导致对象的类型名称。
I've faced this with PowerShell. 我已经在PowerShell中遇到了这个问题。 I believe writing objects is not usually well-accomplished using Write-Host - it appears better suited for strings. 我相信使用Write-Host通常无法很好地完成对象的编写-它似乎更适合于字符串。 If you want to write objects, Write-Output is what you need to use. 如果要编写对象,则需要使用Write-Output。
From the Powershell documentation , Write-Output is used to "pipe objects through to the next command in the pipeline." 根据Powershell文档 ,Write-Output用于“将对象通过管道传递到管道中的下一个命令”。 Since there's no "next command", Write-Output is printing the object on the console. 由于没有“下一个命令”,Write-Output在控制台上打印对象。
Also, simply writing $objName
appears to call Write-Output behind the scenes. 同样,简单地写入$objName
似乎会在后台调用Write-Output。 Thus, if you wished to return your hashtable you'd do 因此,如果您希望返回哈希表,则可以
function Foo {
# Generate $hash_params
$hash_params
return
}
And this would pass $hash_params
to whatever is calling Foo
. 这会将$hash_params
传递给任何调用Foo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.