To achieve this task, you can use VMware PowerCLI, a PowerShell module specifically designed for managing VMware vSphere environments. With PowerCLI, you can easily retrieve information about active powered-on VMs and VMs with shared VMDKs in vCenter. Below is a PowerShell script that accomplishes this:
# Connect to vCenter Server using PowerCLI
Connect-VIServer -Server "vcenter_server" -User "username" -Password "password"
# Get all powered-on VMs
$poweredOnVMs = Get-VM | Where-Object { $_.PowerState -eq "PoweredOn" }
# Output list of powered-on VMs
Write-Host "Powered-On VMs:"
foreach ($vm in $poweredOnVMs) {
Write-Host $vm.Name
}
# Get VMs with shared VMDKs
$sharedVMDKVMs = Get-VM | Get-HardDisk | Where-Object { $_.Sharing -eq "sharingMultiWriter" } | Select-Object -Unique VM
# Output list of VMs with shared VMDKs
Write-Host "VMs with Shared VMDKs:"
foreach ($vm in $sharedVMDKVMs) {
Write-Host $vm.Name
}
# Disconnect from vCenter Server
Disconnect-VIServer -Server "vcenter_server" -Confirm:$false
Replace the following placeholders in the script:
vcenter_server: Replace this with the IP or hostname of your vCenter Server.username: Replace this with your vCenter Server username with sufficient privileges to access VM information.password: Replace this with the password for the specified username.
Save the script with a .ps1 extension, and then run it using PowerShell or the PowerShell Integrated Scripting Environment (ISE).
The script connects to the vCenter Server using the Connect-VIServer cmdlet, retrieves all powered-on VMs using Get-VM, and filters the VMs with the PowerState property set to “PoweredOn.” It then outputs the list of powered-on VMs.
Next, the script retrieves all VMs that have shared VMDKs by using the Get-HardDisk cmdlet and filtering the VMDKs with the Sharing property set to “sharingMultiWriter.” It selects the unique VMs from the list and outputs the VMs with shared VMDKs.
Finally, the script disconnects from the vCenter Server using the Disconnect-VIServer cmdlet.
Please make sure you have VMware PowerCLI installed on the machine where you run the script. You can install it by following the instructions provided by VMware for your specific operating system. Additionally, ensure that you have appropriate permissions to access the vCenter Server and retrieve VM information.