A. Mikkelsen

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

Browsing Posts in PowerShell

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

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

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

Project Onyx Alpha Release

No comments

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

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.

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

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)

Today I had to create a script that creates lists of VMs, for each of the below statements:

  • CD-ROM is ‘Connected’
  • CD-ROM set to ‘Connect at power on’
  • CD-ROM device type set to ‘Client Device’
  • CD-ROM device type set to ‘Datastore ISO file’
  • Floppy is ‘Connected’
  • Floppy set to ‘Connect at power on’
  • Floppy device type set to ‘Client Device’
  • Floppy device type set to ‘Use excisting floppy image in datastore’
  • Serial Ports attached
  • Parallel Ports attached

Get it here

A fellow member in the Danish Usergroup has created a very simple backup script for ESX and ESXi using PowerShell and VCB.

http://www.virtualize.dk

I like that it’s small and simple.
Keep up the good work….

2 years ago a VMUG user in Denmark created a Ubuntu VM that could graficily could show the develeoment of the amount of VM’s in a Cluster.

Unfurtionally someone stole my labtop and the VM :-).

So I had to create a new. And of cause it’s based on PowerShell ;-).

The webpages shows the following information

  • Amount of VM’s in each Cluster
  • Amount of Hosts in each Cluster
  • The size of each Datastore and the amount of free disk space

Get the source code in the Download section under Vi Toolkit for Windows

Powered by WordPress Web Design by SRS Solutions © 2019 A. Mikkelsen Design by SRS Solutions