Powershell: Check if partition is aligned or not


This script has the following Requirements:

  • VI Toolkit
  • Powershell
  • WMI query
  • Windows VM’s

The following script get al lists of all Windows VM’s which are powered on. The next step is a WMI query which queries the Win32_DiskPartition class. The final step is a match with the $StartingOffset variable.

Add-PSSnapIn VMware.VimAutomation.Core

# Connect to vCenter
$VC = Connect-VIServer (Read-Host "Enter vCenter server")

$StartingOffset = "65536"

# Get all VM’s with powerstate = PoweredOn
$VMS = Get-VM | Where {$_.PowerState -eq "PoweredOn"} |Sort Name
ForEach ($VM in $VMS)
    {
  
# Process only Windows Server VM’s
   if ($VM.Guest.OSFullName -match "Microsoft Windows*")
        {  
      
# Do a WMI Query
       $results = get-wmiobject -class "Win32_DiskPartition" -namespace "root\CIMV2" -ComputerName $VM

           foreach ($objItem in $results)
                {
              
# Do the match
               if ($objItem.StartingOffset -match $StartingOffset){
                  
write-host $objItem.SystemName
                  
write-host $objItem.Name
                  
write-host "Partition aligned" -foregroundcolor green
                  
write-host}
              
else{
                  
write-host $objItem.SystemName
                  
write-host $objItem.Name
                  
write-host "Partition NOT aligned" -foregroundcolor red
                  
write-host                  
                    }
                }
        }
    }
# Disconnect from vCenter
Disconnect-VIServer -Confirm:$False

after running the script, the following output will be generated:

image

Advertisements

5 thoughts on “Powershell: Check if partition is aligned or not

  1. Pingback: Episode 61 - Joe Pruitt from F5 and the ABCs of PowerShell « PowerScripting Podcast

  2. Mike R

    Nice script. Am getting a “The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)” attempting to run however. during the WMI query..

    Reply
    1. David S

      I’m getting similar

      Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
      6BA)
      At C:\Test.ps1:15 char:21
      + $wmi = get-wmiobject <<<< -class "Win32_DiskPartition" -namespace "root\CIMV
      2" -ComputerName $vm
      + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMExcept
      ion
      + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
      .GetWmiObjectCommand

  3. ryangrendahl

    Funny, i was writing a very similar script and troubleshooting out he RPC server issue when I ran into this. Would’ve saved me a bit of time! I like it.
    I am writing mine to be a csv generator for reporting.

    The RPC error issue was in my case resovled by adding the “remote management” check box on the targeted server / desktop if the firewall was anabled or disabling the firewall. (this is in my lab, so limited cases.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s