Posts RSS Comments RSS 253 Posts and 411 Comments till now

Setting SNMP configuration in VMWare using Powershell

I have a ton of respect for the VMWare guys and the incredibly useful set of cmdlets they provide (FOR FREE) found HERE, but man have I been banging my head against the wall trying to simply set the SNMP configuration on my ESX hosts.

To help ease the pain for the REST of the world here are some thoughts/gotchas.

– You must have the object return from Get-VMHostSNMP to use Set-VMHostSNMP or Test-VMHostSNMP. Simply providing the name will not work
– You cannot use the name with Get-VMHostSNMP. It appears you must connect to the actual ESX host (using connect-viserver) and then run Get-VMHostSNMP
– Once you “get” the VMHostSNMP object you can reuse without worrying about the connect-viserver command.
– You need to add a ReadOnlyCommunity to each host
– You need to Enable traps with the -enable:$true parameter
– TargetCommunity, TargetPort, TargetHost parameters require that you also pass -addtarget
– You need to enable SNMP on the ESX firewall

You may find the script below useful for getting a VMHostSNMP object for each ESXHost on your Virtual Center

Using the script below you can then do things like
$SNMPHostObjects = .\Get-SNMPHost.ps1 $VirtualCenter

To Add a Community
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -ReadOnlyCommunity “YourCommunity”}

To Enable
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -Enabled:$true }

To Add a Target
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -TargetHost “1.1.1.1” -TargetCommunity “YourCommunity” -AddTarget }

To Test
$SNMPHostObjects | %{ Test-VMHostSnmp $_ }

To Remove a Target
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -TargetHost “1.1.1.1” -removeTarget}

Incredible… simply incredible (free SAN)

Ever needed a SAN to play with? Ever wanted to play with MS Cluster services at home, but didn’t want to fork out the cash for shared storage?

Here is your FREE!!! solution

Leading Storage Virtualization Now Free With Unlimited Connections and Large 2TB Size

I have no clue how they can do this, but I am happy none-the-less.

NOTE: This product is super simple to use and great for testing VMWare/Xen/MS clusters.

blog: Thoughts on cmdlet design

This weekend I did a Powershell session at a codecamp in Philly. During my session I was reminded of a old blog post of mine and the discussion Gil and I had. I thought this would be a perfect post for TheExpertsCommunity. I would love feedback on your thoughts.

I wanted to make a case for using task based cmdlets instead of methods when designing Snapins. There have been several "Vendors" that have produced cmdlets for Powershell: Exchange, Citrix, VMWare, and IBM to name a few. Most have done very well here, but this is one thing I think the VMware team excelled at.

Here is the basic Goal
  1. Get-Something | Filter | Change-Something | Save-Something

The thing to avoid: Depending on methods for object task.
  1. Get-Something | %{$_.DoSomething()}
Here is an Example of what I mean
Lets say we have a Car Object (class). The Car object has Properties like Make, Model, Color, TireCount, Size, and Type. We also have things we can do with a car like start , turn off, stop, turn, load, and unload.

We could approach this by creating a Car class with the set properties and methods. This may seem simpler, but it is not intuitive for your typical Admin. Your typical admin does not want to do this
  1. Get-Car | ?{$_.Type -eq "MiniVan"} | %{$_.LoadPeople()} | %{$_.Start()} | %{$_.Turn("Right")} | %{$_.Stop()} | %{$_.UnLoadPeople()}

Ideally from an Admin perspective a bunch of Task oriented cmdlets would be your best bet. Let’s assume you had these cmdlets instead of Methods:
  • Get-Car
  • New-Car
  • Remove-Car
  • Start-Car
  • Stop-Car
  • Invoke-TurnCar
  • Invoke-LoadCar
  • Invoke-UnLoadCar
  • Set-Car
Your admin can now do this
  1. Get-Car -Type "MiniVan" | Invoke-LoadCar | Start-Car | Invoke-TurnCar -Right | Stop-Car | Invoke-UnloadCar
