0
点赞
收藏
分享

微信扫一扫

通过vCenter的API动态调整vmware vsphere 虚拟机内存大小

由于生产环境,系统内消耗的内存频繁增长,vmware vsphere 的虚拟化可以在不停机的情况下加内存和cpu资源,通过vCenter的API动态调整vmware vsphere 虚拟机内存大小,立即生效的,而基于kvm的虚拟机要关机再开机才能生效,从监控上看内存的资源不够,cpu资源几本不会饱和使用。

param(
    [Parameter(Mandatory=$true)]
    [string]$ip,
    [Parameter(Mandatory=$true)]
    [int]$memorySizeGB
)

function ConnectToVCenter {
    param(
        [Parameter(Mandatory=$true)]
        [string]$server,
        [Parameter(Mandatory=$true)]
        [string]$user,
        [Parameter(Mandatory=$true)]
        [string]$password
    )
    try {
        Connect-VIServer -Server $server -User $user -Password $password
    } catch {
        Write-Error "Failed to connect to vCenter: $_"
        exit 1
    }
}

function Get-VMByIP {
    param(
        [Parameter(Mandatory=$true)]
        [string]$ip
    )

    $vms = Get-VM
    foreach($vm in $vms) {
        $vmIPs = (Get-VMGuest -VM $vm).IPAddress

        if($ip -in $vmIPs) {
            return $vm
        }
    }

    return $null
}

function Set-VMResources {
    param(
        [Parameter(Mandatory=$true)]
        [VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl]$vm,
        [Parameter(Mandatory=$true)]
        [int]$memorySizeGB
    )
    try {
        $memorySizeMB = $memorySizeGB * 1024
        Set-VM -VM $vm -MemoryMB $memorySizeMB -Confirm:$false
    } catch {
        Write-Error "Failed to update VM resources: $_"
    }
}

# Main script starts here
ConnectToVCenter -server 'your_vcenter_server' -user 'your_username' -password 'your_password'

$vm = Get-VMByIP -ip $ip
if($vm -eq $null) {
    Write-Error "No VM found with IP address: $ip"
    exit 1
}

Set-VMResources -vm $vm -memorySizeGB $memorySizeGB

使用

对ip 10.17.214.18设置 10GB内存
pwsh updateMemSize.ps1 10.17.214.18 10
资源只能增,不能减

通过vCenter的API动态调整vmware vsphere 虚拟机内存大小_IP

通过vCenter的API动态调整vmware vsphere 虚拟机内存大小_生产环境_02


举报

相关推荐

0 条评论