Skip navigation
1 2 Previous Next

Virtually an IT Noob

25 posts

Hello,

in order to list the vdisks of type independent, you can use this powercli code:

 

$allvms = Get-VM | where-object {($_.powerstate -ne "PoweredOff")}

foreach ($vm in $allvms | Get-View){

  foreach($dev in $vm.Config.Hardware.Device) {

    if(($dev.GetType()).Name -eq "VirtualDisk") {

      if($dev.Backing.DiskMode -eq "independent_persistent") {

        $vm.Name + "`t" + $dev.Backing.FileName

        $vm.Name + "`t" + $dev.Backing.DiskMode

   

            }   

        }

     }

}

 

(As usual, remember to use the Connect-VIServer cmdlet before!)

 

 

 

 

Best regards,

Pablo

Hi,

in order to list all the thin vdisks of your vmware infrastructure, a small powershell/powercli like this can be used:

 

$allvms = Get-VM | where-object {($_.powerstate -ne "PoweredOff")}

foreach ($vm in $allvms | Get-View){

  foreach($dev in $vm.Config.Hardware.Device) {

    if(($dev.GetType()).Name -eq "VirtualDisk") {

      if($dev.Backing.ThinProvisioned -eq $true) {

        $vm.Name + "`t" + $dev.Backing.FileName

            }   

        }

     }

}

 

(As usual, remember to use the Connect-VIServer cmdlet before!)

 

 

I would like to comment that this script is mainly taken from here.

 

 

Best regards,

Pablo

Hello,

to be honest, i don't like NetworkManager

 

If you want to disable it in CentOS 7, this steps had better be done:

 

  1. Check/modify the /etc/sysconfig/network-scripts/ifcfg-WhatEver files in the correct way (Maybe the IPADDR, PREFIX and GATEWAY options are the most important!)
  2. Execute this commands: systemctl disable NetworkManager.service;systemctl stop NetworkManager.service;service network restart (In order to disable networkmanager, stop it and restart the "network" service)

 

Best regards,

Pablo

Hello,

This can be the steps in order to execute one small script in the ESXi command line (SSH).

 

- Connect to the command line by ssh

- Change directory to some datastore

- Generate the script, this can be one example:

 

#!/bin/sh

 

NUM=0

 

while [ $NUM -lt 3 ]

do

 

DATE=`date +"%Y%m%d_%H-%M-%S"`

esxtop -b -d 2 -n 3 > esxtopcapture-$DATE.csv

NUM=`expr $NUM + 1`

 

done

 

# EOF

 

- Finally, give execution permission to the script:

busybox chmod 755 script.sh

 

 

In this case, the script iterates the "while" loop three time. Every time a esxtop output file is generated.

 

Hope this can be helpful to someone

 

 

Best regards,

Pablo

Hello,

if you want to backup the host configuration using the command line, you can execute this two commands:

 

  1. vim-cmd hostsvc/firmware/sync_config (In order to save/sync the host configuration)
  2. vim-cmd hostsvc/firmware/backup_config (to backup the configuration)


The last command had better output something similar to this:


Bundle can be downloaded at : http://*/downloads/configBundle-localhost..tgz


Then, the backup can be downloaded connecting to this URL: http://IP_of_the_ESXi_Host/downloads/configBundle-localhost..tgz


If you want to restore the backup, this steps must be done:


  1. vim-cmd hostsvc/maintenance_mode_enter (put the host in maintenance mode)
  2. vim-cmd hostsvc/firmware/restore_config /tmp/configBundle.tgz (restore the backup using the backup file. Before, the backup file must be uploaded to a location accessible by the host)


I got this information from this vmware's kb:

VMware KB: Backing up and restoring ESXi configuration using the vSphere Command-Line Interface and vSphere PowerCLI…


And, we can call it a day!


Best regards,

Pablo

Hello,

if you want to check is the virtual machines time is synced with the vSphere host, you can use this powershell/powercli code:

 

Get-VM | Select Name, @{N="SyncTimeWithHost";E={$_.ExtensionData.Config.Tools.SyncTimeWithHost}}

 

 

(As usual, remember to use the Connect-VIServer cmdlet before!)

 

 

Best regards,

Pablo

Hello,

if the virtual machine's mac address is in automatic mode, there are some situations where the virtual machine's mac address can change.

AFAIK, this situations are:

 

Additional bibliography

vSphere 5.5 Documentation Center

VMware KB: Virtual machine MAC address conflicts or have a duplicate MAC Address when creating a virtual machine

 

 

Best regards,

Pablo

Hello,

this code permits report the vm's guest disks with less than 20% free space.

 

I would like to comment that in order to check the free space, the vmware tools must be installed and running in the virtual machines.

If you want to know (using powercli) the virtual machines that haven't installed vmware tools, you can check this.

 

ForEach ($VM in Get-VM | where-object {($_.powerstate -ne "PoweredOff") -and ($_.Extensiondata.Guest.ToolsStatus -Match ".*Ok.*")}){

 

ForEach ($Drive in $VM.Extensiondata.Guest.Disk) {

 

$Path = $Drive.DiskPath

 

#Calculations 

$Freespace = [math]::Round($Drive.FreeSpace / 1MB)

$Capacity = [math]::Round($Drive.Capacity/ 1MB)

 

$SpaceOverview = "$Freespace" + "/" + "$capacity" 

$PercentFree = [math]::Round(($FreeSpace)/ ($Capacity) * 100) 

 

#VMs with less space

if ($PercentFree -lt 20) {     

    $Output = $Output + "Vm: " + $VM.Name + "`n"

    $Output = $Output + "Disk: " + $Path + "`n"

    $OutPut = $Output + "Free(MB): " + $Freespace + "`n"

    $Output = $Output + "Free(%): " + $PercentFree + "`n"  

}

 

} # End ForEach ($Drive in in $VM.Extensiondata.Guest.Disk)

 

} # End ForEach ($VM in Get-VM)

 

$Output

 

 

(As usual, remember to use the Connect-VIServer cmdlet before!)

 

 

My powercli script is based in this two scripts:

http://www.virtu-al.net/2009/03/25/vi-toolkit-one-liner-vm-guest-disk-sizes/

vGeek: Collect disk space detail and send it in email Powercli

 

 

Best regards,

Pablo

Hello,

if you want to migrate virtual machines running in vmware server 2 to vSphere ESXi, this how-to can be helpful.

 

Additionally, this documentation can be checked:

VMware KB: Moving or copying a virtual machine within a VMware environment

VMware KB: Best practices for using and troubleshooting VMware Converter

 

 

Best regards,

Pablo

Hello,

some time ago i faced an issue with the vcenter db transaction log, because it became full every 6 hours (at fixed hours).

In the windows event log this message appeared:

 

The transaction log for database 'VIM_VCDB' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases

 

If i checked the log_reuse_wait_desc column in sys.databases just after the event log happened, i noticed that the transaction log truncation was delayed waiting for a checkpoint. This information didn't help so much ... then, i made some research to learn the way the transaction log works.

 

The MS SQL database have configured the simple recovery model, then transaction log not should be so big as when using the full recovery mode.

In simple mode the database logs the active transactions in the transaction log, and after that, it frees space (log truncation) in the log. This truncation really don't make smaller the physical transaction log, It frees space for reuse by the transaction log. To be able to make smaller the physical transaction log, the log must be shrinked. Anyway, shrinking the file don't help.

 

I found this discussion in the vmware communities. It helped me to go in the correct direction.

 

The transaction log was getting full every time the dbo.cleanup_events_tasks_proc stored procedure was executed.

(To know what stored procedure was filling the transaction log, i used this sql query)


Every 6 hours this procedure tries to delete the data from the vpx_event, vpx_event_arg, and vpx_task tables based in the "database retention policy" specified in the "vCenter server settings". When i activated the "database retention policy" (180 days), the transaction log issue started to happen.

 

I checked the vpx_event, vpx_event_arg, and vpx_task tables and i noticed that this tables had entries/rows since 2012.

 

Then, i increased temporally the transaction log size from 500 MB to 2000 MB and executed manually the dbo.cleanup_events_tasks_proc. It was executed correctly (it deleted the entries older than 180 days in the vpx_event, vpx_event_arg, and vpx_task tables) .

 

Finally, i configured the transaction log size back to 500 MB.

 

 

And no more transaction log full messages in the windows event log



 

Best regards,

Pablo

 

Additional Bibliography

VMware KB: Purging old data from the database used by VMware vCenter Server 4.x and 5.x

VMware KB: Determining where growth is occurring in the VMware vCenter Server database

How to determine SQL Server database transaction log usage

Borja_Mari Virtuoso

Selected as vEXPERT 2014!

Posted by Borja_Mari Apr 2, 2014

Hello,

this year I'm selected as a vEXPERT 2014!

 

Thanks so much for selecting me as vEXPERT.

 

Source: vExpert 2014 Announcement | VMTN Blog - VMware Blogs

 

Best regards,

Pablo

Hello,

if you want to list all the powered-on virtual machines without vmware tools installed, you can use this powercli script:

 

# Change the value of this variable (regular expression), if you would want just some datastores (not all) from the script's output

$datastores=".+"

 

