Efficiently Managing VMware Virtual Machines with PowerShell

In a virtualized environment, maintaining and managing virtual machines can be a complex task. This PowerShell script is specifically designed to simplify the management of VMware virtual machines, allowing administrators to quickly check the status, start, stop, or restart VMs as needed.
This script will:
1. Retrieve the status of all virtual machines.
2. Allow the user to start, stop, or restart a specified VM.
3. Provide a summary of the actions taken.
By automating these processes, IT professionals can save time and ensure that virtual machines are effectively managed.

# Import the required VMware PowerCLI module
if (-Not (Get-Module -ListAvailable -Name VMware.PowerCLI)) {
    Install-Module -Name VMware.PowerCLI -Scope CurrentUser -AllowClobber
}
# Connect to the VMware server
$vcServer = "your-vcenter-server"
$credential = Get-Credential
Connect-VIServer -Server $vcServer -Credential $credential
# Get the list of all VMs
$vms = Get-VM
# Output the current status of VMs
Write-Host "=== Current Status of Virtual Machines ==="
foreach ($vm in $vms) {
    Write-Host "VM Name: $($vm.Name) - Status: $($vm.PowerState)"
}
# Prompt the user for an action
$vmName = Read-Host "Enter the name of the VM to manage (Start/Stop/Restart)"
$action = Read-Host "Do you want to Start, Stop or Restart this VM?"
# Perform the requested action
switch ($action.ToLower()) {
    "start" {
        Start-VM -VM $vmName
        Write-Host "$vmName has been started."
    }
    "stop" {
        Stop-VM -VM $vmName -Confirm:$false
        Write-Host "$vmName has been stopped."
    }
    "restart" {
        Restart-VM -VM $vmName -Confirm:$false
        Write-Host "$vmName has been restarted."
    }
    default {
        Write-Host "Invalid action specified."
    }
}
# Disconnect from the VMware server
Disconnect-VIServer -Server $vcServer -Confirm:$false
Write-Host "VM management actions completed."