For some reason I've got writers block and can't wrap my head around this issue. What I'm trying to do is collect all vm's in a datacenter, check the starting partition offset on all hd's and then output the hd's who's offset doesn't start with 32 or 64. Here is the code that I have:
$VMs = Get-VM
foreach($vm in $vms){
$hds = Get-WmiObject Win32_DiskPartition -ComputerName $vm
foreach ($hd in $hds){
if($hd.StartingOffset -notlike "32*" -or $hd.StartingOffset -notlike "64")
{
missing block:
}}}
Basically what I need to do is find out which hd's do not have those startingoffets, if they don't output the vm name, hard disk name (ex: Hard disk 1) and datastore location. However, I can't figure out a way to relate the Win32_DiskPartition to the vmware disk. I need to pull the HD's datastore location but from the DiskPartition class all i can get is Disk #0, Partition #0.
I hope this isn't too confusing.
May I suggest you have a look at the following, as this may answer your question:
http://ict-freak.nl/2009/12/15/powercli-check-partition-alignment-windows-vms-only/
That's a helpful answer, thank you for that. One thing I would like to do though, an addition to that script is to be able to list the filename of the unaligned HD. Is there a way to do this?
Here's the script I normally use to gather this information:
$vm = get-vm testvm
$ds = Get-Datastore -VM $vm | Where {$_.Name -like "vm_data"}
This will return all of the data vmdk files as we have our OS drives in different datastores than our data. Any other thoughts or suggestions?
assuming $mycol (from the check partition alignment link I posted) has returned you a list. I have taken the first item in the collection as an example.
#remove everything from the comma onwards.
$vmhddnumber = ($mycol[0].Partition).Substring(0,(($mycol[0].Partition).IndexOf(",")) )
#now remove all non numeric characters leaving on the disk number
$vmhddnumber = $vmhddnumber -replace( "[^0-9]", "" )
#Convert to an integer, else can not add one.
$vmhddnumber = [int] $vmhddnumber
#The disk number counts within Windows is zero based, however VMware Hard Disk is not. Therefore you need to add one.
$vmhddnumber++
Now if (and this is a big if, and I haven't tested anything here) there is a direct relationship between the Disk number from the guest OS and the VM hard disk number then you might be able to do something with the $vmhddnumber like so:
#Now search for the VM hard disk that matches that number
[vSphere PowerCLI] G:\Tools\PowerShell\Scripts> Get-HardDisk (Get-vm -name $mycol[0].VMName) | ? { $_.Name -eq "Hard disk " + $vmhddnumber }
CapacityKB Persistence Filename
---------- ----------- --------
16777216 Persistent [MyESX1-local] myVM1/myVM1.vmdk
so you can see in the file name the datastore.
You will need to probably wrap all of this into another foreach loop to cycle through all the returned data and return it in a format you like.
Hopefully this has been helpful. I am very interested to know if there is a direct relationship or if there is a another way to get that linkage.
Let us know how you go.