get-vm | where-object {$_.powerstate -ne "PoweredOff" } | % { get-view $_.ID } | where-object { ($_.Datastore -match $datastores) -and ($_.guest.toolsstatus -Notmatch ".*Ok.*") } | select Name

 

(Remember to use the Connect-VIServer cmdlet before!)

 

The script is based in this script.

 

 

Best regards,

Pablo

Hello,

if you want to add a new vdisk (vmdk file) to a running virtual machine, you can do it via command line.

 

First, connect to the esxi host where is the placed the vm that you want to add a new vdisk/vmdk.

 

Then go to the datastore where you want to place the new vdisk/vmdk:

cd /vmfs/volumes/WhatEverDatastore/

 

Then create the vmdk file using vmkfstools:

vmkfstools -c 150G --diskformat zeroedthick vDisk.vmdk

 

Finally, add the new vdisk to the virtual machine:

 

First, get the vmid of the virtual machine:

 

vim-cmd vmsvc/getallvms

 

With the vmid, add the vdisk/vmdk:

 

vim-cmd vmsvc/device.diskaddexisting 464 /vmfs/volumes/WhatEverDatastore/vDisk.vmdk 0 3

 

 

Best regards,

Pablo

P.D: If you want a linux virtual machine be able to detect the new vDisk, check this.

Hello,

searching the web, a found a nice script that reports orphaned/unused vmdk files:

Virtually Jason: Orphaned VMDK Files

 

It works so fine, but if do the bad praxis of having datastores with the same name in different vmware hosts, then it doesn't work.

 

I have modified the script to make it work in this kind of environments:

 

$report = @()

$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}

$arrHost = Get-VMHost

 

foreach ($vmhost in $arrHost) {

 

$arrDS = Get-Datastore -VMHost $vmhost | Sort-Object -property Name

foreach ($strDatastore in $arrDS) {

#Write-Output "$($strDatastore.Name) in $host.Name Orphaned Disks:"

$ds = Get-Datastore -Name $strDatastore.Name -VMHost $vmhost | % {Get-View $_.Id}

$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags

$fileQueryFlags.FileSize = $true

$fileQueryFlags.FileType = $true

$fileQueryFlags.Modification = $true

$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

$searchSpec.details = $fileQueryFlags

$searchSpec.matchPattern = "*.vmdk"

$searchSpec.sortFoldersFirst = $true

$dsBrowser = Get-View $ds.browser

$rootPath = "[" + $ds.Name + "]"

$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

 

 

foreach ($folder in $searchResult)

{

foreach ($fileResult in $folder.File)

{

if ($fileResult.Path)

{

$pathAsString = out-string -InputObject $FileResult.Path

if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){

# Changed Black Tracking creates ctk.vmdk files that are not referenced in the VMX.  This prevents them from showing as false positives.

if (-not ($pathAsString.toLower().contains("-ctk.vmdk"))){

$row = "" | Select VMHost, DS, Path, File, SizeGB, ModDate

$row.VMHost = $vmhost.Name

$row.DS = $strDatastore.Name

$row.Path = $folder.FolderPath

$row.File = $fileResult.Path

$row.SizeGB = (($fileResult.FileSize/1024)/1024)/1024

$row.ModDate = $fileResult.Modification

$report += $row

#Write-Output "$($row.Path)$($row.File)"

}

}

}

}

}

}

}

 

 

$report

 

 

 

 

(Remember to use the Connect-VIServer cmdlet before!)

 

 

 

Have fun!

 

 

Best regards,
Pablo

 

 

If you want get the RDMs defined in your vmware environment using PowerCLI, then you can just use this:

 

 

$allvms = Get-VM

 

foreach ($vm in $allvms) {

  

    $vmrdm = Get-HardDisk -VM $vm -DiskType "RawPhysical","RawVirtual"

  

    foreach ($rdm in $vmrdm) {

  

        if ($rdm.Name) {

        $row = "" | select Parent,Name,DiskType,ScsiCanonicalName,DeviceName,FileName,CapacityGB

      

        $row.Parent = $rdm.Parent

        $row.Name = $rdm.Name

        $row.DiskType = $rdm.DiskType

        $row.ScsiCanonicalName = $rdm.ScsiCanonicalName

        $row.DeviceName = $rdm.DeviceName

        $row.FileName = $rdm.FileName

        $row.CapacityGB = ($rdm.CapacityKB/1024)/1024

      

      

      

        $report += $row } # End if ($rdm.Name)

 

                  

} # End foreach ($rdm in $vmrdm)

 

 

} # End foreach ($vm in $allvms)

 

 

 

$report

 

 

 

 

 

(Remember to use the Connect-VIServer cmdlet before!)

 

 

Best regards,

Pablo

 

P.D: Got from this vmware's KB