Category Archives: technology

Export vCenter Roles

Below is a powershell script to export vCenter roles to a ready to import set of commands.

Script below will not work without using PowerCLI or being connected to vCenter.

$viRolesList = Get-VIRole;
Foreach ( $role in $viRolesList ) {
     $roleName = $role.Name;
     $privilegeSet = $role | Get-VIPrivilege;

     Echo "New-VIRole -Name ""$roleName""" >> vRoles.txt;

     Foreach ( $privilege in $privilegeSet ) {
         $privilegeID = $privilege.ID;
         Echo "Set-VIRole -role ""$roleName"" -AddPrivilege `
         (Get-VIPrivilege -ID $privilegeID)" >> vRoles.txt;
     }
}

Compare VM Name to VM Filename

In case you were blind sided by the vsphere 5.x bug fix that removes the storage vmotion feature that renames the VM’s folder and files. Below is a script that will at least help you identify the VMs that don’t match their file names.

Open powerCLI
Connect to your vcenter

$colorObject = get-wmiobject -class "Win32_Process" -namespace "root\CIMV2";
$vmCluster = Read-Host "Cluster Name";
$vmList = Get-Cluster $vmCluster | Get-VM;
Foreach ( $vm in $vmList ) {
 $vmView = $vm | Get-View;
 $vmView.Config.Files.VmPathName -match "^.*\/(.*)\.vmx$";
 $vmFName = $matches[1];
if ( $vm.Name -match $vmFName ) {
 write-host "$vm matches $vmFName";
 } else {
 write-host "$vm does not match $vmFName" -foreground "red";
 }
}

List Files Last Modified 30 Days

How to list files last modified in the last 30 days via Powershell.  Run these commands in the directory containing the files to filter.

$lastModAge = (get-date).adddays(-30);
Get-ChildItem | Where { $_.LastWriteTime -gt $lastModAge }

NSLookup Sweep

I keep finding the need to find an open IP address for a new server, and I get tired of searching manually.  I am also not patient enough for a ping sweep, so I created a nslookup sweep with powershell. The nslookup sweep just looks through a subnet to find the first address without a dns entry. The next step just requires you to ping the address to insure it’s not being used.  Check out this post for a TCP Port Scanner in PowerShell.


$i = "1";
$subnet = "192.168.1";
while ( $i -lt 255 ) {
$ipaddr = "$subnet.$i"
$nsResult = nslookup $ipaddr 2>&1 | select-string "Name";
if ( $nsResult ) {
$i++;
}else{
echo "$ipaddr is available";
$i = "256";
}
}


PowerCLI and Storage

Below are some interesting sets of PowerCLI commands that will give you more information on your VM’s hard drives and your datastores.

The below will give you the total allocated hard drive space of all the VM’s in a cluster.

Get-Cluster "Cluster Name" | get-vm | Get-HardDisk | %{ $vmCapacity += $_.capacityKB }
$vmCapacity

The below will give you the Total Capacity, Free Space, and Usage of your datastores.

Get-Datastore | %{ $datastoreFreeSpace += $_.FreeSpaceMB }
Get-Datastore | %{ $datastoreTotalCapacity += $_.CapacityMB }
$datastoreUsageTotal = $datastoreTotalCapacity - $datastoreFreeSpace

write-host "Total Capacity: $datastoreTotalCapacity MBs"
write-host "Total Free Space: $datastoreFreeSpace MBs"
write-host "Total Usage: $datastoreUsageTotal MBs"


Cron Job Reminders

Lately I have found that I need a better system of reminding myself about time sensitive tasks. So after some searching and testing, I have found this solution. The solution is a Cron Job with the notify-send tool. This is particularly useful using Gnome 3.

First run the command crontab -e to in a terminal to edit your cron jobs.

You should now be using VI to edit your Cron Jobs.

Press you are now in insert mode

Then add a line similar to the one below.

0-30/10 13 * * 1-5 DISPLAY=:0.0 notify-send Test “Hello”

The above line adds a notification entitled Test with the message Hello.  It also starts notifying you ever 10 minutes from 1:00PM to 1:30PM daily.  Just delete the line if you do not want it to continue.

Then save and exit, your VI session. Press Esc then type :wq and press Enter.


SSH Tunnel

The other day I came across the need to setup an SSH Tunnel for remote management.  The idea of using an SSH tunnel is that the data going through the tunnel is encrypted.  So an extra layer of security.

This post assumes you know how to do basic networking, setup an ssh server, and reasonable computer skills.

First step is to setup an SSH server within the network you want to tunnel into.

Second allow for the SSH server to be connected to from the outside.

Now the fun begins:

Get outside of your network.

Install an ssh client

Now lets pretend you want to tunnel in and connect to your internal web server.

In a terminal window type the below command.

ssh -L 80:internalwebserver:80 -l username sshserver

Once you enter your password, go to your web browser and go to http://localhost

You should have been directed to your internal web server.

Congratulations you have setup your first successful SSH Tunnel.


Get Total Space Used by VMs

The other day I was in need of finding out how much space my VMs actually take up.  Below are the commands I used in the vSphere PowerCLI to get that information.

      
$TotalDisk = 0;
#Get Sum of all Disks in TBs
Get-VM | Get-HardDisk | %{$TotalDisk+=[int]$_.CapacityKB}
$TotalDisk = $TotalDisk/1024/1024/1024;
echo "$TotalDisk TBs"


vSphere Resource Allocation and Memory Ballooning

Recently I increased the amount of RAM on a VM, but I forgot to increase the Limit – MB under the Resource Allocation tab on the pool. So my VM had 6144MBs of RAM but was only allowed 4096MBs. Because of this the VM tools on the VM thought that the ESX Host was running low on physical memory and started the ballooning process. This caused the memory usage on the VM to go through the roof, causing the VM to have major issues.

To fix this issue, I set the Limit – MB to the amount of memory I had assigned to the VM and the VM’s memory usage drastically went down.

So to summarize my findings… If the Limit – MB is set lower than the amount of memory the VM has, it can cause ballooning. Also if the VM is getting close to using the set limit then it could cause ballooning.

For more information on vSphere Memory usage, and Memory Ballooning check out the links below.
http://www.vmware.com/files/pdf/perf-vsphere-memory_management.pdf
http://www.vmware.com/pdf/vsphere4/r40/vsp_40_resource_mgmt.pdf


Grub 2 Set Default Boot Option

Here is something I found the need to do the other day.  I needed to change the default option on my grub 2 setup.

First you will need to know the menu item title you want to set grub 2 to boot to.

Open the /etc/grub2.cfg file to get a look at your grub menu items.

su -c 'vi /etc/grub2.cfg'

Do not make changes.

Look for entries like the line below…

menuentry 'Fedora Linux, with Linux 3.1.6-1.fc16.x86_64' --class fedora --class gnu-linux --class gnu --class os {

The 'Fedora Linux, with Linux 3.1.6-1.fc16.x86_64' is an example of a menu item title.

Now copy the menu item title you want grub 2 to use as the default, and use it in the command below.

su -c 'grub2-set-default "menu title item"'

or as an example

su -c 'grub2-set-default "Fedora Linux, with Linux 3.1.6-1.fc16.x86_64"'

Now restart and it should boot to the menu option you chose.