Posts RSS Comments RSS 127 Posts and 199 Comments till now

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 player.

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 player.

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 player.

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

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}