Skip to content

Instantly share code, notes, and snippets.

@MarWeUMR
Last active January 27, 2024 12:16
Show Gist options
  • Save MarWeUMR/d311ad7ff3a1494e9a2ae4e53168a929 to your computer and use it in GitHub Desktop.
Save MarWeUMR/d311ad7ff3a1494e9a2ae4e53168a929 to your computer and use it in GitHub Desktop.
powercli-cheat-sheet.md

How to install power cli in powershell

 Install-Module -Name VMware.PowerCLI

How to allow bad certificates

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

How to connect with vSphere

Connect-VIServer -Server <SERVER> -User <USERNAME>

How to configure bios on next boot for all VMs

# Get all VMs
$vms = Get-VM

# Loop through each VM
foreach ($vm in $vms) {
    # Create the configuration spec
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $vmConfigSpec.bootOptions = New-Object VMware.Vim.VirtualMachineBootOptions
    $vmConfigSpec.bootOptions.EnterBIOSSetup = $true

    # Apply the configuration
    $vm.ExtensionData.ReconfigVM($vmConfigSpec)

    # Output the VM name for tracking
    Write-Host "Configured BIOS setup on boot for VM: $($vm.Name)"
}

How to change the image used

# ISO path
$newIsoPath = "[netinstall_darz] os/linux/RHEL/9.1/rhel-baseos-9.1-x86_64-dvd.iso"

# Get all VMs
$vms = Get-VM

# Loop through each VM
foreach ($vm in $vms) {
    # Get the VM's CD/DVD drive
    $cdDrive = Get-CDDrive -VM $vm

    # Create a new virtual device config spec for the CD/DVD drive
    $cdSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
    $cdSpec.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::Edit
    $cdSpec.Device = $cdDrive.ExtensionData

    # Set the new ISO backing
    $isoBacking = New-Object VMware.Vim.VirtualCdromIsoBackingInfo
    $isoBacking.FileName = $newIsoPath
    $cdSpec.Device.Backing = $isoBacking

    # Set connectable info
    $connectInfo = New-Object VMware.Vim.VirtualDeviceConnectInfo
    $connectInfo.AllowGuestControl = $true
    $connectInfo.StartConnected = $true
    $connectInfo.Connected = $true
    $cdSpec.Device.Connectable = $connectInfo

    # Create the VM config spec
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $vmConfigSpec.DeviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
    $vmConfigSpec.DeviceChange[0] = $cdSpec

    # Apply the configuration
    $vm.ExtensionData.ReconfigVM($vmConfigSpec)

    # Output the VM name for tracking
    Write-Host "Set ISO for VM: $($vm.Name) to $newIsoPath"
}

How to revert all machines to the latest Snapshot?

# Get all VMs
$vms = Get-VM

# Loop through each VM
foreach ($vm in $vms) {
    # Get the latest snapshot of the VM
    $latestSnapshot = $vm | Get-Snapshot | Sort-Object Created -Descending | Select-Object -First 1

    # Check if the VM has a snapshot
    if ($latestSnapshot -ne $null) {
        # Revert the VM to the latest snapshot
        Set-VM -VM $vm -Snapshot $latestSnapshot -Confirm:$false

        # Output the VM name for tracking
        Write-Host "Reverted VM: $($vm.Name) to snapshot: $($latestSnapshot.Name)"
    } else {
        Write-Host "No snapshot found for VM: $($vm.Name)"
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment