Difference between revisions of "Powercli examples"

From UVOO Tech Wiki
Jump to navigation Jump to search
 
(12 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
$vCenters = @("vmc01", "vmc02")
 
$vCenters = @("vmc01", "vmc02")
 
Connect-VIServer -Server $vCenters
 
Connect-VIServer -Server $vCenters
 +
```
 +
 +
### Max CPU Memory in last 7 days
 +
```
 +
$clusterName = "YourClusterName"
 +
 +
$endDate = Get-Date
 +
$startDate = $endDate.AddDays(-7)
 +
 +
$cpuStats = Get-Stat -Entity (Get-Cluster -Name $clusterName) -Stat "cpu.usage.average" -Start $startDate -Finish $endDate -MaxSamples 1 -IntervalMins 5
 +
$maxCpuUsage = $cpuStats | Measure-Object -Property Value -Maximum | Select-Object -ExpandProperty Maximum
 +
 +
$maxMemoryUsage = $memoryStats | Measure-Object -Property Value -Maximum | Select-Object -ExpandProperty Maximum
 +
$memoryStats = Get-Stat -Entity (Get-Cluster -Name $clusterName) -Stat "mem.usage.average" -Start $startDate -Finish $endDate -MaxSamples 1 -IntervalMins 5
 +
 +
echo $maxCpuUsage
 +
echo $maxMemoryUsage
 
```
 
```
  
Line 90: Line 107:
  
 
```
 
```
 +
 +
# Running VMS
 +
```
 +
$runningVMs = Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn'}
 +
foreach ($vm in $runningVMs) {
 +
    $vmName = $vm.Name
 +
    $hostName = $vm.Host.Name
 +
    Write-Host "VM: $vmName is running on host: $hostName"
 +
}
 +
```
 +
 +
### Running VMs to File
 +
```
 +
$outputFilePath = "runningVMs.txt"
 +
$runningVMs = Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn'}
 +
foreach ($vm in $runningVMs) {
 +
    $vmName = $vm.Name
 +
    $hostName = $vm.Host.Name
 +
    $outputLine = "$vmName"
 +
    Add-Content -Path $outputFilePath -Value $outputLine
 +
}
 +
```
 +
 +
### Set ESXi host to Maintenance Mode
 +
```
 +
$hostName = "YourESXiHostName"
 +
 +
# Get the VMHost object
 +
$vmhost = Get-VMHost -Name $hostName
 +
 +
# Put the host into maintenance mode
 +
Set-VMHost -VMHost $vmhost -State Maintenance -Confirm:$false
 +
 +
```
 +
 +
### Get hosts in maintenance mode
 +
```
 +
Get-VMHost | Where-Object {$_.ConnectionState -eq "Maintenance"} | Select-Object Name
 +
```
 +
 +
### Disconnect
 +
```
 +
# Disconnect from the vCenter server
 +
Disconnect-VIServer -Confirm:$false
 +
```
 +
 +
### Total Used memory in VMs
 +
```
 +
$targetCluster = "foo"
 +
 +
$hostsNotInMaintenance = Get-Cluster $targetCluster | Get-VMHost | Where-Object { $_.ConnectionState -ne "Maintenance" }
 +
 +
function Get-TotalUsedMemory {
 +
    param(
 +
        [string]$esxiHost
 +
    )
 +
 +
    $hostSystem = Get-VMHost $esxiHost | Get-View
 +
    $memoryInfo = $hostSystem.Summary.QuickStats
 +
 +
    return $memoryInfo.OverallMemoryUsage
 +
}
 +
 +
 +
$totalUsedMemory = 0
 +
 +
foreach ($host in $hostsNotInMaintenance) {
 +
    $totalUsedMemory += Get-TotalUsedMemory -esxiHost $host
 +
}
 +
 +
Write-Host "Total Used Memory on Hosts not in Maintenance in Cluster $targetCluster : $totalUsedMemory MB"
 +
```
 +
 +
# Refs
 +
- https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/VM-metrics-I-O-CPU-MEM/td-p/2807641

Latest revision as of 01:36, 16 February 2024

auth to api

$vCenters = @("vmc01", "vmc02")
Connect-VIServer -Server $vCenters

Max CPU Memory in last 7 days

$clusterName = "YourClusterName"

$endDate = Get-Date
$startDate = $endDate.AddDays(-7)

$cpuStats = Get-Stat -Entity (Get-Cluster -Name $clusterName) -Stat "cpu.usage.average" -Start $startDate -Finish $endDate -MaxSamples 1 -IntervalMins 5
$maxCpuUsage = $cpuStats | Measure-Object -Property Value -Maximum | Select-Object -ExpandProperty Maximum

$maxMemoryUsage = $memoryStats | Measure-Object -Property Value -Maximum | Select-Object -ExpandProperty Maximum
$memoryStats = Get-Stat -Entity (Get-Cluster -Name $clusterName) -Stat "mem.usage.average" -Start $startDate -Finish $endDate -MaxSamples 1 -IntervalMins 5

echo $maxCpuUsage
echo $maxMemoryUsage

do stuff

# Connect to vCenter server
# Connect-VIServer -Server <vCenterServer> -User <Username> -Password <Password>

# Specify the cluster name
$clusterName = "YourClusterName"

# Get all virtual machines on the specified cluster
$vmList = Get-VM -Location (Get-Cluster -Name $clusterName)

# Display information about each virtual machine
foreach ($vm in $vmList) {
    Write-Host "VM Name: $($vm.Name) | Cluster: $($vm.Cluster) | PowerState: $($vm.PowerState)"
}

# Disconnect from vCenter server
Disconnect-VIServer -Confirm:$false

Max CPU

# Connect to vCenter server
Connect-VIServer -Server <vCenterServer> -User <Username> -Password <Password>

# Specify the cluster name
$clusterName = "YourClusterName"

# Specify the start and end dates for the 7-day period
$endDate = Get-Date
$startDate = $endDate.AddDays(-7)

# Get CPU usage statistics for the specified cluster in the 7-day period
$cpuStats = Get-Stat -Entity (Get-Cluster -Name $clusterName) -Stat "cpu.usage.average" -Start $startDate -Finish $endDate -MaxSamples 1 -IntervalMins 5

# Find the maximum CPU usage value
$maxCpuUsage = $cpuStats | Measure-Object -Property Value -Maximum | Select-Object -ExpandProperty Maximum

# Display the result
Write-Host "Maximum CPU Usage in the last 7 days for Cluster '$clusterName': $maxCpuUsage%"

# Disconnect from vCenter server
Disconnect-VIServer -Confirm:$false

Max Memory

# Specify the start and end dates for the 7-day period
$endDate = Get-Date
$startDate = $endDate.AddDays(-7)

# Get memory usage statistics for the specified cluster in the 7-day period
$memoryStats = Get-Stat -Entity (Get-Cluster -Name $clusterName) -Stat "mem.usage.average" -Start $startDate -Finish $endDate -MaxSamples 1 -IntervalMins 5

# Find the maximum memory usage value
$maxMemoryUsage = $memoryStats | Measure-Object -Property Value -Maximum | Select-Object -ExpandProperty Maximum

# Display the result
Write-Host "Maximum Memory Usage in the last 7 days for Cluster '$clusterName': $maxMemoryUsage%"

CPU GHz used - this doesn't work

# Convert CPU usage from percentage to GHz (assuming 2.6 GHz per core)
$cpuUsageGHz = $cpuStats.Value * 2.6 / 100

# Find the maximum CPU usage (GHz) value
$maxCpuUsageGHz = $cpuUsageGHz | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum

# Display the result
Write-Host "Maximum CPU Usage (GHz) in the last 7 days for Cluster '$clusterName': $maxCpuUsageGHz GHz"

Current CPU

# Get real-time CPU usage statistics (percentage) for the specified cluster
$cpuStats = Get-Stat -Entity (Get-Cluster -Name $clusterName) -Stat "cpu.usage.average" -Realtime

# Display the result
Write-Host "Current CPU Usage in percentage for Cluster '$clusterName': $($cpuStats[-1].Value)%"

Running VMS

$runningVMs = Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn'}
foreach ($vm in $runningVMs) {
    $vmName = $vm.Name
    $hostName = $vm.Host.Name
    Write-Host "VM: $vmName is running on host: $hostName"
}

Running VMs to File

$outputFilePath = "runningVMs.txt"
$runningVMs = Get-VM | Where-Object {$_.PowerState -eq 'PoweredOn'}
foreach ($vm in $runningVMs) {
    $vmName = $vm.Name
    $hostName = $vm.Host.Name
    $outputLine = "$vmName"
    Add-Content -Path $outputFilePath -Value $outputLine
}

Set ESXi host to Maintenance Mode

$hostName = "YourESXiHostName"

# Get the VMHost object
$vmhost = Get-VMHost -Name $hostName

# Put the host into maintenance mode
Set-VMHost -VMHost $vmhost -State Maintenance -Confirm:$false

Get hosts in maintenance mode

Get-VMHost | Where-Object {$_.ConnectionState -eq "Maintenance"} | Select-Object Name

Disconnect

# Disconnect from the vCenter server
Disconnect-VIServer -Confirm:$false

Total Used memory in VMs

$targetCluster = "foo"

$hostsNotInMaintenance = Get-Cluster $targetCluster | Get-VMHost | Where-Object { $_.ConnectionState -ne "Maintenance" }

function Get-TotalUsedMemory {
    param(
        [string]$esxiHost
    )

    $hostSystem = Get-VMHost $esxiHost | Get-View
    $memoryInfo = $hostSystem.Summary.QuickStats

    return $memoryInfo.OverallMemoryUsage
}


$totalUsedMemory = 0

foreach ($host in $hostsNotInMaintenance) {
    $totalUsedMemory += Get-TotalUsedMemory -esxiHost $host
}

Write-Host "Total Used Memory on Hosts not in Maintenance in Cluster $targetCluster : $totalUsedMemory MB"

Refs