1

Sorry for the wierd title, I didn't know how to phrase the question. I'm relatively new to Powershell and I'm writing a program. Basically, I have an array where the user has selected settings or wish to have "Selected" from the GWMI query stored in "$settings_array". I want to output the results to a CSV. When I try to run it, only the first Select statement gets output to the CSV. Output to the textbox works fine. I know it has something to do with how it's being stored in the array at each ieration. $resultList is intialized as an array ($resultList = @()). There are hundreds of lines of code for the Form and other functions, but here is the relevant code. Thanks for the help! Let me know if I need to post more of the code.

$colItems = GWMI Win32_NetworkAdapterConfiguration -computername $PCname -namespace "root\CimV2" -filter "IpEnabled = TRUE"
ForEach ($objItem in $colItems)
{ 
    ForEach ($objItem2 in $settings_array)
    {
        $resultList += $objItem  | Select $objItem2
        $richTextBox1.Appendtext("$objItem2" + ": " + $objItem.$objItem2 + "`r`n")
        $richtextbox1.ScrollToCaret()
    }
}
$resultList | export-csv "$ScriptDir\Exports\$Outputfile" 
1
  • This worked: $colItems | Select-Object -Property $settings_array | export-csv "$ScriptDir\Exports\$Outputfile" -NoType after building settings array in the same function. Building $settings_array in a separate function created errors. Commented Mar 8, 2013 at 16:25

2 Answers 2

1

CSV is made with rows and columns. Each object in the array you export gets a row, and each object gets a column value for each property. You add a new record/object to the resultlist with a single property every time(every object has only one property). The reason you only get the first is because your records contain different property-names. To solve this "non-static propertyname" problem, powershell takes the first object's properties as a template for the csv file. Since object2,object3 etc. doesn't include the same property, they will be blank. However, when you hit another object with the same property as the first object had, the value will be included too. Ex. you get the Name property for all network adapters, but blank values on the rest.

Your sample is missing information, ex. how $settings_array is built. If it's a normal string-array like:

$settings_array = "Name", "DisplayName", "Test"

or

$settings_array = @("Name", "DisplayName", "Test")

Then you can pass the whole array to select.

$colItems = GWMI Win32_NetworkAdapterConfiguration -computername $PCname  -namespace "root\CimV2" -filter "IpEnabled = TRUE"
ForEach ($objItem in $colItems)
{ 
    #Write to screen
    ForEach ($objItem2 in $settings_array)
    {
        $richTextBox1.Appendtext("$objItem2" + ": " + $objItem.$objItem2 + "`r`n")
        $richtextbox1.ScrollToCaret()
    }
}
#Save to CSV
$colItems | Select-Object -Property $settings_array | export-csv "$ScriptDir\Exports\$Outputfile" 

Notice the last line. Now, the foreach loop is only used for your textbox-content, while the last line formats the CSV as it should.

EDIT Try this to get your settings:

Function GetSettings { 
    $out = @()
    Foreach ($objItem in $chklstGetMIPRet.CheckedItems) {
        $out += $objItem.ToString()
    } 
    $out
}
$settings_array = GetSettings
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response...I'll try this when I get back to work. $settings_array is built running through a loop similar to foreach($item in $chklistbox.SelectedItems) $settings_array += $item I can't remember the exact syntax right now, but it is an array of strings.
I tried this solution, but I get this: Select-Object : Cannot convert System.Management.Automation.PSObject to one of the following types {System.String, System.Management.Automation.ScriptBlock}. At E:\Scripts\CSTools\CSTools.ps1:713 char:56 + $colItems | Select-Object <<<< -Property $sett ings_array | export-csv "$ScriptDir\Exports\$Outputfile" + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupport edException + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Co mmands.SelectObjectCommand
Here is the code for $settings_array Function Settings(){ Foreach ($objItem in $chklstGetMIPRet.CheckedItems) {$settings_array1 += [String]$objItem} Return $settings_array1 } $settings_array = Settings($this)
This almost worked: $colItems | Select-Object -Property "$settings_array" | export-csv "$ScriptDir\Exports\$Outputfile" -NoType I get all the properties' names exported to cell A1 of the CSV, but none of the actual content from the results.
The problem seams to be Your settingsarray. I don't think Your values are converted properly. You should add codesamples in an edit to Your question, including how you create Your form(so we know that the propertynames are correct). I've added an edit to my answer With a New "settings"-function you can try out.
1

Just a simpe tip: you can export in XML with export-clixml and whatever the number of column, they are all added in the file.

1 Comment

This worked perfectly for me, better than the output I probably would have thought of.

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.