VMware Cloud Community
IKirill
Enthusiast
Enthusiast
Jump to solution

Operator IF - One question

Hi for all!

I have little script, for example

Get-VM -Server $vserverOld | select @{N="IP Adress";E={@($_.guest.IPAddress[0])}},

@{N="VM Name";E={$_.Name}},

@{N="OS Type";E={$_.Guest.OSFullName}},

|

Export-Csv -Path "C:\iv\VM-CA-ALL-SUPER.csv" -NoTypeInformation -UseCulture

I want to add a condition. If the VM in the Chicago location, the column should be Chicago, else - Manhattan. I can't understand the syntax.

The sounds like this:

@{N="City";E={

                if($_.Location.GetType().Name -match "Chicago"){

                    "Chicago"

                }

                else{

                    "Manhattan"

                }}} |

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Then you could do like this

Get-VM -Server $vserverOld | select @{N="IP Adress";E={@($_.guest.IPAddress[0])}},

@{N="VM Name";E={$_.Name}},

@{N="OS Type";E={$_.Guest.OSFullName}},

@{N='Location';E={

    $parent = $_.Folder

    while($parent -isnot [VMware.VimAutomation.ViCore.Types.V1.Inventory.Datacenter]){

        $parent = $parent.Parent

    }

    if($parent.Name -ne 'Chicago'){'Manhattan'}else{$parent.Name}

}} |

Export-Csv -Path "C:\iv\VM-CA-ALL-SUPER.csv" -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

A VirtualMachine object has no Location property afaik.

From which property can you determine the location?

Is that the parent Folder in which the VM is, or in the VM's Notes field or in a Tag...?

If it is the parent Folder name, you could do

Get-VM -Server $vserverOld | select @{N="IP Adress";E={@($_.guest.IPAddress[0])}},

@{N="VM Name";E={$_.Name}},

@{N="OS Type";E={$_.Guest.OSFullName}},

@{N='Location';E={$_.Folder.Name}} |

Export-Csv -Path "C:\iv\VM-CA-ALL-SUPER.csv" -NoTypeInformation -UseCulture

If it is something else, the calculated property will need to change.


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

0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd!

Location is Datacenter

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, then try like this

Get-VM -Server $vserverOld | select @{N="IP Adress";E={@($_.guest.IPAddress[0])}},

@{N="VM Name";E={$_.Name}},

@{N="OS Type";E={$_.Guest.OSFullName}},

@{N='Location';E={

    $parent = $_.Folder

    while($parent -isnot [VMware.VimAutomation.ViCore.Types.V1.Inventory.Datacenter]){

        $parent = $parent.Parent

    }

    $parent.Name

}} |

Export-Csv -Path "C:\iv\VM-CA-ALL-SUPER.csv" -NoTypeInformation -UseCulture


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

0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

thanks again!

But what about if?

IF datacenter is Chicago, then Chicago, else - Manhattan

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It takes the name of the datacenter.

Are you saying that there are other datacenter names that should also be marked as Manhattan?


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you could do like this

Get-VM -Server $vserverOld | select @{N="IP Adress";E={@($_.guest.IPAddress[0])}},

@{N="VM Name";E={$_.Name}},

@{N="OS Type";E={$_.Guest.OSFullName}},

@{N='Location';E={

    $parent = $_.Folder

    while($parent -isnot [VMware.VimAutomation.ViCore.Types.V1.Inventory.Datacenter]){

        $parent = $parent.Parent

    }

    if($parent.Name -ne 'Chicago'){'Manhattan'}else{$parent.Name}

}} |

Export-Csv -Path "C:\iv\VM-CA-ALL-SUPER.csv" -NoTypeInformation -UseCulture


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

0 Kudos
IKirill
Enthusiast
Enthusiast
Jump to solution

Lucd you are amazing Master!!!

Thanks!

0 Kudos