VMware Cloud Community
kristianroos
Contributor
Contributor
Jump to solution

Get diffrent outputs from same script depending on what VM it hits?

Hello.

Im beginner to Powershell and PowerCLI, so you know what level this is on 😃

I get different outputs depending on what VM my script hits (whetever they have more than one disc or not). I dont understand how I can fix this...

For instance, I have created 4 new VMs.

TestVMfirst1 have only one disk

TestVM02 have 2 disks

TestVM03 have 3 disks

TestVM04 have only one disk

My goal is to get an csv-file with all discs included. I want Name, Datastore and SizeGB, this info I get from Get-harddisk.

When I run my script to select all 4 VMs:

$VMs = get-cluster -name kluster01 | get-vm -name TestVM*

-----------------------------------------------------------------------------

$VMs = get-cluster -name kluster01 | get-vm -name TestVM*

$Results = @()

foreach ($VM in $VMs) {

    $Result = new-object PSObject

    $Result | add-member -membertype NoteProperty -name "Name" -value $VM.Name

    $VMDiskCount = 1

    get-harddisk $VM | foreach {

        $disk = $_

        $Result | add-member -name "Disk($VMDiskCount)Name" -value $disk.Name -membertype NoteProperty

        $Result | add-member -name "Disk($VMDiskCount)Datastore" -value $disk.Filename.Split(']')[0].TrimStart('[') -membertype NoteProperty

        $Result | add-member -name "Disk($VMDiskCount)SizeGB" -value ([math]::Round($disk.CapacityKB / 1MB)) -membertype NoteProperty

        $VMDiskCount++

    }

    $Results += $Result

}

$Results | select-object * | format-table

-----------------------------------------------------------------------------

I get result with max 1 disk:

Name                           Disk(1)Name                    Disk(1)Datastore                                    Disk(1)SizeGB
----                           -----------                    ----------------                                    -------------
TestVMfirst1                   Hard disk 1                    DS-201                                                    40
TestVM02                       Hard disk 1                    DS-201                                                    40
TestVM03                       Hard disk 1                    DS-201                                                    40

But if I run the same but with TestVM0* (selects 2-4 VMs):

$VMs = get-cluster -name kluster01 | get-vm -name TestVM0*

-----------------------------------------------------------------------------

$VMs = get-cluster -name kluster01 | get-vm -name TestVM0*

$Results = @()

foreach ($VM in $VMs) {

    $Result = new-object PSObject

    $Result | add-member -membertype NoteProperty -name "Name" -value $VM.Name

    $VMDiskCount = 1

    get-harddisk $VM | foreach {

        $disk = $_

        $Result | add-member -name "Disk($VMDiskCount)Name" -value $disk.Name -membertype NoteProperty

        $Result | add-member -name "Disk($VMDiskCount)Datastore" -value $disk.Filename.Split(']')[0].TrimStart('[') -membertype NoteProperty

        $Result | add-member -name "Disk($VMDiskCount)SizeGB" -value ([math]::Round($disk.CapacityKB / 1MB)) -membertype NoteProperty

        $VMDiskCount++

    }

    $Results += $Result

}

$Results | select-object * | format-table

-----------------------------------------------------------------------------

I get this result:

Name            Disk(1)Name     Disk(1)Datastore      Disk(1)SizeGB Disk(2)Name     Disk(2)Datastore      Disk(2)SizeGB
----            -----------     ----------------      ------------- -----------     ----------------      -------------
TestVM02        Hard disk 1     DS-201                      40 Hard disk 2     DS-201                      10
TestVM03        Hard disk 1     DS-201                      40 Hard disk 2     DS-202                      10
TestVM04        Hard disk 1     DS-201                      40

I miss all my disks on TestVM03, i seems that the first VM sets the limit on how many discs can be outputted in the table.. A dirty workaround is to create dummy discs on the first targeted VM, but it seems like the wrong way to solve my problem.

Thanks in advance

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You're conclusion is correct I'm afraid, the first row determines how many properties you will see.

An alternative workaround is to sort the rows on the number of properties in descending order.

Something like this

$Result | Sort-Object -Property {($_ | Get-Member).Count} -Descending | Format-Table


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You're conclusion is correct I'm afraid, the first row determines how many properties you will see.

An alternative workaround is to sort the rows on the number of properties in descending order.

Something like this

$Result | Sort-Object -Property {($_ | Get-Member).Count} -Descending | Format-Table


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
kristianroos
Contributor
Contributor
Jump to solution

Woaw, thanks LucD

Perfect, precise and fast answer. What more can you ask for?

I found out that I could´nt have | Format-Table when using export-csv. So the final line became:

$Results | Sort-Object -Property {($_ | Get-Member).Count} -Descending | export-csv -notypeinformation "c:\output.csv"

Thanks  again.

BR

Kristian

Reply
0 Kudos