0

I have an array named $RolesFunArray. It contains one or more elements like this:

ZPP01 0  
ZPP02 1   
ZPP03 1  

All arrays are system.string except for $RolesFunArray which is system.object.

I am trying to output this variable to a text file. It always shows the object type and not the string. I have tried -join, [string]$RolesFunArray and several other things and none of them work (no errors, just more System.Object[]).

Basically, I am reading a larger file line by line, grabbing some text, matching that text against another file and then picking text from the two files and writing them together to that $Line variable, which I then am outputting to a new line in a third file. I want tabs between the variables in the $Line output.

Any ideas? Thanks!

Here is what I am using to output.

$Line = $FirstWord[1] + ([char]0x0009) + $TstcSplit[6] + ([char]0x0009) + $RolesFunArray

add-content -Encoding ASCII -path "c:\sapdl\test\$File.log" -value "$Line'r" 
9
  • It might be difficult to reproduce your issue here as you have variables we have to assume their state. $FirstWord is a string? If not that would be an issue. That would be my first question since it would determine how the object is saved. Also, if $line was typed earlier it would persist that way throughout the code. $RolesFunArray should just be a space delimited string if cast as such. What is $FirstWord.GetType().Fullname Commented Apr 13, 2016 at 17:05
  • Also if you are building a tab delimited file might as well crate you own objects and use Export-CSV -delimiter "t"` Commented Apr 13, 2016 at 17:08
  • $FirstWord.GetType().Fullname reports as being a string. Commented Apr 13, 2016 at 19:04
  • Then the type of $line should be string as well. Is that the case? Commented Apr 13, 2016 at 19:05
  • $RolesFunArray.GetType().Fullname reports as system.object Commented Apr 13, 2016 at 19:06

1 Answer 1

0

The easiest way to do that is to:

$RolesForFunArray | Export-Csv out.csv

There are number of other functions to let you use xml, json etc. Otherwise you could simply try "$RolesForFunArray"

The output depends on type of object ofcourse, this works for String[] arrays. For other types you need to convert it via foreach loop item by item.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. I want to output the array as string into another variable ($Line) which then outputs to a file using that add-content line in the code I posted. Using quotes around the $RolesFunArray (still output system.object[]) did not work. I'll keep poking around as I am stuck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.