ViProperties in PowerCLI refer to the object properties retrieved for example, using the Get-VM cmdlet. These properties include essential information about virtual machines in VMware environments, such as their names, power state, hardware configuration, and more. ViProperties serve as the foundation for managing and automating virtual machine tasks in PowerCLI, allowing administrators to gather critical data and make informed decisions about their VMware infrastructure.

Find the vCenter where the object (VM, VMHost, Datastore, VMHost, Cluster) is stored to.

New-VIProperty -Name vCenter -ObjectType VirtualMachine -Value {
    param($obj)
    return (($obj.uid -split ":")[0] -split "@")[1]
} -Force | Out-Null

New-VIProperty -Name vCenter -ObjectType VMHost -Value {
    param($obj)
    return (($obj.uid -split ":")[0] -split "@")[1]
} -Force | Out-Null

New-VIProperty -Name vCenter -ObjectType Cluster -Value {
    param($obj)
    return (($obj.uid -split ":")[0] -split "@")[1]
} -Force | Out-Null

New-VIProperty -Name vCenter -ObjectType Datastore -Value {
    param($obj)
    return (($obj.uid -split ":")[0] -split "@")[1]
} -Force | Out-Null

After they’re created, then someone may use them as mentioned in the example below:

Get-VM | Select Name, vCenter
Get-VMHost | Select Name, vCenter

These custom VI Properties can be packed into a function, that can be used whenever you connect to vCenter, otherwise you don’t need them.

Packing into a function is easy, the code can be found below:

function Create-VIProperties {
    New-VIProperty -Name vCenter -ObjectType VirtualMachine -Value {
        param($obj)
        return (($obj.uid -split ":")[0] -split "@")[1]
    } -Force | Out-Null

    New-VIProperty -Name vCenter -ObjectType VMHost -Value {
        param($obj)
        return (($obj.uid -split ":")[0] -split "@")[1]
    } -Force | Out-Null

    New-VIProperty -Name vCenter -ObjectType Cluster -Value {
        param($obj)
        return (($obj.uid -split ":")[0] -split "@")[1]
    } -Force | Out-Null

    New-VIProperty -Name vCenter -ObjectType Datastore -Value {
        param($obj)
        return (($obj.uid -split ":")[0] -split "@")[1]
    } -Force | Out-Null
}

Not only you can put this function to the PS Module but it can be put within the Connect-vCenter function so that whenever you call the function the necessary VI Properties will be created.

0
0