VMware Cloud Community
JordonC
Enthusiast
Enthusiast
Jump to solution

Power on a list of VMs

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

 

0 Kudos
1 Solution

Accepted Solutions
gordonng
Contributor
Contributor
Jump to solution

 

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.

View solution in original post

7 Replies
gordonng
Contributor
Contributor
Jump to solution

 

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.

LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
JordonC
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
JordonC
Enthusiast
Enthusiast
Jump to solution

@LucD 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.  

0 Kudos
gordonng
Contributor
Contributor
Jump to solution

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

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos