Creating a Custom PowerCLI Module for Common VMware Tasks

PowerCLI is powerful enough to manage an entire VMware environment, yet many administrators still begin each day by copying commands from old scripts. A custom PowerCLI module turns those repeated snippets into a consistent toolkit for inventory checks, virtual machine operations, datastore reporting, and routine maintenance.

For Australian IT teams, reusable automation can make a measurable difference. A consultant moving between Sydney and Melbourne customers, or an infrastructure team supporting sites in Brisbane, Perth and regional areas, benefits from having the same tested commands available across every vCenter Server.

A module also creates a clean boundary between infrastructure knowledge and individual scripts. Functions can apply naming standards, logging, safety checks and Australian time-zone requirements without forcing every operator to remember the implementation details.

This approach fits naturally with the practical automation material available on the Virtxpert blog, where VMware, PowerCLI, Git and home-lab techniques come together. The aim is a maintainable toolset that feels straightforward to use on a busy service desk or during a planned change window.

Why A Module Earns Its Place

A PowerCLI module is a directory containing one or more PowerShell module files. It can expose functions such as Get-VmSummary, New-VmSnapshotReport or Set-VmPowerState, while hiding connection handling and repetitive parameter validation.

The main benefit is consistency. A function can require a vCenter name, verify that the target object exists, record the operator and timestamp, and refuse risky actions unless an explicit switch is supplied. That is much safer than relying on an administrator to remember those checks in a one-off script.

Choose A Practical Module Boundary

Start with tasks that are repeated frequently and have clear inputs and outputs. Inventory reports, VMware Tools checks, snapshot discovery, datastore capacity alerts and powered-on virtual machine summaries are good candidates for an initial release.

Avoid creating one enormous “do everything” function. Smaller commands are easier to test and compose. For example, Get-VmInventory should retrieve and format data, while a separate reporting function should export it to CSV or JSON. This separation also helps when different customers have different reporting requirements.

Set Up The Project Structure

A simple layout keeps source control manageable:

Virtxpert.Vmware/
├── Virtxpert.Vmware.psd1
├── Virtxpert.Vmware.psm1
├── Public/
│   ├── Get-VmInventory.ps1
│   └── Get-DatastoreCapacity.ps1
├── Private/
│   ├── Resolve-ViConnection.ps1
│   └── Write-ModuleLog.ps1
└── Tests/
    └── Virtxpert.Vmware.Tests.ps1

The module manifest should define the name, version, author, PowerShell requirements and exported functions. The .psm1 file can dot-source public and private scripts, then use Export-ModuleMember to expose only the commands intended for users.

Store the project in Git and commit small, meaningful changes. A technician working from an office in Adelaide or a home lab on an NBN connection should be able to clone the repository, install dependencies and load the module without manually reconstructing its internals.

Build Safe Reusable Functions

A function should use approved PowerShell verbs and provide clear parameters. Strong parameter types, validation attributes and pipeline support reduce accidental misuse:

function Get-VmInventory {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]$Name,

        [Parameter()]
        [switch]$IncludeNetwork
    )

    $vm = Get-VM -Name $Name -ErrorAction Stop

    $result = [pscustomobject]@{
        Name        = $vm.Name
        PowerState  = $vm.PowerState
        Guest       = $vm.Guest.OSFullName
        VMwareTools = $vm.ExtensionData.Guest.ToolsVersionStatus2
    }

    if ($IncludeNetwork) {
        $result | Add-Member -NotePropertyName Network -NotePropertyValue (
            $vm | Get-NetworkAdapter | Select-Object -ExpandProperty NetworkName
        )
    }

    $result
}

Keep VMware cmdlets close to the function that needs them, and return objects rather than formatted text. Object output can be filtered, exported or passed to another command. Use Write-Verbose for operational detail and reserve Write-Warning or terminating errors for conditions that need attention.

Handle Sessions And Errors

Connection management deserves its own private helper. Rather than calling Connect-VIServer in every public function, create a resolver that checks the current VIServer context and connects only when required. Avoid storing passwords in scripts; use PowerShell credentials, the SecretManagement ecosystem or an approved enterprise vault.

Error handling should be deliberate. Add -ErrorAction Stop when a failed VMware operation must prevent the rest of the function from running. For destructive operations, use SupportsShouldProcess so administrators can preview changes with -WhatIf and approve them with -Confirm.

Australian environments often span multiple time zones, particularly when a team in Sydney supports Western Australia. Store event timestamps in UTC and convert them for display rather than relying on the workstation’s local clock. This prevents confusing reports during daylight-saving changes in New South Wales, Victoria and the ACT.

Test Package And Publish

Pester tests can validate parameter behaviour, object properties and error conditions without requiring a live production vCenter. Mock Get-VM, Get-Datastore and connection helpers to test predictable scenarios such as a missing VM, an unreachable server or a datastore above its capacity threshold.

Before publishing a release, run static analysis with PSScriptAnalyzer, import the module in a clean PowerShell session and test it against a non-production vCenter. Increment the manifest version when behaviour changes, and document compatibility with PowerCLI and vCenter releases.

A private Git repository or internal PowerShell repository is usually the right first destination. Public distribution requires extra care around examples, customer names, credentials and licensing. Review the site disclaimer before reusing published material or presenting the module as an official VMware product.

Recommendations For Daily Use

Build the first version around a small set of dependable commands, then expand based on real operational demand. A module that saves a few minutes during every change window can become more valuable than a large collection of rarely used functions.

Make installation simple with a documented command such as Install-Module or an internal package repository. Include examples for common workflows, expected permissions and safe rollback steps. Plain language matters, especially when a contractor or “mate from the infrastructure team” needs to use the tooling during an after-hours incident.

Grow The Toolkit Carefully

Once the core functions are stable, add features that improve visibility rather than complexity. Examples include vSAN health summaries, datastore oversubscription reports, orphaned snapshot detection and compliance checks for VM hardware versions.

Keep customer-specific policies in configuration files or separate wrapper modules. A government environment in Canberra, a mining operation in Western Australia and a small business in Newcastle may all use VMware differently. The reusable core should remain portable, while local rules stay explicit and easy to audit.

A well-structured module becomes a shared operational asset: easier onboarding for new administrators, safer automation during maintenance, and fewer differences between a home lab and production practice. Start with one repeated VMware task, place it under version control, and release a tested command that your team can trust.