Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing CSV from a variable instead of file?

I have a command that formats it's output in the form of CSV. I have a list of machine this command will run against using a foreach loop. in the below example $serverlist is automatically generated with an AD Query.

foreach ($server in $serverlist) {
 $outputlist = mycommand
 }

what I would like to do is somehow end up with objects from the resulting CSV so I can then only select certain objects for a report. However the only way I can see to do this is using import-csv, which only seems to want to work with files and not variable: ie.

Import-Csv output.csv | ft "HostName","TaskName" | 
   Where-object {$_.TaskName -eq 'Blah'}

I'd like to be able to have import-csv $outputlist instead. doing this causes import-csv to have a hissyfit :)

Can anyone point me in the right direction on how to achieve this?

Cheers

like image 453
Ben Short Avatar asked Jan 13 '10 06:01

Ben Short


People also ask

What are the two different ways to import CSV module?

Answer: On the Data tab, in the Get & Transform Data group, click From Text/CSV. In the Import Data dialog box, locate and double-click the text file that you want to import, and click Import. In the preview dialog box, you have several options: Select Load if you want to load the data directly to a new worksheet.

Why is my CSV not importing correctly?

One of the most common CSV import errors is that the file is simply too large. That can be caused by too many fields or records in the file, too many columns, or too many rows. The import error can be caused by limits set by the program using the file or the amount of available memory on the system.

How do I import a CSV file?

On the Data menu, point to Get External Data, and then click either Edit Text Import or Data Range Properties. If you select Edit Text Import, select the file that you imported originally, and then make changes to the external data in the Text Import Wizard.


1 Answers

The command you want is called ConvertFrom-CSV. The syntax is shown below.

NAME
    ConvertFrom-CSV

SYNOPSIS
    Converts object properties in comma-separated value (CSV) format into CSV
    versions of the original objects.

SYNTAX
    ConvertFrom-CSV [[-Delimiter] <char>] [-InputObject] <PSObject[]> [-Header <string[]>] [<CommonParameters>]
    ConvertFrom-CSV -UseCulture [-InputObject] <PSObject[]> [-Header <string[]>] [<CommonParameters>]
like image 194
Josh Avatar answered Oct 02 '22 00:10

Josh