Category Archives: PowerShell

Automated deployment & configuration of Log Insight with PowerCLI

As part of my datacenter build I needed to install and configure Log Insight automatically, this is easier than it sounds, its easy enough to deploy and OVA file with PowerCLI but the configuration of Log Insight is all done via a configuration website and not through OVF properties or VM properties.

What’s more, the normal trick of using Invoke-VMscript to reach inside the VM and perform some configuration by manipulating files would not work as the administrator password is not set until you complete the web page configuration.

Thankfully PowerShell has some great ways to manipulate Internet Explorer, with this I was able to automatically fill in the website and configure Log Insight to automatically connect to my infrastructure to start collecting data.  For debug purposes I have left the automation of internet explorer visible in the below script when it is run, this can easily be turned off by altering the $ie.visible = $true line below to change it to $false.

You will see my script automatically chooses a host and datastore based upon the cluster and the size needed, you can of course change it for your environment if you would prefer to choose where it was deployed.

Don’t forget that as a follow on you can also bulk configure your ESXi hosts to send their logs to Log Insight with the following PowerCLI script.

Sample Screenshots

image

SNAGHTML15f6ad

The Script

Connect-VIServer 192.168.1.50 -user "administrator@vsphere.local" -pass vmware
$LIInstallFile = "C:\tmp\VMware-vCenter-Log-Insight-1.0.4-1169900_OVF10.ova"
$LIName = "LI01"
$LINetwork = "VM Network"
$LIIP = "192.168.1.150"
$LISNM = "255.255.255.0"
$LIDGW = "192.168.1.1"
$LIDNS = "192.168.1.1"
$LICluster = "Production"
$LIEmail = "myemail@vsphere.local"
$LIPassword = "VMware123!"
$LISMTPServer = "mail.vsphere.local"
$LISMTPport = 25 
$LILicense = "YOUR-KEY-GOES-HERE"
$LINTP = "0.us.pool.ntp.org, 1.us.pool.ntp.org, 2.us.pool.ntp.org, 3.us.pool.ntp.org"
$LIvC = "192.168.1.50"
$LIvCUser = "administrator@vsphere.local"
$LIvCPass = "vmware"
$SleepTime = 15
$LISpaceNeededGB = "5"

Write-Host "$(Get-Date): Selecting host for $LIName from $LICluster Cluster"
$LIVMHost = Get-Cluster $LICluster | Get-VMHost | Where {$_.PowerState -eq "PoweredOn" -and $_.ConnectionState -eq "Connected" } | Get-Random
Write-Host "$(Get-Date): $LIVMHost selected for $LIName"
Write-Host "$(Get-Date): Selecting Datastore for $LIName"
$LIDatastore = $LIVMHost | Get-Datastore | Where {$_.ExtensionData.Summary.MultipleHostAccess} | Where {$_.FreeSpaceGB -ge $LISpaceNeededGB} | Get-Random
if (!$LIDatastore) {
	Write-Host "$(Get-Date): No shared datastore found with $LISpaceNeededGB GB Free"
	Write-Host "$(Get-Date): LogInsight will not be installed"
} Else {
	Write-Host "$(Get-Date): $LIDatastore selected for $LIName"
	Write-Host "$(Get-Date): Importing $LIName from $LIInstallFile"
	$LIDeployedVMTask = $LIVMHost | Import-vApp -Name $LIName -Source $LIInstallFile -Datastore $LIDatastore -Force -RunAsync

	do {
		Sleep $SleepTime
		Write-Progress -Activity "Deploying Log Insight to $LIVMHost" -status "Progress" -PercentComplete $LIDeployedVMTask.PercentComplete 
		
	} until ($LIDeployedVMTask.PercentComplete -eq 100 )
	Write-Host "$(Get-Date): $LIName deployed and the task result was $($LIDeployedVMTask.State)"
	If ($LIDeployedVMTask.State -ne "success") {
		Write-Host "$(Get-Date): Unable to deploy LogInsight, deploy failed with $($LIDeployedVMTask.ExtensionData.Info.Error.LocalizedMessage)"
	} Else {
		$LIDeployedVM = Get-VM $LIName

		# Reconfigure the vApp with Name and IP details.
		$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
		$spec.changeVersion = $LIDeployedVM.ExtensionData.Config.ChangeVersion
		$spec.vAppConfig = New-Object VMware.Vim.VmConfigSpec
		$spec.vAppConfig.property = New-Object VMware.Vim.VAppPropertySpec[] (6)
		$spec.vAppConfig.ipAssignment = New-Object VMware.Vim.VAppIPAssignmentInfo
		$spec.vAppConfig.ipAssignment.ipAllocationPolicy = "fixedPolicy"

		$spec.vAppConfig.property[0] = New-Object VMware.Vim.VAppPropertySpec
		$spec.vAppConfig.property[0].operation = "edit"
		$spec.vAppConfig.property[0].info = New-Object VMware.Vim.VAppPropertyInfo
		$spec.vAppConfig.property[0].info.key = 0
		$spec.vAppConfig.property[0].info.value = $LIDGW

		$spec.vAppConfig.property[1] = New-Object VMware.Vim.VAppPropertySpec
		$spec.vAppConfig.property[1].operation = "edit"
		$spec.vAppConfig.property[1].info = New-Object VMware.Vim.VAppPropertyInfo
		$spec.vAppConfig.property[1].info.key = 1
		$spec.vAppConfig.property[1].info.value = $LIDNS

		$spec.vAppConfig.property[2] = New-Object VMware.Vim.VAppPropertySpec
		$spec.vAppConfig.property[2].operation = "edit"
		$spec.vAppConfig.property[2].info = New-Object VMware.Vim.VAppPropertyInfo
		$spec.vAppConfig.property[2].info.key = 2
		$spec.vAppConfig.property[2].info.value = $LIIP

		$spec.vAppConfig.property[3] = New-Object VMware.Vim.VAppPropertySpec
		$spec.vAppConfig.property[3].operation = "edit"
		$spec.vAppConfig.property[3].info = New-Object VMware.Vim.VAppPropertyInfo
		$spec.vAppConfig.property[3].info.key = 3
		$spec.vAppConfig.property[3].info.value = $LISNM

		$spec.vAppConfig.property[4] = New-Object VMware.Vim.VAppPropertySpec
		$spec.vAppConfig.property[4].operation = "edit"
		$spec.vAppConfig.property[4].info = New-Object VMware.Vim.VAppPropertyInfo
		$spec.vAppConfig.property[4].info.key = 4
		$spec.vAppConfig.property[4].info.value = $LIName


		$Reconfig = $LIDeployedVM.ExtensionData

		Write-Host "$(Get-Date): Reconfiguring $LIName after deployment"
		$Configtask = $Reconfig.ReconfigVM_Task($spec)

		Write-Host "$(Get-Date): Reconfiguring Network on $LIName to join $LINetwork"
		$NetworkChange = $LIDeployedVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $LINetwork -Confirm:$false

		Write-Host "$(Get-Date): Power On $LIName for first time"
		$LIDeployedVM | Start-VM | Out-Null

		Write-Host "$(Get-Date): Waiting for Log Insight Website to be active before configuration"
		$NumConnections = 0
		do {
			Sleep 7
			$Port = 443
			$Connection = New-Object Net.Sockets.TcpClient
			Try { 
				$Connection.Connect($LIIP,$Port)
				If ($Connection.Connected) {
					Write-Host "$(Get-Date): Waiting for Log Insight Website to be active before configuration"
					$NumConnections ++
				}
			}
			Catch {}
			Finally {}
			
		} until ($NumConnections -gt 10)

		$LIURI = "https://$LIIP"
		$ie = new-object -com "InternetExplorer.Application"
		$ie.navigate($LIURI)
		while($ie.ReadyState -ne 4) {start-sleep 1} 
		$ie.visible = $true
		$doc = $ie.Document
		If ($doc.nameProp -eq "Certificate Error: Navigation Blocked") {
			$doc.getElementByID("overridelink").Click()
			while($doc.nameProp -ne "vCenter Log Insight - Startup") {
				$doc = $ie.Document
				start-sleep 1
				Write-Host "$(Get-Date): Waiting for Log Insight Config Page"
			} 
		}
		$doc = $ie.Document
		$ie.Document.getElementById("skip-button").Click()
		Write-Host "$(Get-Date): Configuring Password"
		Start-Sleep 5
		$ie.Document.getElementsByName("user.email")| Foreach {$_.value=$LIEmail}
		$ie.Document.getElementsByName("newPassword")| Foreach {$_.value=$LIPassword}
		$ie.Document.getElementsByName("newPasswordConfirm")| Foreach {$_.value=$LIPassword}
		$ie.Document.getElementById("save-button").Click()
		Write-Host "$(Get-Date): Configuring License Key"
		Start-Sleep 5
		$ie.Document.getElementsByName("licenseKey")| Foreach {$_.value=$LILicense}
		$ie.Document.getElementById("skip-button").Click()
		Write-Host "$(Get-Date): Configuring Alert Email address"
		Start-Sleep 5
		$ie.Document.getElementsByName("alertsConfig.adminAlertReceivers")| Foreach {$_.value=$LIEmail}
		$ie.Document.getElementById("save-button").Click()	
		Write-Host "$(Get-Date): Configuring NTP"
		Start-Sleep 5
		$ie.Document.getElementsByName("ntpConfig.ntpServersCsv")| Foreach {$_.value=$LINTP}
		$ie.Document.getElementById("save-button").Click()
		Write-Host "$(Get-Date): Configuring SMTP"
		Start-Sleep 5
		$ie.Document.getElementsByName("smtpConfig.server")| Foreach {$_.value=$LISMTPServer}
		$ie.Document.getElementsByName("smtpConfig.port")| Foreach {$_.value=$LISMTPport}
		$ie.Document.getElementById("save-button").Click()
		Write-Host "$(Get-Date): Configuring vCenter Integration"
		Start-Sleep 5
		$ie.Document.getElementsByName("vsphereConfig.credentials[0].enabled")| Foreach {$_.Checked=$True}
		#TODO GO back and adjust for multi vCenter env
		$ie.Document.getElementsByName("vsphereConfig.credentials[0].hostname")| Foreach {$_.value=$LIvC}
		$ie.Document.getElementsByName("vsphereConfig.credentials[0].username")| Foreach {$_.value=$LIvCUser}
		$ie.Document.getElementsByName("vsphereConfig.credentials[0].password")| Foreach {$_.value=$LIvCPass}
		$testlinks = $ie.Document.get_links()
		$testlinks | Foreach { $_ | Where {$_.OuterText -eq "Test"} | Foreach { $_.Click() } }
		Start-Sleep 60
		$ie.Document.getElementById("save-button").Click()
		Start-Sleep 5
		$ie.Document.getElementById("save-button").Click()
		Start-Sleep 5
		$ie.Document.getElementById("skip-button").Click()
		$ie.Quit()
		Write-Host "$(Get-Date): $LIName deployment and configuration completed."
	}
}

Introduction to PowerCLI

Whenever I visit VMUGs or talk about PowerCLI I am constantly surprised by people who have not yet heard of PowerCLI or are unaware of the basics of how it works, let alone the power it has and time it can save people.  I wanted to address this and give people a head start learning PowerCLI.

The below video gives you an idea of what PowerCLI is, how it works and how you can get started. It’s the first of what will hopefully be a series of videos on PowerCLI concepts, please add a comment here for the kind of videos you would like to see.

Remotely Managing VMware vSphere through your mobile device

Remember the VMware Community PowerPack for PowerGUI? With it we were able to manage our VMware environment through a MMC style GUI placed on top of PowerCLI Scripts, this gave us the benefit of custom task based actions with a nice and easy to use interface.

Now Imagine if you could have the same great customized management options for your vSphere environment on a mobile device or tablet device.  Imagine no longer!

Mobile IT

Mobile IT is a new application which is available on a number of different mobile devices and tablets that allows us to import Mobile Packs which are custom written packs that work with your applications and also Power Packs which were previously written for PowerGUI.

So What do I need?

The Server

The first thing you will need to do is setup the server which is going to run Mobile IT, this is where we have two options.  In a corporate environment you will probably want all connections to go straight to your systems and you will want to manage the traffic and every aspect of the environment, in this case you would have the Mobile IT Server in your DMZ and the Mobile IT Agent on your network as per the image below taken from the Mobile IT documentation.

TinyGrab Screen Shot 12-01-2014 17.55.30