This reads more like a sentence than script syntax.

Using Get-Credential to Store Passwords “securely” in a file.

Lee Holmes of PowerShell Cookbook fame has a post here Importing and Exporting Credentials in PowerShell. After fielding a few questions on EE regarding this post.. I wrote these two scripts to make it a little simpler.

Set-myCredential: This will prompt you for credentials and store them in the file specified.

#####################
#Set-myCredential.ps1
Param($File)
$Credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content $File
#####################

Get-myCredential: This will get you credentials from a file specified. It require you know the user name.

#####################
#Get-myCredential.ps1
Param($User,$File)
$password = Get-Content $File | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($user,$password)
$credential
#####################

With these two script you can do something like this (using VMware Toolkit for example.)

c:\scripts\Set-myCredential.ps1 c:\tools\mp.txt
$creds = c:\scripts\Get-myCredential.ps1 MyUserName c:\tools\mp.txt
Get-ViServer MyVirtualCenter -cred $creds

Vmotion with VI ToolKit (Playing Around Series)

I wanted to show you how simple it was to VMotion with new CMDLets. I also want to point out the most if not all the VMware CMDLets return VM Objects so you could easily do something like
Get-VM MyVM | Stop-VM | Remove-CDDrive | Move-VM -Dest $EsxHost | New-CDDrive -isoPath $isoPath -StartConnected | Start-VM

Best Viewed Full Screen

Get the Flash Player to see this content.


Demo File

# Connect to Virtual Center
Get-VC home.halr9000.com
# Get ESX host
$esxHost = Get-VMHost 192.168.0.55
# Verify we got the correct ESX Host
$esxHost
# Get Virtual Machine
$vm = Get-VM SDK-XPSP2
# Verify We have the correct VM
$vm
# Test VM move with -whatif
Move-VM -VM $VM -Destination $EsxHost -whatif
# Lets do it for real
Move-VM -VM $VM -Destination $EsxHost

Controlling VMWare VMs with VI Toolkit (Playing Around Series)

In this video I show starting, Pausing, Resuming and Stopping VMs using Get-VM, Start-VM, Suspend-VM, and Stop-VM

Note: These are fast run throughts, but you can pause and read the comments.

BEST VIEWED FULL SCREEN

Get the Flash Player to see this content.


Demo File

# Connect to Virtual Center
Get-VC home.halr9000.com
# View our current VMs
Get-VM
# Pick Specific VM
get-vm SDK-XPSP2
# To start VM
get-vm SDK-XPSP2 | Start-VM
# To pause VM
get-vm SDK-XPSP2 | Suspend-VM -Confirm:$False
# To resume it again
get-vm SDK-XPSP2 | Start-VM
# To stop VM
get-vm SDK-XPSP2 | Stop-VM -Confirm:$False

Intro to VMWare VI Toolkit for Windows (Playing Around Series)

I know there are a good bit of people out there that are unable install and play with the new VMWare toolkit. So in this post I though I would record some of my playing around with Get-VM 🙂

I show Get-VM, Get-HardDisks, Get-CDDrive, Get-NetworkAdapter, and Get-FloppyDrive. I recommend pausing and reading the video as you go.

Note: This is a fast run through. I recommend pausing and reading the Comments.

BEST VIEWED FULL SCREEN

Get the Flash Player to see this content.

Some shout outs:
To Hal from PowerScripting.Net for use of his Lab for recordings. Here is his blog: Hals Blog
To Camtasia for incredible product:
Click Here
To VMWare for some Great CMDLets: Click Here

Demo File

Get-VC home.halr9000.com
# Lets look at the all the commands we have.
Get-Vicommand | more
# Now lets look at Get-VM
Get-VM
# What properties do we have for the VMs
Get-VM | Get-Member -type property
# Lets look at the Hard Disks
Get-VM | Get-HardDisk
# Here are the properties for a HardDisk
Get-VM | Get-HardDisk | Get-Member -type Property
# Lets look at VMs with over 2gb of disk
Get-VM | Get-HardDisk | ?{$_.CapacityKB*1kb -gt 2gb}
# What about 4gb
Get-VM | Get-HardDisk | ?{$_.CapacityKB*1kb -gt 4gb}
# Now lets look at the CD Drives
Get-VM | Get-CDDrive
# What about the properties available
Get-VM | Get-CDDrive | get-member -type property
# Lets make sure none of the CD’s have ISO’s attached
Get-VM | Get-CDDrive | ?{$_.ISOPath}
# How bout NICs
Get-VM | Get-NetworkAdapter
# what do they have to offer?
Get-VM | Get-NetworkAdapter | get-member -type property
# Lets look at all the NICs, but only the Mac and NetworkName
Get-VM | Get-NetworkAdapter | select MacAddress,NetworkName
# Finally, Lets take a quick peak at floppies
Get-VM | Get-FloppyDrive

cmdlet Design Suggestion

I wanted to make a case for using task based cmdlets instead of methods when designing Snapins. There have been several “Vendors” that have produced cmdlets for Powershell: Exchange, Citrix, VMWare, and IBM to name a few. Most have done very well here, but this is one thing I think the VMware team excelled at.

< sidebar >
As some of you may know, I have a long background in Active Directory. As an Ex-Microsoft Support Professional in the Directory Services Team and later a Rapid Response Engineer specializing in Active Directory, let’s just say I have passion for all things AD.
I was lucky enough to spend a significant amount of time with the DS team at MS responsible for the Powershell cmdlets they will release at some time in the future. While I cannot give any details I can say… OMG! I CANNOT WAIT to be able to talk about them.
< / sidebar >

Here is the basic Goal

Get-Something | Filter | Change-Something | Change-SomethingElse | Save-Something

The thing to avoid: Depending on methods for object task.
Get-Something | %{$_.DoSomething()}

Here is an Example of what I mean
Lets say we have a Car Object (class). The Car object has Properties like Make, Model, Color, TireCount, Size, and Type.
We also have things we can do with a car like start , turn off, stop, turn, load, and unload.

We could approach this by creating a Car class with the set properties and methods. This may seem simpler, but it is not intuitive for your typical Admin. Your typical admin does not want to do this

Get-Car | ?{$_.Type -eq "MiniVan"} | %{$_.LoadPeople()} | %{$_.Start()} | %{$_.Turn("Right")} | %{$_.Stop()} | %{$_.UnLoadPeople()}

Ideally from an Admin perspective a bunch of Task oriented cmdlets would be your best bet. Let’s assume you had these cmdlets instead of Methods:
Get-Car
New-Car
Remove-Car
Start-Car
Stop-Car
Invoke-TurnCar
Invoke-LoadCar
Invoke-UnLoadCar
Set-Car

Your admin can now do this

Get-Car |  ?{$_.Type -eq "MiniVan"} | Invoke-LoadCar | Start-Car | Invoke-TurnCar -Right | Stop-Car | Invoke-UnloadCar

This reads more like a sentence than a script syntax.

VMWare and Powershell, The Early Years… The Life Before VMWare Powershell CMDLets

I am on the CTP for the VMWare Powershell Toolkit, and although I am unable to release any Information. I thought it would be a good idea to post some Powershell VMWare commands (old school) before they go live with the REAL Deal.

