Showing posts with label VMWare. Show all posts
Showing posts with label VMWare. Show all posts

Wednesday, September 5, 2018

Match vmware virtual machine hard disk to computer hard disks

One of the common problems that vcenter admins have faced is mapping the hard disks seen in vmware to the hard disks seen on computers in disk management.

This powershell script helps in matching disks seen in vcenter to those seen on the computer.
Install the vmware powercli before this. Recent powercli link is here
https://code.vmware.com/web/dp/tool/vmware-powercli/6.5.4

Even if you have an updated one the script should work fine. Unless the cmdlets are changed.

You also need to have at least read permissions in vmware and administrator on the server.

If the disk serial number is missing in the OS this script will not work.

Check  if the disk serial number is present by running this in powershell.

Get-WmiObject -Class Win32_DiskDrive | select serialnumber

If there is no serial number then add/modify the following parameter in the vm's advanced configuration.

disk.EnableUUID = “TRUE

KB link is here
https://kb.vmware.com/s/article/52815


 ## Show corresponding disks in vmware and computer ##

param(
   [Parameter(Mandatory=$true)]
[string] $Computername
)


Function OutData($data) {Write-host $data -ForegroundColor Green}
Function Get-LocalDisk($Computer,[System.Management.Automation.CredentialAttribute()]$Credentials)
{
$PDiskDrives = Get-WmiObject -Class Win32_DiskDrive -ComputerName $Computer -Credential $Credentials

return $PDiskDrives
}

## Load Vmware assemblies ##
Import-Module VMWare.VimAutomation.sdk
Import-Module VMWare.VimAutomation.Core

## Connect Vmware
$Credentials = Get-Credential  -Message "Enter your Vcenter credentials"
$LocalDriveObjects =  @()
Connect-VIServer Vcenter -Credential $Credentials
$LocalDisks = Get-LocalDisk $Computername -Credentials $Credentials
$vmHardDisks = Get-VM -Name $ComputerName | Get-HardDisk
$vmDatacenterView = Get-VM -Name $ComputerName | Get-Datacenter | Get-View
$virtualDiskManager = Get-View -Id VirtualDiskManager-virtualDiskManager
Write-Host "Number of disks : $($LocalDisks.count)"
foreach($DObject in $LocalDisks)
 {

     # Write-host "Working on $DObject"
 
      foreach($vmHardDisk in $vmHardDisks)
              {
              $vmHardDiskUuid = $virtualDiskManager.queryvirtualdiskuuid($vmHardDisk.Filename, $vmDatacenterView.MoRef) | foreach {$_.replace(' ','').replace('-','')}
            
              if($DObject.SerialNumber -eq $vmHardDiskUuid)
                {
                 $Output = "$($vmHardDisk.Name) $($vmHardDisk.capacityGB)" + "GB" + " Computer Disk: " + $("Disk " + $Dobject.Index) + " " + $([math]::round($DObject.Size/1GB, 3)) + "GB" + " on $Computername"
                 $Vdisk = "Vcenter: $($vmHardDisk.Name) $($vmHardDisk.capacityGB)" + "GB "
                 $ComputerDisk = "Computer: " + $("Disk " + $Dobject.Index) + " " + $([math]::round($DObject.Size/1GB, 2)) + "GB"
                 Write-Host $Vdisk -ForegroundColor Yellow -NoNewline
                 Write-Host $ComputerDisk -ForegroundColor White
             
               
                }
              }
   
 }






 ## Author: Parag Waghmare


Wednesday, November 29, 2017

Community Vmware management pack for vmware sphere 6.0

Mitch Luedy has created a very good management pack for monitoring VMware. He also was gracious enough to make it available to everyone for free.

Link to his github repository of the entire solution is here.

https://github.com/Mitch-Luedy/Community.VMware

This management pack uses PsSnapins in the powershell scripts. And vmware has ceased the use of snapins from 6.0.

 I have modified the scripts to use modules and this management pack will work on vsphere 6.0 now.

Link to that repository is here.

https://github.com/paragwaghmare/Community-Vmware

 


Wednesday, July 12, 2017

Increase VMware Vcenter server disk space using powershell

Increasing virtual disks in VMware is pretty easy and straightforward.
Mapping them to the right disk in your computer is hard. They have not direct correlation seen in the Vmware ui.
So its pretty much guesswork after checking the available diskspace in computer and matching it with the diskspace seen in VMWare.

This script matches the disks in VMware with the one in computer.
Increases the space
Extends all the partitions in your computer which have free diskspace.You shouldn't have diskpace lying around unused anyway ;)

Things you need.
Account which has rights on the VMware server. Account with rights on your computer.
VMware assemblies or powercli installed.

Download link here
https://my.vmware.com/group/vmware/details?downloadGroup=VSP510-PCLI-510&productId=285


 If your script fails with and exception for SoapInterceptor
find the dll VMware.VimAutomation.Logging.SoapInterceptor.dll in your powercli install directory.
and add this line to your script after placing it in the right location.

[System.Reflection.Assembly]::LoadFrom("c:\temp\VMware.VimAutomation.Logging.SoapInterceptor.dll")



############# Variable declaration and initialization##################################################
$ComputerName = "Put your VMware computername here"
$username = "domain\username"
$password = ConvertTo-SecureString -AsPlainText -Force "password"
$Credentials = New-Object System.Management.Automation.PSCredential ($username, $password)
$LocalDriveObjects =  @()
$capacityIncrease = 2
$Volume = "D:"

############################################Assemblies#############################################################
add-pssnapin VMware.VimAutomation.Core
add-pssnapin VMware.VimAutomation.Vds

##########################################################################################################


Function CreateEventLog()
{
$ApplicationEventLogSources = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application"

if(!(($ApplicationEventLogSources | Select-Object "PSChildName") -match "DiskIncreaseScript"))
{
 New-EventLog -LogName Application -Source DiskIncreaseScript -ComputerName "localhost"

}

}


Function WriteEvent($message)
  {
      Write-EventLog -logname Application -computername "localhost" -Source DiskIncreaseScript -Eventid 65503 -Message $message
  }

Function Increase-Disk($HardDisk)
         {
            
            $CurrentCapacity = $HardDisk.CapacityGB
            $Capacity = $CurrentCapacity + $capacityIncrease
            WriteEvent "Current capacity on $Computername : $CurrentCapacity. This will be increased to $Capacity"
            Set-HardDisk -HardDisk $HardDisk -CapacityGB $Capacity -GuestCredential $Credentials -Confirm:$false
         }

Function Increase-volume($Computer)
         {
          
           WriteEvent "Increasing volumes on $Computer"
          
           Invoke-Command -ComputerName $Computer -scriptblock { 'list disk' | diskpart | ? {
  $_ -match 'disk (\d+)\s+online\s+\d+ .?b\s+\d+ [gm]b'
} | % {
  $disk = $matches[1]
  "select disk $disk", "list partition" | diskpart | ? {
    $_ -match 'partition (\d+)'
  } | % { $matches[1] } | % {
    "select disk $disk", "select partition $_", "extend" | diskpart | Out-Null
  }
}
}


          }


Function Get-LocalDisk($Computer)
{
$PDiskDrives = Get-WmiObject -Class Win32_DiskDrive -ComputerName $Computer

 foreach($PdiskDrive in $PDiskDrives)
  {

   $partitionquery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($PdiskDrive.DeviceID)'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
   $Partitions = Get-WmiObject -Query $partitionquery -ComputerName $Computer
   #$Partitions
    foreach($Partition in $Partitions)
     {
     $Logicaldrivesquery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} WHERE AssocClass = Win32_LogicalDiskToPartition"
     $LogicalDrives =  Get-WmiObject -Query $Logicaldrivesquery -ComputerName $Computer
 
    
      foreach($LogicalDrive in $LogicalDrives)
        {
          
       
        $obj = New-Object -Type PSCustomObject -Property @{
        Disk        = $PdiskDrive.DeviceID
        DiskSize    = $PdiskDrive.Size
        DiskModel   = $PdiskDrive.Model
        Partition   = $partition.Name
        RawSize     = $partition.Size
        DriveLetter = $LogicalDrive.DeviceID
        VolumeName  = $LogicalDrive.VolumeName
        Size        = $LogicalDrive.Size
        FreeSpace   = $LogicalDrive.FreeSpace
        SerialNumber = $PdiskDrive.SerialNumber
                                                           }

        $LocalDriveObjects = $LocalDriveObjects + $obj
        }
     }
}
        
return $LocalDriveObjects
}

##################### Main Script######################################################
Connect-VIServer "VcenterServerName" -Credential $Credentials
$LocalDisks = Get-LocalDisk $Computername
$vmHardDisks = Get-VM -Name $ComputerName | Get-HardDisk 
$vmDatacenterView = Get-VM -Name $ComputerName | Get-Datacenter | Get-View 
$virtualDiskManager = Get-View -Id VirtualDiskManager-virtualDiskManager

CreateEventLog

foreach($DObject in $LocalDisks)
 {
  if($DObject.DriveLetter -eq $Volume)
     {
      WriteEvent  "Checking driveletter $($DObject.Driveletter) on $ComputerName"
      foreach($vmHardDisk in $vmHardDisks)
              {
              $vmHardDiskUuid = $virtualDiskManager.queryvirtualdiskuuid($vmHardDisk.Filename, $vmDatacenterView.MoRef) | foreach {$_.replace(' ','').replace('-','')}
             
              if($DObject.SerialNumber -eq $vmHardDiskUuid)
                {
                 WriteEvent "Found a match $vmHardDiskUuid $($DObject.SerialNumber) $($vmHardDisk.Name) on $Computername"
                 WriteEvent "Disk Object: $DObject"
                 Increase-Disk $vmHardDisk
                 Start-Sleep -Seconds 10
                
                
                }
              }
     }
 }

Start-Sleep -Seconds 120

Increase-volume $Computername



Thursday, June 22, 2017

VMWare monitoring using SCOM

The management pack for monitoring VMware is here.

https://github.com/Mitch-Luedy/Community.VMware

It's a very good management pack.and also has clear instructions for installation.


But after installation you may find that the required objects are not being discovered.
This is largely due to the fact that the powershell scripts which are running the discoveries have this

Try {
    Add-PSSnapin VMware.VimAutomation.Core
} Catch {
    Start-Sleep -Seconds 10
    Try {
        Add-PSSnapin VMware.VimAutomation.Core
    } Catch {
        DefaultErrorLogging
        Exit
    }
}

If the mp was unsealed then maybe I could have updated the code and added import-module instead of this one. But to fix it  try this.

1. Install the VMware powercli version from this link.

https://my.vmware.com/web/vmware/details?downloadGroup=PCLI550&productId=352

VMware-PowerCLI-5.1.0-793510.exe

After installation run the commands in powershell and if you do not get any errors then you are good to go.