Passing Objects between functions and Export-CSV

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
azeiler
Posts: 12
Last visit: Thu Jan 20, 2022 4:44 pm

Passing Objects between functions and Export-CSV

Post by azeiler »

I have been getting into the object oriented nature of Powershell recently, and ran into an issue.

I have created a custom class and wrote a script that instantiates that class and populates its properties. It will then do that a number of times based on elements in an array. Lastly, I used Export-CVS to get the properties of all those objects into a CSV. It worked great.

But then I thought it would be more efficient to have another function creates a new object of the right class, populates all the values, and then returns that object to the main script. It seems to work OK from within that function, but when I return the object and try to Export-CSV, all I get is a single column with the "length" property. Is there something about passing an object between functions that causes this problem?

I can attach relevant bits of code if requested, I just figured I would start by conceptually explaining my issue.

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing Objects between functions and Export-CSV

Post by jvierra »

Without an example the question cannot be answered. Attach a simple example of your issue and we will look at it. It is likely you are not returning the correct object type or that enumeration is converting the object to a string.

A common cause of this behavior is an unintended result in a function is outputting a string as the first object returned. The Csv converter creates the Csv structure based on the type of the first object in the collection.
azeiler
Posts: 12
Last visit: Thu Jan 20, 2022 4:44 pm

Re: Passing Objects between functions and Export-CSV

Post by azeiler »

Here is the function, amended for propriety sake. It takes an array which is basically each line of a block of text, extracts information from the text, and sets that information to the properties of a new object of type ZClass. When I use this same basic code in my script without making it a separate function, and set several of the objects it creates to an array to Export-CSV, it works fine. But when I make it a function as posted here, Export-CSV just gives me a list of length values.
  1. function Get-XClassData()
  2. {
  3.     [CmdletBinding()]
  4.     param (
  5.         [Parameter(Mandatory = $True, ValueFromPipeline = $True)]
  6.         [object[]]$XObjectText
  7.     )
  8.    
  9.     BEGIN
  10.     {
  11.         $ANames = @(...)
  12.         $ANames = @(...)
  13.         $CNames = @(...)
  14.         $DNames = @(...)
  15.         $ENames = @(...)
  16.         $FNames = @(...)
  17.         $GCNames = @(...)
  18.     }
  19.     PROCESS
  20.     {
  21.         $LineContent = $XObjectText[0] -split '"'
  22.         $XClass = New-Object ZClass
  23.         $XClass.ClassName = $LineContent[1]
  24.        
  25.         $XObjectText | %{
  26.             If ($_.SubString(0, [math]::min(14, $_.length)) -eq '...')
  27.             {
  28.                 $LineContent = $_ -split '"'
  29.                 $XClass.ClassDescription = $LineContent[1]
  30.             }
  31.             If ($_ -like "*...*")
  32.             {
  33.                 $LineContent = $_ -split '"'
  34.                 switch ($LineContent[1])
  35.                 {
  36.                     { $ANames -contains $LineContent[1] }
  37.                     { $XClass.ABlocks++ }
  38.                     { $BNames -contains $LineContent[1] }
  39.                     { $XClass.BBlocks++ }
  40.                     { $CNames -contains $LineContent[1] }
  41.                     { $XClass.CBlocks++ }
  42.                     { $DNames -contains $LineContent[1] }
  43.                     { $XClass.DBlocks++ }
  44.                     { $ENames -contains $LineContent[1] }
  45.                     { $XClass.EBlocks++ }
  46.                     { $FNames -contains $LineContent[1] }
  47.                     { $XClass.FBlocks++ }
  48.                     { $GCNames -contains $LineContent[1] }
  49.                     { $XClass.GBlocks++ }
  50.                 }
  51.             }
  52.         }
  53.        
  54.         $XClass.GetClassType()
  55.        
  56.         Write-Output $XClass
  57.     }
  58.     END { }
  59.  
  60. ForEach(...)
  61. {
  62. $XClass = Get-XClassData $XObjectText
  63. $Classes += $XClass
  64. }
  65.  
  66. $Classes | Export-Csv "c:\test_output.csv" -NoTypeInformation
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing Objects between functions and Export-CSV

Post by jvierra »

We need real code and not pseudo code. It should be attached here as a PS1 file that runs and demonstrates th issue you are having. As posted the code is not useable. The code is full of syntax errors.
azeiler
Posts: 12
Last visit: Thu Jan 20, 2022 4:44 pm

Re: Passing Objects between functions and Export-CSV

Post by azeiler »

jvierra wrote: Fri May 03, 2019 9:13 am We need real code and not pseudo code. It should be attached here as a PS1 file that runs and demonstrates th issue you are having. As posted the code is not useable. The code is full of syntax errors.
It's full of syntax errors because I removed code that I am not allowed to post on the internet. When I tried to create a similar (working) function to post here, everything worked fine. Can you please try to help me without the specific code?

I have attached two screenshots. The first is the properties of my class based objects while still inside the function. The second is the parameters after I write-output to a parameter outside the function. You can see that just this write-output causes it to lose its type and all its specific values.

https://ibb.co/3TcGwPV
https://ibb.co/GTRszn5
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing Objects between functions and Export-CSV

Post by jvierra »

Please create a copy of your PS1 file that demonstrates your issue. We cannot help you with broken code or pictures of code.

It is also possible that you will see your own error when creating a simple version of your code. YOu need a basic class and the method you are calling the function. You have most of it just make it work. There is no need for proprietary data. Just change all data to be dummy data.

Also check all of the issues I listed in my first response.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing Objects between functions and Export-CSV

Post by jvierra »

/Two things I can tell you immediately:
1. The following line will output a string into your collection and that string will always be the first item in the collection.

Code: Select all

      $XClass.GetClassType()
2. When returning results from a function there is no need to use Write-Output.

Code: Select all

     Write-Output $XClass

Just output the object like this:
$XClass
azeiler
Posts: 12
Last visit: Thu Jan 20, 2022 4:44 pm

Re: Passing Objects between functions and Export-CSV

Post by azeiler »

I was able to fix this issue, though I'm not entirely sure I understand. Your suggestions seem to be related.

XClass contains a method which sets one of it's parameters. I originally had the method returning that parameter as well, thinking there might be cases where I want to save the parameter and/or use it once its been set. When I modified the method to not return anything, my issue was resolved.

When I called the method within my function, I think the value that method was returning was ultimately being written to the pipeline. Then I think Powershell was converting those items I was intending to write to the pipeline to a String, and Export-CSV was exporting the length of those "strings" to my file.

So my lesson learned: do not (even accidentally) write anything to the pipeline from a function, or the powershell will make all output from that function match the type of the initial output.

Thanks for your help Jvierra!
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked