A. Mikkelsen VMware ESX scripts, commands, tools and other nice to know things that will make your virtualization days easier!!!!

Thursday, March 18, 2010

The Must Have – Powershell PodCasts

Filed under: PodCasts (PS),PowerShell,Scripting — Tags: , , — A. Mikkelsen @ 13:39 pm

A few weeks ago I came across a podcast on http://get-scripting.blogspot.com about Powershell.

I highly recommend that all people using powershell check it out.

Get the latest and the archived podcasts at
http://get-scripting.blogspot.com/search/label/podcast

Wednesday, March 10, 2010

Powershell – List all vCenter client sessions & disconnect if inactive

Filed under: PowerShell,Scripting,Virtual Center — Tags: , , , — A. Mikkelsen @ 14:25 pm

Today I needed to create a list of all users connected to a vCenter sever through the Client.
And during so I created a few variants 🙂

This creates a list of all usernames connected to the vCenter server through a vCenter Client.

$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$sessMgr = get-view $serviceInstance.Content.sessionManager
foreach ($sess in $sessMgr.SessionList){
   write "$($sess.UserName)"
}

If you want to see a witch clients have been idle for 60 minutes try this.

$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$sessMgr = get-view $serviceInstance.Content.sessionManager
foreach ($sess in $sessMgr.SessionList){
    if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)){
        write "$($sess.UserName)"
      }
}

If you the want to terminate the sessions that have been inactive  for more than 60 minute

$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$sessMgr = get-view $serviceInstance.Content.sessionManager
$oldSessions = @()
foreach ($sess in $sessMgr.SessionList){
    if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)){
        $oldSessions += $sess.Key
      }
}
$sessMgr.TerminateSession($oldSessions)

You can modify the hell out of this script to suit your needs and be my guest….

You can download the full script here.

The scripts are based on code found  at the PowerCLI community by LucD.
http://communities.vmware.com/message/914858#914858

Friday, February 26, 2010

ESXHealthScript release 49

Filed under: ESX HealthCheck,Tools — Tags: , , , — A. Mikkelsen @ 18:08 pm

Yesterday we released version 49 of the ESXHealthScript.

Kenneth Mazie. (kcmazie aka. kcmjr) has done a huge job in updating and adding new features to the script.

  • Added automatic detection for ESX 3 or ESX 4
  • Added corrections for changes with ESX4
  • Added Cisco CDP info listing (from user Carson)
  • Added corrections to v4 tomcat server

To read more about the ESXHealthScript please visit the project web site.
https://sourceforge.net/projects/esxhealthscript/

If you are using the script, and have ideas for new features/functions, please let us know on the projects website or even better post the needed code 🙂

Sunday, January 31, 2010

Playing with PowerCLI

Filed under: PowerShell — Tags: , , , , , , , — A. Mikkelsen @ 21:45 pm

Last week I attended a Magirus course on administrating you VMware environment using PowerCLI, and below is some of the small scripts I created.

These code sniplets will help you manage your VMware environment and give you some ideas of how powerful the VMware PowerCLI really is.

I’m sure VMware will add even more CMDLETS to the PowerCLI in the feature.

Get the latest PowerCLI here.
A function to load different PSSnapins.
Put it in the beginning of all you Powershell scripts,
to load the different PSSnapin you need.


function LoadSnapin{
  param($PSSnapinName)
  if (!(Get-PSSnapin | where {$_.Name   -eq $PSSnapinName})){
    Add-pssnapin -name $PSSnapinName
  }
}
LoadSnapin -PSSnapinName   "VMware.VimAutomation.Core"

Clone a VM to template.


$VMToClone = "vm_name"
$TemplateName = "TemplateName"
$Datacenter = "Training"
get-vm $VMToClone| stop-vm
New-Template -VM $VMToClone -Name $TemplateName 
   -Location $(Get-Datacenter $Datacenter)

Convert Template to VM – without changing the name.


$TemplateName = "TemplateName"
Set-Template -Template $(get-template $TemplateName) -ToVM

Convert VM to Template – without changing the name.


$VMtoTemplate = "vm_name"
$vm = Get-VM $VMtoTemplate | Get-View
$vm.MarkAsTemplate()

Deploying a VM from template.


