PowerCLI Script to ...
 
Notifications
Clear all

PowerCLI Script to change Power Management Active Policy on Standalone host or cluster

1 Posts
1 Users
0 Reactions
252 Views
Brandon Lee
Posts: 395
Admin
Topic starter
(@brandon-lee)
Member
Joined: 14 years ago

I have been writing a lot lately about power management, power consumption, and enabling low-power modes for home lab environments, as this is extremely important when thinking about running a home lab 24x7x365. It helps to make sure that your server runs as efficiently as possible without spinning unnecessary CPU cycles and power when idle.

If you want an easy way to automate the process of setting the power management active policy for your ESXi host, PowerCLI is a great way to do this. Check out the code below:

Here is a summary of what the script will do:

  • Check to see if you connected: If not connected, you will be asked to connect to a vCenter server
  • Cluster or standalone: It will prompt you whether you want to configure a cluster or a standalone host
  • Nam: Enter the name of the cluster or host you want to configure
  • Select the power policy: Choose a power policy (High Performance, Balanced, or Low Power)
  • Confirmation: The script will confirm whether or not it was able to apply the power policy settings to the host or cluster

Ok, so here is the script for you to use, enjoy!

# Check if connected to any vCenter Server
$connected = $false
$servers = Get-VIServer
foreach ($server in $servers) {
    if ($server.IsConnected) {
        $connected = $true
        break
    }
}

# If not connected, prompt for connection
if (-not $connected) {
    Write-Host "Not connected to any vCenter Server. Please connect to a vCenter Server."
    $vCenterServer = Read-Host "Enter the vCenter Server name or IP"
    Connect-VIServer -Server $vCenterServer

    # Check if the connection was successful
    $connectedServer = Get-VIServer | Where-Object {$_.IsConnected}
    if (-not $connectedServer) {
        Write-Host "Failed to connect to the vCenter Server. Please try again."
        exit
    }
}

# Function to get the power policy code based on user input
function Get-PowerPolicyCode {
    param (
        [string]$policy
    )

    switch ($policy.ToLower()) {
        "high performance" { return 1 }
        "balanced" { return 2 }
        "low power" { return 3 }
        default {
            Write-Host "Invalid power policy. Please choose between 'High Performance', 'Balanced', or 'Low Power'."
            exit
        }
    }
}

# Prompt user to select target type (cluster or standalone host)
$targetType = Read-Host "Do you want to configure a Cluster or a Standalone Host? (Enter 'Cluster' or 'Host')"

# Initialize variables
$vmhosts = @()

# Get the target based on user input
if ($targetType -eq "Cluster") {
    $clusterName = Read-Host "Enter the name of the Cluster"
    $vmhosts = Get-Cluster -Name $clusterName | Get-VMHost
} elseif ($targetType -eq "Host") {
    $hostName = Read-Host "Enter the name of the Standalone Host"
    $vmhosts = Get-VMHost -Name $hostName
} else {
    Write-Host "Invalid input. Please enter 'Cluster' or 'Host'."
    exit
}

# Check if any hosts were found
if ($vmhosts.Count -eq 0) {
    Write-Host "No hosts found for the specified target."
    exit
}

# Prompt user for desired power policy
$powerPolicy = Read-Host "Enter the power policy (High Performance, Balanced, Low Power)"
$policyCode = Get-PowerPolicyCode -policy $powerPolicy

# Loop through each host and set the power management policy
foreach ($vmHost in $vmHosts) {
    $view = (Get-VMHost $vmHost | Get-View)
    (Get-View $view.ConfigManager.PowerSystem).ConfigurePowerPolicy($policyCode)

    Write-Host "Power policy for host $($vmHost.Name) set to $powerPolicy."
}

ย