Automation

 View Only
  • 1.  Power on a list of VMs

    Posted Apr 18, 2023 01:09 AM

    Hello 

    I am working on a simple PowerCLI project to increase the disk space on a list of VMs from a csv.  I have the disk expansion part done.  The next part of the logic is powering on any powered off VMs in the list.  Will need them power on for the second part that I will add later that extends the drive in the OS.  Can anyone make some suggestions to the code below to power on the VMs?  I have tried a few options without any luck.  

     

    Write-Host "vCenter Server Connection and Credentials" -ForegroundColor Green
    #.vCenter Server Details
    If($vCenterCreds -eq $null){
        $vCenterConn = Read-Host -Prompt 'Enter the vCenter Server FQDN'
        $vCenterCreds = Get-Credential -Message ("Enter the Credentials for vCenter Server "+$vCenterConn)
    }
    else{
        Write-Host "Credentials for $vCenterConn already acquired" -ForegroundColor Green
    }
    Write-Host "Connecting to vCenter Server $vCenterConn" -ForegroundColor Green
    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
    Connect-VIServer -Server $vCenterConn -Credential $vCenterCreds | Out-Null
    Write-Host "Expanding VDI C:\ Drive By 10GB" -ForegroundColor Green
    Import-Csv -Path 'I:\Vmware\VDI_List.csv' | %{
    Get-VM -Name  $_.Name |
    Get-HardDisk -Name 'Hard disk 1' | %{
    Set-HardDisk -HardDisk $_ -CapacityGB ($_.CapacityGB + 10) -Confirm:$False
    }
    
    }
    #.
    Write-Host "::: Disk Expansion Complete :::" -ForegroundColor Green

     



  • 2.  RE: Power on a list of VMs
    Best Answer

    Posted Apr 18, 2023 02:53 AM

     

    To power on the VMs after expanding the disk space, you can use the Start-VM cmdlet provided by PowerCLI. You can add the following code snippet after expanding the disk space:

     

    # ...
    Import-Csv -Path 'I:\Vmware\VDI_List.csv' | ForEach-Object {
        $vm = Get-VM -Name $_.Name
        if ($vm.PowerState -eq 'PoweredOff') {
            Write-Host "Powering on VM $($vm.Name)" -ForegroundColor Green
            Start-VM -VM $vm -Confirm:$false | Out-Null
        } else {
            Write-Host "VM $($vm.Name) is already powered on" -ForegroundColor Yellow
        }
        Get-HardDisk -VM $vm -Name 'Hard disk 1' | ForEach-Object {
            Set-HardDisk -HardDisk $_ -CapacityGB ($_.CapacityGB + 10) -Confirm:$False
        }
    }
    Write-Host "::: Disk Expansion Complete :::" -ForegroundColor Green

     

     

    In this code snippet, Start-VM cmdlet is used to power on the VMs that are powered off. The -Confirm:$false parameter is used to suppress the confirmation prompt. The Get-HardDisk cmdlet is updated to use the -VM parameter to specify the VM for which the hard disk is being expanded.

    Note: Please make sure to test the code in a non-production environment before using it in a production environment to avoid any unintended consequences.



  • 3.  RE: Power on a list of VMs

    Posted Apr 18, 2023 07:57 AM

    Sorry for my ignorance, but where does that snippet extend the disk inside the Guest OS (as the user seems to be asking)?



  • 4.  RE: Power on a list of VMs

    Posted Apr 18, 2023 11:42 AM

    Hi LucD,

    This part. I did not change the code for how the capacity is increased, I just copied the original code that the poster has used as his original intention is to increase by 10 GB.

        Get-HardDisk -VM $vm -Name 'Hard disk 1' | ForEach-Object {
            Set-HardDisk -HardDisk $_ -CapacityGB ($_.CapacityGB + 10) -Confirm:$False

     



  • 5.  RE: Power on a list of VMs

    Posted Apr 18, 2023 04:55 PM

    My question was related to the part of the original question where the user also mentioned to increase the disk inside the Guest OS.
    I didn't see that in your snippet



  • 6.  RE: Power on a list of VMs

    Posted Apr 18, 2023 11:08 AM

    Thanks  that did the trick.  I now see why my attempts were getting errors.  



  • 7.  RE: Power on a list of VMs

    Posted Apr 18, 2023 11:14 AM

     Thanks so much for the assistance in the past.  Is there a native way for PowerCLI / VCenter to expand the disk in the OS level without passing a txt file or ps1 file to a folder on the VM it self?  We have a separate process that expands the drives because many of the VMs are behind a firewall that cant be reached from systems running this PowerCLI script.  



  • 8.  RE: Power on a list of VMs

    Posted Apr 18, 2023 04:54 PM

    Since the VMs can't be reached the Invoke-VMScript option is out I guess.
    Is port 902 to the ESXi node that hosts the VM also closed?
    If that is open you should be able to use Invoke-VMScript, provided the VMware Tools are installed in the Guest OS.

    Otherwise I don't see any direct option.

    You could eventually run a scheduled task inside the Guest OS (Task Scheduler or crontab) that checks at regular intervals if the current disks have been expanded, and then take appropriate action.

    But that would be my last resort solution.