Convert string output to input into export-csv

Welcome Forums General PowerShell Q&A Convert string output to input into export-csv

This topic contains 12 replies, has 6 voices, and was last updated by

 
Participant
2 months, 4 weeks ago.

  • Author
    Posts
  • #108602

    Participant
    Points: 0
    Rank: Member

    Good morning
    Attempting to get the output of the following command which produces a string object. Here is a snippet of the output:
    xxApp011
    xxApp014
    xxApp033
    xxApp038
    xxApp046
    xxApp048
    xxApp053
    xxApp054
    xxApp065
    xxApp066

    Attempted to use ConvertTo-csv and that didn't work, any suggestions as to how to change the output so export-csv will be happy

    Thank you for your input

    Norm

    $result0000 = foreach ($server0000 in $servers0000) {
        Get-VM -Name $server0000 | where { ($_.PowerState -eq "PoweredOn") } | Select-Object -ExpandProperty Name | Sort 
    } 
    
  • #108605

    Participant
    Points: 0
    Rank: Member

    Hello Norm Long,

    By your command I am assuming you are using VSphere PowerCLI and trying to get Powered On VMs. I would change your command script slightly.

    
    $POVMs = Get-VM | Where-Object {$PSItem.PowerState -eq "PoweredOn"} | Select-Object Name
    
    $POVMs | Sort | Export-CSV 
    
    

    Should work. I don't have PowerCLI on this computer so I can't test. The above will return all VMs that are powered on in a VCenter. If you need to narrow it down further first. You can specify either a Cluster or Hosts either in the Get-VM command or Get-VICluster | Get-VM.

    The other benefit of the above is you are keeping the objects in tact until the end. You could start incorporating Get-View with the Get-VM and start getting useful info from Disk, CPU, Memory, Networking, etc.

    Cory Fuchs

  • #108608

    Participant
    Points: 0
    Rank: Member

    Hello Cory;
    Thanks for the input, however that did not work. I'm struggling with the output producing a string object. Here is what I'm seeing and what your suggestion produced:

    #TYPE System.String
    "Length"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"
    "8"

    Norm

    • #108629

      Participant
      Points: 0
      Rank: Member

      Hello Norm,

      Add the "-Expandproperty" after the Select-Object. Name has to be enumerated. Add "-NoTypeInfomation" on the export-csv

      Code should be:

      $POVMs = Get-VM | Where-Object {$PSItem.PowerState -eq "PoweredOn"} | Select-Object Name
      
      $POVMs | Sort | Export-CSV  -NoTypeInformation
      

      Cory Fuchs

    • #108631

      Participant
      Points: 0
      Rank: Member

      One more time because I didn't add the -ExpandProperty

      $POVMs = Get-VM | Where-Object {$PSItem.PowerState -eq "PoweredOn"} | Select-Object -ExpandProperty Name
      
      $POVMs | Sort | Export-CSV output.csv -NoTypeInformation
      
  • #108641

    Participant
    Points: 1
    Rank: Member

    Don't forget to play with the ConvertFrom-String CMDLET it might surprise you!

  • #108658

    Participant
    Points: 0
    Rank: Member

    Hello Cory;
    Tried your suggestions, no worky!

    Norm

    • #108664

      Moderator
      Points: 49
      Team Member
      Rank: Member

      How about a one-liner like so:

      get-vm | Where-Object {$_.powerstate -eq 'PoweredOn'} | 
      select-object name | sort-object name | 
      export-csv "$env:userprofile\desktop\output.csv" -NoTypeInformation
      
    • #108730

      Participant
      Points: 0
      Rank: Member

      Alex;
      What you suggested worked!!! Could you please explain why it worked, don't understand why it worked.

      Norm

  • #108671

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    Hi @NormLong with the suggestions by Cory, what is not working, can you let us know the output you got. And please let us know your expected output as well.

  • #108694

    Participant
    Points: 158
    Helping Hand
    Rank: Participant

    Powershell Export- commands typically want a PSObject to convert to the other format. The -NoTypeInformation removed this information from the header:

    #TYPE System.String
    

    If the output is not what you expect, you should check to see where it is breaking.

    $vms = Get-VM | Where-Object {$_.Powerstate -eq 'PoweredOn'} | Select-Object -Property Name
    $vms
    

    Does this output the VM Names in Powershell? If it does, then you should work on exporting it to another format:

    $vms = Get-VM | Where-Object {$_.Powerstate -eq 'PoweredOn'} | Select-Object -Property Name
    $vms | Export-CSV -Path "$env:userprofile\Desktop\vms.csv" -NoTypeInformation
    

    If this code is not working, provide exactly what you are running and example output.

  • #108734

    Moderator
    Points: 49
    Team Member
    Rank: Member

    The issue is with '-expandproperty, that 'converts' the object into a string.

    look at the example:

    $test = [PSCustomObject]@{Name = 'Name'} | select-object name
    $test.GetType()
    < #  
        cool $test is an object
        You can pipe objects to Export-CSV
    #>
    
    $test = $test | select-object -ExpandProperty name
    $test.GetType()
    < #  
        now $test is a string!
        the only property that string
        has is 'length' that's why you
        saw '8' in your CSV.
        If you export a string to csv
        you'll just see the length of it
    #>
    

    Cory's example should have worked, but it dod not sort it as he did not tell 'sort-object' to sort after name. THe sample he provided without the '-expandproprty' should have worked, but unsorted.
    Rob's should have worked as well. :¬)

  • #108737

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    @Norm – You problem was because of -ExpandProperty usage. That parameter picks the value from the property(name is property here) which is of data type string.

You must be logged in to reply to this topic.