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