Question

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\a411882\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.

Was it helpful?

Solution

You're passing a virtual machine object instead of a string to get-wmiObject -ComputerName. When I do that, get-wmiObject throws an RPC error. You might try -computerName $vm.guest.Hostname instead of -computerName $vm.

In the commented line, your use of % should return a remainder, which will always be a whole number or zero. Maybe you were expecting a quotient instead, and wanted to evaluate if it's an integer?

PS C:\temp> (1 / 2) -isnot [int]
True
PS C:\temp> (2 / 1) -isnot [int]
False

Recent Windows OS align their partitions automatically, so there's that. Here's a good post about alignment generally on VMware, including a link to a more detailed discussion of guest partitions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top