What's the difference between WriteObject(x, true) and multiple writeobjects when writing a PowerShell cmdlet?

早过忘川 提交于 2019-12-07 16:29:29

问题


I want to write a cmdlet that reads multiple records from a database and puts them onto the pipeline.

I think I can do either a single WriteObject(Enumerable<rec>, true) or I can loop myself and call WriteObject multiple times.

What's the difference between these two?


回答1:


Here is the documentation: Cmdlet.WriteObject Method (Object, Boolean)

And here is the example:

# Writes objects one by one
function Test1
{
    [CmdletBinding()]param()
    $data | %{ $PSCmdlet.WriteObject($_) }
}

# Writes the collection and allows the core to enumerate it one level.
# Numbers of written objects is the collection item count.
function Test2
{
    [CmdletBinding()]param()
    $PSCmdlet.WriteObject($data, $true)
}

# Writes the collection as a single object.
# Numbers of written objects is 1.
function Test3
{
    [CmdletBinding()]param()
    $PSCmdlet.WriteObject($data, $false)
}

function Test
{
    (Test1).GetType().Name
    (Test2).GetType().Name
    (Test3).GetType().Name
}

$data = New-Object System.Collections.ArrayList

Write-Host "1 item"
$null = $data.Add('hello')
Test

Write-Host "2+ items"
$null = $data.Add('world')
Test

Output:

1 item
String
String
ArrayList
2+ items
Object[]
Object[]
ArrayList

Thus, calling WriteObject(item) for each item in a collection is basically the same as WriteObject(items, true); in both cases the collection itself has gone.

WriteObject(items, false) is different; it returns a reference to the collection and the caller can use that effectively depending on a scenario. For example, if a collection is a DataTable object (not unrolled set of DataRow items) then a caller can operate on DataTable members of the returned object.




回答2:


Well, WriteObject(Object, boolean) will allow you to output a collection and have it stay intact (if called with "false" for the second argument). Normally PowerShell will enumerate any collections that get put on the pipeline.

So you could output a string array and the result would be of type [String[]]. While if you let PowerShell unwrap it, it will be an array of strings in a [Object[]].

You can also call that overload with "true" and it will be just like a loop calling WriteObject(Object).



来源:https://stackoverflow.com/questions/6103389/whats-the-difference-between-writeobjectx-true-and-multiple-writeobjects-whe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!