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"
}}} |
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
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
Thanks Lucd!
Location is Datacenter
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
thanks again!
But what about if?
IF datacenter is Chicago, then Chicago, else - Manhattan
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
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
Lucd you are amazing Master!!!
Thanks!