Help with function, output not as expected

I would appreciate any help I can get on this, it’s not working the way I want and I can’t figure out why as it seems like it should. I’m new to powershell, just finished up the Powershell in a Month of Lunches book and decided to write a little module to help me out with some day to day tasks.

Here is the code:

The part that queries the computers for logged on user and last boot time works just fine and returns results as I would expect. It’s the error handling that catches computers that are unreachable, and should output them at the end that isn’t working. Where I would expect it to look something like:



I just get blank space… it’s like it’s there, but hidden.

You’re not appending to the array correctly.

Define $errorPC = @() to create an empty array. To add something to it, it’s just $errorPC += $computer - you don’t need the comma in there. That said, I’m not entirely clear why you’re using an array at all. And all you’re doing in your END block is passing computer names to an ErrorHandling function, which isn’t doing anything at all.

You might look into the book’s sequel - Learn PowerShell Toolmaking in a Month of Lunches - as that’s where you’re now playing ;).

I was using an array because it could potentially have many computer names contained in it since I could feed it a list, or pipe a list to it, so I assumed I need the array functionality…

I guess I don’t understand why my ErrorHandling function is doing nothing. I assumed it would create PSObjects and output them similarly to how it looks when the GetComputerInfoWork function runs. I could just as easily put Write-Output $pc is unreachable into the END block instead… which does work… but then the output looks like:

NOTONLINE is unreachable
NOTONLINE2 is unreachable
NOTONLINE3 is unreachable

I was just trying to clean it up a little bit and put an Unreachable heading with all the unreachable computers underneath it.

So, the pipeline is really intended to accumulate output for you, meaning it kind of is an array already.

ErrorHandling is creating PSObjects and writing them to the pipeline. The problem is that you’re trying to stuff two different types of objects into the pipeline, right? Get-PCInfo only gets one output pipeline, but you’re outputting both your custom objects and the output of GetComputerInfoWork. When you jam two object types into the pipeline at once, the formatting system goes a bit berserk. So that’s one consideration.

Now in your case, it’s a little goofier for the shell, because you have two functions outputting objects of type PSObject, but they’re very different looking. So the formatting system won’t deal well with that at all.

The fact that you’re not outputting the unreachable ones until the end doesn’t really change the shell’s confusion.

You ideally want to output ONE kind of object. So perhaps that would be an object having a ComputerName property, and a Reachable property which is either True or False. It might then have some other properties, which might be empty if Reachable is False for that computer. For me, I’d do all of that in a single function, rather than having the two “helper” functions you have. You’re actually complicating the error handling the way you’re doing it, because you’re crossing scopes with error context. Totally legal, but harder to keep track of, and there are some gotchas.

I think it’s more a design thing. One function doesn’t really output two “tables” of information - not without a lot of custom formatting work, which is what the Dir command has going on under the hood, because it has to display File and Folder objects on one view.

That makes sense, I understand now. Thank you for the explanation.