Hi,
Is there a way to check and get the VMName using VMDK Name ?
Example, I have 1234.vmdk, I would like to know, which VM is currently using or attached to ?
If you only want to find VMDK that are connected to VMs, you could do the following.
But note that you might get false positives since you don't have the complete path.
$targetVMDK = 'test1.vmdk','test2.vmdk','test3.vmdk'
$targetRegEx = $targetVMDK -join '|'
Get-Datacenter -PipelineVariable dc|
Get-Cluster -PipelineVariable cl |
Get-VM -PipelineVariable vm |
Get-HardDisk -PipelineVariable hd |
ForEach-Object -Process {
if($hd.Filename -match $targetRegEx){
New-Object -TypeName PSObject -Property @{
Datacenter = $dc.Name
Cluster = $cl.Name
VM = $vm.Name
Harddisk = $hd.Name
File = $hd.Filename
}
}
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Do you have the complete path, including the Datastore & Folder?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I dont have any, I just a VMDK file name, which I need to validate, if that is used by any VM ?
That makes it very hard to find an answer since that VMDK can exist on multiple datastores.
Without a full path, you can not determine with 100% certainty that the filename you have is actually used by a VM.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could use the same logic, just in reverse.
Fetch the VMDK files from all VMs, then compare the results with the VMDK name you have.
But since the name you have is only a partial name (Datastore + folder + filename is the full path), more than 1 result can be found.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I tried as below, but how can I capture the output in csv for matched results
#import your csv files
$csv1 = Import-Csv "D:\Orphaned_VMDKs.csv"
$csv2 = Import-Csv "D:\VM_VMDKs_Info.csv"
#Compare both csv files (.Name and .VMDKName corresponds to your column name for your vmdk in the csv file )
$compare = Compare-Object $csv1.Name $csv2.VMDKName -IncludeEqual | ? {$_.SideIndicator -eq "=="}
if ($compare) {
foreach ($vmdk in $compare.InputObject) {
Write-Output "VMDK $vmdk exist in both csv files"
}
}
Else
{
Write-Output "VMDK $vmdk Match not Found"
}
Not sure what exactly you are trying to do.
The "orphaned" files include VMDK that are NOT connected to any VM.
So only if your VMDK is not in that list, it might be connected to a VM.
Also, the output of the orphaned script produces objects, you would need to look at the Name or Extents property of those objects.
I have no clue what you have in VM_VMDKs_Info.csv.
Does that contain all VMDKs attached to VMs?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
VM_VMDKs_Info.csv. has the output of below script, which contain all VMDKs attached to VMs?
$VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
"" | Select-Object -Property @{N="Folder";E={$VM.Folder}},
@{N="VM";E={$VM.Name}},
@{N="Hard Disk";E={$HardDisk.Name}},
@{N="Capacity(GB)";E={$HardDisk.CapacityGB}},
@{N="VMDKName";E={$HardDisk.FileName.Split('/')[1].TrimStart('[')}},
@{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
@{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
@{N="VMDKpath";E={$HardDisk.FileName}}
}
}
}
}
If you only want to find VMDK that are connected to VMs, you could do the following.
But note that you might get false positives since you don't have the complete path.
$targetVMDK = 'test1.vmdk','test2.vmdk','test3.vmdk'
$targetRegEx = $targetVMDK -join '|'
Get-Datacenter -PipelineVariable dc|
Get-Cluster -PipelineVariable cl |
Get-VM -PipelineVariable vm |
Get-HardDisk -PipelineVariable hd |
ForEach-Object -Process {
if($hd.Filename -match $targetRegEx){
New-Object -TypeName PSObject -Property @{
Datacenter = $dc.Name
Cluster = $cl.Name
VM = $vm.Name
Harddisk = $hd.Name
File = $hd.Filename
}
}
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect, that worked perfectly. 🙂 Thank you very much
The Filename property has the full path afaik.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference