PowerShell script exchange
Post Reply
Martin9700
Influencer
Posts: 17
Liked: 3 times
Joined: Nov 10, 2010 2:18 pm
Full Name: Martin Pugh
Location: Massachusetts
Contact:

From Newsletter - Getting screenshots

Post by Martin9700 » 3 people like this post

Saw Gostov's newsletter about getting screen shots from vSphere and had to write a little Powershell script to get the MoRef and launch the web page for you. Source code is on Spiceworks: http://community.spiceworks.com/scripts ... -your-vm-s


Didn't want to post to another community only (not sure if that's ok or not) so here is the script:

Code: Select all

<# 
.SYNOPSIS
	Simple script to retrieve a screenshot of a virtual server.
.DESCRIPTION
	Simple script that accepts input from parameter or the pipeline and will 
	start up a web page with a screenshot of the console page for the specified
	virtual machines.  
	
	This is a capability already present in VMware vSphere 5 and this script 
	just simplifies getting the proper information.  
	
	Requirments:  VMware vSphere 5 or greater
	              VMware PowerCLI
				  
	Don't forget to edit the PARAM section (specifically $VMHost) to match
	your environment.
	
	Special Thanks to Hubba Bubba at Spiceworks for testing this for me.
.PARAMETER Name
	Name, or names for the virtual servers you wish to view.  Accepts input
	from the pipeline or it will prompt you.  Can be many servers.
.PARAMETER VMHost
	Name of your vCenter server, or the vSphere host server.  
.EXAMPLE .\Get-VMScreenshots.ps1 -Name myvm -VMHost myvCenter
	Will open your default browser and show the current console screen for "myvm".
.EXAMPLE Get-Content Servers.txt | .\Get-VMScreenshots.ps1
	Will pull each server name out of Servers.txt and display the screen shot
	for those VM's.
.EXAMPLE Get-VM *TEST* | .\Get-VMScreenshots.ps1
	Will execute the Get-VM cmdlet, searching for all VM's with TEST in their
	names and display those screen shots.
.NOTES
	Author:         Martin Pugh
	Twitter:        @thesurlyadm1n
	Spiceworks:     Martin9700
	Blog:           www.thesurlyadmin.com
	
	Changelog:
		1.0         Initial release
.LINK
	http://community.spiceworks.com/scripts/show/1748-get-vmscreenshot-get-screen-shots-from-the-console-session-of-your-vm-s
.LINK
	http://www.youtube.com/watch?v=flo0bMs6hjY
#>
Param (
	[Parameter(Mandatory=$true,
		ValueFromPipeline=$true,
		ValueFromPipelineByPropertyName=$true)]
	[String[]]$Name,
	[string]$VMHost = "vCenterServer"
)

Begin {
	#Load VMWare CLI cmdlets
	Try	{ 
		If (-not (Get-PSSnapin VMware.VimAutomation.Core))
		{	Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop 
		}
	}
	Catch { 
		Write-Host "`n`nUnable to load VMware CLI cmdlets, are they installed on this host?"; Break 
	}
	#Now connect to vCenter using your credentials
	Connect-VIServer $VMHost -ErrorAction Stop
}

Process {
	Foreach ($VM in $Name)
	{	$MoRef = (Get-VM $VM).ExtensionData.MoRef.Value
		Start-Process -FilePath "https://$VMHost/screen?id=$MoRef"
	}
}
Accepts input from parameters, will prompt you if you don't enter anything too. Will also accept input from pipeline, so Get-Content, Get-VM all work.

Get-Content servers.txt | .\Get-VMScreenshot.ps1
Get-VM *Test* | .\Get-VMScreenshot.ps1
.\Get-VMScreenshot.ps1 -Name "server1","server2"


Enjoy!
Martin
www.thesurlyadmin.com
@thesurlyadm1n
Vitaliy S.
VP, Product Management
Posts: 27120
Liked: 2720 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: From Newsletter - Getting screenshots

Post by Vitaliy S. »

Thanks for sharing!
Martin9700
Influencer
Posts: 17
Liked: 3 times
Joined: Nov 10, 2010 2:18 pm
Full Name: Martin Pugh
Location: Massachusetts
Contact:

Re: From Newsletter - Getting screenshots

Post by Martin9700 »

Talked about how I created this script here:
http://thesurlyadmin.com/2013/01/31/dis ... owershell/
Martin
www.thesurlyadmin.com
@thesurlyadm1n
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 16 guests