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 ) {
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 }

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.