Author Posts

August 15, 2018 at 4:34 pm

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 
} 

August 15, 2018 at 4:56 pm

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

August 15, 2018 at 5:03 pm

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

August 15, 2018 at 6:07 pm

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

August 15, 2018 at 6:08 pm

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

August 15, 2018 at 7:20 pm

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

August 15, 2018 at 8:12 pm

Hello Cory;
Tried your suggestions, no worky!

Norm

August 15, 2018 at 8:24 pm

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

August 15, 2018 at 9:32 pm

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.

August 16, 2018 at 1:34 pm

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.

August 16, 2018 at 6:52 pm

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

Norm

August 16, 2018 at 7:02 pm

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. :¬)

August 16, 2018 at 7:07 pm

@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.