The second option (which was perfect for my home lab) is where Dell hosts the gateway server for you, you can then install the Mobile IT Server and Agent on your network (I had them both on the same server) as per the below diagram taken from the Mobile IT documentation.

TinyGrab Screen Shot 12-01-2014 17.55.08

The Install was very easy and I had the system up and running in around 20 minutes, once setup it was easy to import the VMware Community Power Pack and add the same machine as a new instance in the web admin interface.  I also installed PowerCLI on this server which is needed by the Community PowerPack.

TinyGrab Screen Shot 12-01-2014 18.58.46

The Client

Now all you need to do is install the app on your phone or tablet, a client is available in the app store of each of the following devices and is currently free. Once downloaded and you have entered your company name and credentials it will communicate with your Mobile IT server where you can authorize the device for use on your network.

Download

Check out the Mobile IT Site here for downloads and documentation.

See it in action

See it in action with the VMware Power Pack below and remember, this is all based on PowerCLI so you can customize the scripts and allow access to your tasks from any mobile device.

PowerCLI Group Discussion

VMworld PowerCLI Group Discussion–Part 3–Launching and Using

image

Following on from the previous posts in this series, this post will take us through the group discussion held at VMworld this year and expand on the items noted during the session.

This post will focus on what people in the session used PowerCLI for and also the various ways people used to launch the scripts.

Continue reading

PowerCLI Group Discussion

VMworld PowerCLI Group Discussion–Part 2–Resources

ResourcesFollowing Part 1 of these posts where we discussed getting started in PowerCLI I now give you Part 2 of the note taking by Nigel Boulton at VMworld in the PowerCLI Group Discussion.

In this part of the session we discussed what resources were available to people using PowerCLI.

As with the previous post I will expand on the notes taken and give you the complete list below.

If you have more resources make sure you add a comment and share them with others.

Continue reading

NorCal PowerShell User Group–San Francisco

PowerShell user groups are a great way for people to talk about PowerShell with like minded people and share thoughts, tips and tricks and generally discuss their usage..

If you are in the San Francisco area then now is your chance to get involved, in fact if you are anywhere in the world you can join, they will be transmitting it live over Google hangouts.

How can I RSVP and find out more?

The first meeting in San Francisco has been scheduled for Friday Nov 01 at 6:30 PM on the site below so please go there and signup today!

http://www.meetup.com/Northern-California-Powershell-User-Group/events/143718672/

North California PowerShell User Group

Im a big fan of user groups, I have attended many VMware User Groups and also PowerShell User Groups, these are a great way for people to talk about a particular technology and share thoughts, tips and tricks and generally discuss their usage of the particular product.

Recently I was contacted by Eric Courville who told me he was starting a PowerShell User Group for the NorCal area, he will be starting a group in an area that doesn’t currently have one and is looking for support from the community to attend, talk about your experiences, maybe learn some tips and tricks and general chat about PowerShell.  Both the new and the old PowerShell users are invited!

If you are in the Sacramento area and would like to join this event please see below for details:

How can I RSVP and find out more?

The initial meetup has been scheduled for Wed Oct 02 at 7:00 PM on the site below so please go there and signup today!

http://www.meetup.com/Northern-California-Powershell-User-Group/

That’s a bit far for me

Working with Eric we are also thinking of starting one in the bay area if we get enough Interest, if this is something you would attend in or around Palo Alto then please do leave a comment on this blog post to show your interest.

Updating from ESXi 5.1 to ESXi 5.1 Update1 with PowerCLI

Recently I was asked by someone how they could do rolling automated updates from ESXi 5.1 to ESX 5.1 Update 1 in their environment using PowerCLI, obviously there are a number of ways to achieve this and William Lam did a great job of showing these in this blog including a piece of sample PowerCLI code.

I figured I would share the code I used to perform this so that others may gain from this experience…

Continue reading

Disabling Netflow with PowerCLI

Today I was asked if there was a script to disable Netflow on a VDPortgroup, the below was a couple of quick and dirty scripts to first of all list all VDPortgroups and if they have Netflow enabled, the second was to disable Netflow for a VDPortgroup or a number of VDPortgroups.

This may help with a known issue on ESXi 5.1 when Netflow is enabled: http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2042370

Continue reading