Learn how to automate your vSphere infrastructure.

Category: Mixed scripts

Create Calculated Properties

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

Create a PowerShell Module


A PowerShell module is like an army Swiss knife for your computer. Just as a Swiss knife packs multiple tools into one handy device, a PowerShell module bundles various commands and functions into a single, cohesive package.

Creating your own PowerShell module is beneficial because it:

  1. Organizes code into reusable components.
  2. Prevents naming conflicts.
  3. Facilitates collaboration and documentation.
  4. Supports versioning and testing.
  5. Enables sharing with the community.

Creating a PS Module is a straight forward process. The locations where you can place your PS module can be found by executing this command:

(ls env:\PSModulePath).Value -split ";"

Within one of those folders, you have to create a folder with the name of the Module and inside create a psm1 file with the same name as the module. The path should look like this: C:\Users\<yourUser>\Documents\WindowsPowerShell\Modules\<moduleName>\<moduleName>.psm1

e.g. When the module is called psclimodule:

C:\Users\Admin\Documents\WindowsPowerShell\Modules\psclimodule\psclimodule.psm1 

The functions you create can be stored within that file, and then they are imported automatically when you call a function from that module.

0
0

© 2024 PowerCli scripting

Theme by Anders NorenUp ↑