$strNewVMName = "NewVM_01"
$strTemplate = "TemplateName"
$strDestinationHost = "ESX01"
New-VM -Name $strNewVMName -Template $(get-template   $strTemplate) 
   -VMHost $(Get-VMHost $strDestinationHost)

Deploying a VM from template using a Customization Specification and using Thin provisioning.
Make sure the CustomSpec has been created beforehand.


$strNewVMName = "NewVM_01"
$strTemplate = "TemplateName"
$strDestinationHost = "ESX01"
$strCustomSpec = "TEST-CustomSpec"
New-VM -Name $strNewVMName -Template $(get-template $strTemplate) 
   -VMHost $(Get-VMHost $strDestinationHost) -DiskStorageFormat 
   Thin -OSCustomizationSpec $(Get-OSCustomizationSpec $strCustomSpec)

Moving a VM to a specific folder.


$strDistinationFolder = "MyFolder"
$strDatacenter = "Training"
$VMToMove = "MyVM"
move-vm -VM $(get-vm $VMToMove) -Destination $(Get-Folder 
    -Name $strDistinationFolder -Location $(Get-Datacenter $strDatacenter))

Copying a file to a Windows VM (With or without network access)
Requires VMware tools to be running.


$VM = get-vm -name "myVM"
$target = "C:\MY_DIR\"
$source = "C:\MY_DIR\test.txt"
Copy-VMGuestFile -Source $source -Destination $target -vm $VM 
   -LocalToGuest -HostUser "root" -HostPassword "password" 
   -GuestUser "myVM\administrator" -GuestPassword "password" 
   -Force:$true

Copying a file from a Windows VM (With or without network access)
Requires VMware tools to be running.


$VM = get-vm -name "myVM"
$target = "C:\MY_DIR\"
$source = "C:\MY_DIR\test.txt"
Copy-VMGuestFile -Source $source -Destination $target -vm $VM 
   -GuestToLocal -HostUser "root" -HostPassword "password" 
   -GuestUser "myVM\administrator" -GuestPassword "password" 
   -Force:$true

Listing the content of “C:\Windows\System32” from a VM – remotely


$VM = get-vm -name "myVM"
Invoke-VMScript -VM $VM -ScriptText "dir" -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password"

Run msinfo32 on a guest VM and pipe the output to a TXT file – Using PowerShell.


$VM = get-vm -name "myVM"
$script = '&"$env:ProgramFiles\Common Files\Microsoft Shared\
   MSInfo\msinfo32.exe" /report "$env:Tmp\inforeport.txt"'
Invoke-VMScript -VM $VM -ScriptText $script -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password"

Open the above output file in the guest VM – Using PowerShell.


$VM = get-vm -name "myVM"
$script = '&"notepad.exe" "$env:Tmp\inforeport.txt"'
Invoke-VMScript -VM $VM -ScriptText $script -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password" -ScriptType Powershell

Run msinfo32 on a guest VM and pipe the output to a TXT file – Using batch commands.


$VM = get-vm -name "myVM"
$script = '&"%programfiles%\Common Files\Microsoft Shared\
   MSInfo\msinfo32.exe" /report "%tmp%\inforeport.txt"'
Invoke-VMScript -VM $VM -ScriptText $script -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password" -ScriptType Bat

Open the above output file in the guest VM – Using batch commands.


$VM = get-vm -name "myVM"
$script = '"notepad.exe"   "%Tmp%\inforeport.txt"'
Invoke-VMScript -VM $VM -ScriptText $script -HostUser "root" 
   -HostPassword "password" -GuestUser "myVM\administrator" 
   -GuestPassword "password" -ScriptType Bat

Wednesday, November 18, 2009

Project Onyx Alpha Release

Filed under: PowerShell,Project Onyx,Scripting — Tags: , — A. Mikkelsen @ 18:05 pm

Finally everyone outside the closed beta is now able to get hands on with the cool application Project Onyx.
Thanks to Carter Shanklin

Project overviewGot it from http://www.ntpro.nl/blog/archives/1330-Project-Onyx-Alpha-Release.html

Onyx is a standalone application that serves as a proxy between the vSphere Client and the vCenter Server. It monitors the network communication between them and translates it into an executable PowerShell code. Later, this code can be modified and saved into a reusable function or script.

  1. To connect to a server, click the “Connect” icon.
  2. Enter the IP of the vCenter Server on which you want to work and click Connect.
  3. Connect with any of the following clients:For vSphere Client, in the “IP Address” field enter the following address:
    http:// [IP of the machine where Onyx is running] : [listening port]

    For vSphere PowerCLI, run the following command:
    Connect-VIServer [IP of the machine where Onyx is running] -Protocol http
    -Port [listening port] -User [username] -Pass [password]

  4. To start the generation of PowerCLI.NET scripts, click the “Start” icon.
  5. To save the generated script, click the “Save output to file” icon.

You can download Project Onyx Alpha from http://blogs.vmware.com/files/onyx_1.5.3607.25516.zip or http://bit.ly/vmwOnyx15.

To learn how it works please see this video from YouTube.com

Monday, October 5, 2009

Powershell – Add host to Cluster and license it.

Filed under: PowerShell — Tags: , , , , , — A. Mikkelsen @ 19:40 pm

This PowerShell script adds a host to a Cluster and licenses it.


Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer "VC_server_name" -User "Administrator" -Password "password"
add-vmhost "esxhost" -location (get-datacenter -name 'Datacenter' | 
   get-folder -name 'Folder' | get-cluster -name 'Cluster') -user "root" 
   -password "password" -force: $true
$targethostMoRef = (get-VMHost $strHost  | get-view).MoRef
$si = Get-View ServiceInstance
$LicManRef=$si.Content.LicenseManager
$LicManView=Get-View $LicManRef
$licassman = Get-View $LicManView.LicenseAssignmentManager
$licassman.UpdateAssignedLicense($targethostMoRef.value,”YOUR LIC KEY”,
   ”vSphere4 Enterprise Plus (1-12 cores per CPU”)
Disconnect-VIServer -Confirm:$false

Download it here.

Thursday, September 17, 2009

A must have for anyone using PowerShell to control VMware

Filed under: ESXi,PowerShell,Scripting,Tools,Virtual Center,vSphere (ESX) — Tags: , , , , , , , , — A. Mikkelsen @ 12:12 pm

VMware have released a demo on YouTube that shows a “must have” for PowerShell users who automate and manage vSphere and vCenter through PowerShell.

The project is named “Onyx” and works like a middelware between the vCenter Client and the vCenter Server.
Onyx is unfortunately still in a private beta and therefore not acceble to the Public…….. 🙁

Read more on rtfm-ed.co.uk.
http://www.rtfm-ed.co.uk/?p=1643

Or read more on the “get-scripting podcast” blog
http://get-scripting.blogspot.com/2009/08/get-scripting-podcast-episode-12-carter.html

Wednesday, September 16, 2009

Scripts to document your vSphere enviroment

Filed under: ESX HealthCheck,PowerShell — Tags: , , — A. Mikkelsen @ 15:13 pm

http://communities.vmware.com/message/1286580#1286580

http://www.ivobeerens.nl/?p=256

http://www.configuresoft.com/esx-compliance-checker.aspx

http://www.vmware.com/support/developer/vima/

http://www.run-virtual.com/?p=233 (VMware CPU Host Info)

http://teckinfo.blogspot.com/2009/01/vdiagram-document-your-vi-with-one.html (vDiagram)

Thursday, July 9, 2009

The Original ESX HealthCheck script – Updated 0.30

Filed under: ESX HealthCheck,Scripting,Tools — Tags: , — A. Mikkelsen @ 13:10 pm

After almost 2 years of silence a new version of the ESXHealthScript has surfaced.

Version 0.30 is released.

Wednesday, July 8, 2009

PowerCLI To WMI via PowerWF

Filed under: Scripting — Tags: , , , , , — A. Mikkelsen @ 21:37 pm

In this demo Ben (@xcud) takes a PowerCLI script that’s been transcribed into workflow activities and drop a ‘ToWMI’ activity at the end which causes the workflow to push the data retrieved from PowerCLI into WMI when the workflow is run. The workflow is set to run automatically every 5 seconds by being deployed as a Task in the Agent. The data is shown both on the PowerShell commandline and in a PowerGadget digital readout. Download the beta now: http://powerwf.com/

(Text copied from NTPRO.NL)

« Newer PostsOlder Posts »

Powered by WordPress