Difference between revisions of "Powercli examples"
Jump to navigation
Jump to search
Line 139: | Line 139: | ||
Set-VMHost -VMHost $vmhost -State Maintenance -Confirm:$false | Set-VMHost -VMHost $vmhost -State Maintenance -Confirm:$false | ||
+ | ``` | ||
+ | |||
+ | ### Get hosts in maintenance mode | ||
+ | ``` | ||
+ | Get-VMHost | Where-Object {$_.ConnectionState | ||
``` | ``` | ||
Revision as of 21:08, 13 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" 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
Disconnect
# Disconnect from the vCenter server Disconnect-VIServer -Confirm:$false