Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.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,命令$output=Invoke表达式“cfn lint*.yaml”为$output提供了一个数组: $output.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[]

命令
$output=Invoke表达式“cfn lint*.yaml”
$output
提供了一个数组:

$output.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
Write Output$Output
为我提供了这个漂亮的输出:

Write-Output $output
W2506 Parameter AMIID should be of type [AWS::EC2::Image::Id, 
AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>]
trace3-ec2-virtual-machine-creation.yaml:27:3

W2001 Parameter VpcId not used.
trace3-ec2-virtual-machine-creation.yaml:32:3
为什么要这样做


我知道我可以通过调用
Write Host($output | Out String)
来解决这个问题,但我不明白为什么这样做。

Write output
[-InputObject]
作为参数,而
Write Host
[-Object]]
作为参数

返回作为输入提交的对象

将对象发送到主机。它不返回任何对象。但是,主机将显示写入主机发送给它的对象

如您所见,
Write-Output
PSObject
数组作为输入参数,并返回与
输出相同的
数组

Out-String
Write-Host
配合良好,因为它正在将数组转换为一个
多行字符串
,因此
Write-Host
会将它作为文本返回到信息流,而没有
Out-String
则会将数组的所有行合并为一个字符串行字符串

PS /> $test = @(
    'this is a'
    'test'
)

$writeHost = Write-Host -Object $test 6>&1
$writeOutput = Write-Output -Object $test

$writeHost.GetType()
$writeOutput.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                       
-------- -------- ----                                     --------                                                                                                       
True     False    InformationRecord                        System.Object                                                                                                  
True     True     Object[]                                 System.Array
编辑 根据您的评论,我不知道为什么,我无法找到有关cmdlet的MS文档的任何相关信息,但我们可以假设它正在对输入对象进行某种操作。我们知道
写主机
写信息
的包装器

以该函数为例,它接收一个
[object]
,在这种情况下,它接收一个
数组
,其中有两个项目作为输入,并将其输入的
字符串
表示形式写入信息流:

$test = @(
    'this is a'
    'test'
)

function TestString ([object]$String) {
    Write-Information ([string]$String) -InformationAction Continue
}

PS /> TestString $test
this is a test

为什么Write Host会将数组中的所有行都转换成一行字符串?@PortMan我的假设是因为它是如何编码的,但我不能确定。有可能它的编码是为了去掉所有
新行
回车
,并将它们合并到一个单行字符串中。@PortMan我在我的答案中添加了一个编辑,希望有意义。
$test = @(
    'this is a'
    'test'
)

function TestString ([object]$String) {
    Write-Information ([string]$String) -InformationAction Continue
}

PS /> TestString $test
this is a test