Hal from TechProsaic wrote a blog post here http://halr9000.com/article/445 about Using Plink to get Information about ESX and creating a custom object for the VM’s that the ESX host currently runs. I really liked the script, but I wanted a little more out of it. So I modified it (sorry Hal) and add some functionality. Below is a list of things it does.

  • Get Virtual Machines Running on ESX Host. Returns Custom Object
  • Provides a similar function to Get-Process from the ESX Host
  • Will Run a generic Command on the ESX Host
  • NOTE: At lines 16/31/62 I am having some issues with syntax highlight changing these to email addresses. Please make user the end up as $user @ $srv (no spaces)

    You can also get a working copy here http://powershellcentral.com/scripts/54

    # Invoke-VMCommand.ps1
    # Purpose     : Run a remote command and return the results
    # Requirements: plink.exe from the Putty project must be in $env:path
    # Use -help parameter for instructions

    Param (
        $VMHost,
        $username,
        $Command,
        [switch]$Help,
        [switch]$Verbose
    )

    # Obtains list of VMX (config files) corresponding to each VM on a given ESX server
    function GetVMX ($user, $pass, $srv) {
        $cmd = "plink.exe $user@$srv -pw $pass"
        $cmd += " vmware-cmd -l"
        Write-Verbose "Command line: $cmd"
        $VMList = Invoke-Expression $cmd
        $collOut = @()
        $VmList | ForEach-Object {
            $objOut = "" | Select-Object VmHost, VmName, VMXpath, HasSnapshot # create our output object with desired properties
            $objOut.VmHost = $srv
            $objOut.VMXpath = $_
            $objOut.VmName = (Split-Path $_ -Leaf) -replace ".vmx$"
            $collOut += $objOut
        }
        $collOut
    }
    function Get-ESXProcess($user, $pass, $srv){
        $cmd =  "plink.exe -t $user@$srv -pw $pass "
        $cmd += "`"ps -Af | grep `’`’`""
        Write-Verbose "Command line: $cmd"
        $results = invoke-Expression $cmd
        $colObj = @()
        foreach($result in $results)
        {
            if($result -match "^UID"){continue}
            $myobj = "" | Select-Object UID,PID,PPID,C,STIME,TTY,TIME,CMD
            $ary = $result.split([string[]]" ",[System.StringSplitOptions]::RemoveEmptyEntries)
            $myobj.UID   = $ary[0]
            $myobj.PID   = $ary[1]
            $myobj.PPID  = $ary[2]
            $myobj.C     = $ary[3]
            $myobj.STIME = $ary[4]
            $myobj.TTY   = $ary[5]
            $myobj.Time  = $ary[6]
            $proc = $null
            write-verbose "Length: $($ary.Length)"
            for($i = 7;$i -le $ary.Length;$i++)
            {
                $proc = "$proc $($ary[$i])"
                write-Verbose "Adding [$i] $($ary[$i])"
            }
            Write-Verbose "COMMAND = $proc"
            $myobj.CMD   = $proc
            $colObj += $myobj
        }
        $colObj
    }
    function RunVMCommand ($user, $pass, $srv, $vmcmd) {
        $cmd = "plink.exe $user@$srv -pw $pass "
        $cmd += "`"$vmcmd | grep `’`’`""
        Write-Verbose "Command line: $cmd"
        invoke-Expression $cmd
    }
    function GetSecurePass ($SecurePassword) {
      $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($SecurePassword)
      $password = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
      [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
      $password
    }

    # Returns help text
    function ShowUsage {
      $helptext = @"

    Invoke-VMCommand
      Requirements: plink.exe from the Putty project must be in your Path

    INPUT:

      VMHost     : name or IP of ESX server(s) (REQUIRED)
      UserName   : User to ssh With (REQUIRED)
      Command    : Command to Run. This can be a GetVMX, PSList, or a Custome String (REQUIRED)
      Help       : shows usage

    "@
      Write-Host $helptext
    }

    # Main
    if ($help) { ShowUsage; exit; }
    if ($verbose) { $verbosepreference = "continue" }

    $password = (Read-Host "Enter Password" -AsSecureString)

    if($Command -eq "GetVMX"){GetVMX $username (GetSecurePass $password) $VMHost}
    elseif($command -eq "PSList"){Get-ESXProcess $username (GetSecurePass $password) $VMHost}
    else{RunVMCommand $username (GetSecurePass $password) $VMHost $Command}