5

I'm trying to verify that the file system partitions within each of the servers I'm working on are aligned correctly. I've got the following script that when I've tried running will either claim that all virtual servers are aligned or not aligned based on which if statement I use (one is commented out):

$myArr = @()
$vms = get-vm | where {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -match "Microsoft Windows*" } | sort name
foreach($vm in $vms){
    $wmi = get-wmiobject -class "win32_DiskPartition" -namespace "root\CIMV2" -ComputerName $vm
    foreach ($partition in $wmi){
        $Details = "" | Select-Object VMName, Partition, Status
        #if (($partition.startingoffset % 65536) -isnot [decimal]){
        if ($partition.startingoffSet -eq "65536"){
            $Details.VMName = $partition.SystemName
            $Details.Partition = $partition.Name
            $Details.Status = "Partition aligned"
        }
        else{
            $Details.VMName = $partition.SystemName
            $Details.Partition = $partition.Name
            $Details.Status = "Partition not aligned"
        }
    $myArr += $Details
    }
}
$myArr | Export-CSV -NoTypeInformation "C:\users\myself\Documents\Scripts\PartitionAlignment.csv"

Would anyone know what is wrong with my code? I'm still learning about partitions so I'm not sure how I need to check the starting off-set number to verify alignment.

EDIT:

    $myArr = @()
    $vms = get-vm | where {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -match "Microsoft Windows*" } | sort name
    $wmi = get-wmiobject -class "win32_DiskPartition" -namespace "root\CIMV2" -ComputerName $vm
    #foreach ($_ In Get-WMIObject Win32_DiskPartition | Select Name, BlockSize, NumberOfBlocks, StartingOffSet, @{n='Alignment'; e={$_.StartingOffSet/$_.BlockSize}}) {$_}
    foreach ($wmi| Select Name, BlockSize, NumberOfBlocks, StartingOffSet, @{n='Alignment'; e={$_.StartingOffSet/$_.BlockSize}}) {$_}
Valrok
  • 330
  • 3
  • 11

1 Answers1

4

Well, I see you've commented out the line that attempts to do the actual arithmetic. Your code as it is right now says "if partition starting offset = 65536 then partition is aligned".

That's not how it works. Partitions have all sorts of starting offsets. The WMI class returns 2 partitions on my laptop right now, neither of which have a starting offset of 65536.

Secondly, even if you uncommented the line above it, the one where it divides the starting offset by 65536 and compares the remainder to the Decimal data type... that's not how it works either. Don't use the Decimal type.

PS C:\> 1 -Is [Decimal]
False
PS C:\> 1.23 -Is [Decimal]
False

They both evaluate to false. That's not going to tell whether the division resulted in a remainder or not.

Give this a whirl:

Foreach($_ In Get-WMIObject Win32_DiskPartition | Select Name, BlockSize, NumberofBlocks, StartingOffset, @{n='Alignment';e={$_.StartingOffset/$_.BlockSize}}) { $_ }

Name           : Disk #0, Partition #0
BlockSize      : 512
NumberofBlocks : 614400
StartingOffset : 1048576
Alignment      : 2048

Name           : Disk #0, Partition #1
BlockSize      : 512
NumberofBlocks : 487778304
StartingOffset : 315621376
Alignment      : 616448

If Alignment is a whole number, you're good. If it's a decimal, alignment is wrong.

Here's a good article on partition alignment:

http://technet.microsoft.com/en-us/library/dd758814(v=SQL.100).aspx

By the way, this is not something you typically need to worry about on Windows 2008+ VMs. Windows can handle its own partition alignment. Windows 2003 and below, maybe.

Ryan Ries
  • 55,011
  • 9
  • 138
  • 197
  • Could you explain to me what @{n='Alignment';e={$_.StartingOffset/$_.BlockSize}}) { $_ } is doing? Still pretty new to powershell and not sure what it does or how to get it to work to where it actually looks through each of my VMs – Valrok Oct 25 '13 at 17:43
  • 1
    It's adding an ad-hoc property to the output called 'Alignment' which consists of the Starting Offset of the partition divided by the block size (file allocation size) of the partition. Feel free to break it apart into more lines of code, I just wanted to cram it all into a single line. – Ryan Ries Oct 25 '13 at 18:59
  • One last question, how are you making your one line run through all VMs in an environment? Whenever I try running your line of code I'm getting the results for a single VM. I'm not sure why the $_ at the start of the foreach loop isn't going through every VM. I'll edit my main post to show what I've tried. – Valrok Oct 28 '13 at 20:29