1

I have following output from some PowerShell Cmdlet, as below

  0    server-1 Content  Damaged       client 12/06/2013 08:00:41
123    server-1 Content  Damaged       client 12/07/2013 08:00:33
  0    server-1 Content  Damaged       client 12/08/2013 08:00:32
234    server-1 Content  Damaged       client 12/09/2013 08:00:34
  0    server-1 Content  Damaged       client 12/09/2013 16:09:41
 70    server-1 Content  Damaged       client 12/10/2013 08:00:33
  0    server-1 Content  Damaged       client 12/11/2013 08:00:31

I'm able to append the above output into a text file.

When im trying to Import above ouput in a new CSV file(using Import-Csv), all infomation coming under a single cell in a each line,but i need 7 cells in each line !!! please somebody guide me here.

And also please tell me, How to cound a line which is starting from 0 using PowerShell ?

2 Answers 2

2

Import-CSV by default assumes that each record (or row) of data is separated by a comma (hence the name comma separated values). If you have something else which is used consistently as your delimiter between fields (such as tab or space), you can use the -Delimiter parameter for the cmdlet to specify that and get it to parse properly.

However, Import-CSV also assumes that you have a header record, which you do not. So you'll need to either parse your data using Get-Content instead, or add a header record (preferable).

Once you're parsing your data into a collection of objects (or really, a collection of collections), just look at the first value in each and see if it equals 0.

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

Comments

2

The data is not in CSV format, so Import-CSV will not work. You need to split each line into separate values and create PS Objects using those values. Each value needs to be assigned to a property of the object, so you'll also need to assign a property name to each one.

There are many ways to accomplish this in Powershell. Here is one. The property names are just Prop1-Prop7. You'll need to replace those with something meaningful, and replace the testfile.txt file name with your file spec.

#Create an array of property names
$Props = &{$args} Prop1 Prop2 Prop3 Prop4 Prop5 Prop6 Prop7

Get-Content testfile.txt |
 ForEach-Object {
    $Parts = $_ -split '\s+'  #Split the line at the spaces to create an array of values
    $PropHash = [ordered]@{}  #Create an empty hash table for the property hash (the [ordered] is optional, but keeps the properties in the same order as the property list)
    for ($i=0; $i -le 6; $i++) 
     {$PropHash[$Props[$i]] = $Parts[$i]} #Assign the split values to the property names by their respective array indexes
    [PSCustomObject]$PropHash #Output a PSCustomObject built from the hash table
 }

Comments

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.