Category Archives: PowerShell

Automated deployment of vCloud Networking and Security 5.5

A while ago I posted a PowerCLI method for deploying vShield 5.0 which can be found here, as mentioned in the comments of that post this did not work for vCNS 5.5, this is because the process of deploying it changed and actually became a lot neater than what it was before.  Before we had to reach inside the guest OS and mess around with configuration files.  In the 5.5 release the vCNS team made some changes to make this easier for people to automatically deploy,

 

I actually wrote this a while ago and forgot about it until recently when I was asked if it could be done, this was a great reminder for me to post the code for others to use.

 

You will see in the code that we need to specify some variables at the start of the script, you will see in the code that you need to specify a cluster to deploy vCNS to, as part of this script it automatically picks a random host and a shared datastore which has enough room to deploy the appliance to, this is just an example and can be changed to your requirements as needed – I was just having fun with a kind of automated placement Winking smile

You will need PowerCLI 5.8 R1 for this and don’t forget, once vShield has been deployed you can easily take the configuration further by using the vShield module I created here.   For other examples of deployment scripts for VMware appliances check out the repo here and contribute!

Example output

image

Code

Automating Tags and Tag Category creation and assignment with PowerCLI

Fimageor a couple of releases now PowerCLI has been able to work with vSphere Tags, A tag is a label that you can apply to objects in the vSphere inventory.

After creating a tag, you can assign that tag to a category. Categories allow you to group related tags together. When you define a category, you can also specify the type of objects to which its tags can be applied to and whether more than one tag in the category can be applied to an object.

For example, if you want to tag your virtual machines by the owner, you can create a category called “Owner” and specify that it applies to virtual machines only and that only a single tag can be applied to a virtual machine at any time. The tags in this category could be Alan, John or Fred etc.

I have had a few people ask me how they can use PowerCLI to work with external systems, CMDBs, databases or even just a CSV file.

One example of this is where a company could have various information about hosts or datastores or virtual machines, like the project that purchased these, a cost code or an owner.  This data is generally stored somewhere else but it would be great to see this information straight in the vSphere Web Client where you manage the objects so that you can instantly contact the owner or work out which project the object is being used for etc.

The below video shows how we can use PowerCLI and this generic script I created to import the data, create the tags and tag categories and assign them to the machines, it uses a csv as input but this could obviously be changed to anything which can be read in PowerShell, like a API, database, application etc etc.

Automating tags and tag categories video

Example Script

This script is the script I created as an example which relates each of the items in the Name column to an object in the inventory then for each of the other column headers it will create a category and then the tags that are under the categories, once this has been done it will apply the tags to the objects in the Name column.

Connect-viserver myvc.corp.local -user administrator@vsphere.local -pass Pa$$w0rd
$CMDBInfo = Import-CSV C:\Software\cmdbinfo.csv

# Get the header names to use as tag category names
$TagCatNames = $cmdbinfo | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Select -Expand Name

# Create the Tag Category if it doesnt exist
Foreach ($Name in ($TagCatNames | Where {$_ -ne "Name"})) {
    if (-Not (Get-TagCategory $Name -ea SilentlyContinue)) {
        Write-Host "Creating Tag Category $Name"
        New-TagCategory -Name $Name -Description "$Name from CMDB" | Out-Null
        
        # Create Tags under the Tag Categories
        $UniqueTags = $cmdbinfo | Select -expand $name | Get-Unique
        Foreach ($Tag in $UniqueTags) {
            if (-Not (Get-Tag $Tag -ea SilentlyContinue)) {
                Write-Host "..Creating Tag under $Name of $Tag"
                New-Tag -Name $Tag -Category $name -Description "$Tag from CMDB" | Out-Null
            }
            # Assign the Tags to the VMs/Hosts
            $cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {
                Write-Host ".... Assigning $Tag in Category of $Name to $($_.Name)"
                $TagAssignment = Get-Tag -Category $Name -name $Tag
                New-TagAssignment -entity $($_.Name) -Tag $Tagassignment | Out-Null
            }
        }          
    }
}

PowerShell User Group–Campbell–CA–Nov 5th

Are you interested in PowerShell? Do you want free Pizza? If the answer to either of these questions is YES then join us!

Are you in the Campbell, California area on 5th Nov 2014?

I will be presenting about PowerCLI and covering the following areas:

  • Introduction to managing VMware products with PowerShell
  • Reporting
  • Deployment
  • Automation
  • Integration
  • Community
  • Where are VMware headed?
  • Question time!

If you are not interested in managing VMware products with PowerShell then attend anyway, we will no doubt reference lots of PowerShell general tips and tricks and definitely have some great conversations about PowerShell in general.

When and Where?

Wednesday, November 5, 2014

6:00 PM to 9:00 PM

 

Groupware Corporate Office

550 Division Street, Campbell, CA (map)

RSVP

Make sure you sign up here.

PowerCLI in the vSphere Web Client–Announcing PowerActions

You don’t know how excited I am to write this!  Around a year ago I presented something we were working on internal as a tech preview for my VMworld session, the response was phenomenal, if you were there you would remember people standing up and clapping and asking when this awesomeness would be available, its taken a while but its here and its worth the wait.  So what is this that I am so excited about?

 

PowerActions is a new fling from VMware which can be downloaded here, it adds the automation power of PowerCLI into the web client for you to use your scripts and automation power back inside the client, have you ever wanted to right click an object in the web client and run a custom automation action, maybe return the results and then work with them further all from the convenience of the web client…. Now you can!

This works in 2 ways….

Console

PowerShell console in the Web Client

Firstly you can access a PowerCLI console straight in the web interface, even in safari, this fling allows for a dedicated host to be used as a PowerShell host and this machine will be responsible for running the PowerCLI actions, once its setup you will access the console from within the web client and all commands will run remotely on the PowerShell host, it even uses your current logged on credentials to launch the scripts meaning you don’t have to connect the PowerCLI session.

 

You can use tab completion on your cmdlets and even use other PowerShell snapins and modules to control any PowerShell enabled infrastructure to extend your automation needs within the vSphere Web Client.

 

MenuRight Click your objects

Secondly you can now right click an object in the Web Client and create a dedicated script which will work against this object, have the ability to extend your web client and take the object as an input to use inside your script.

This comes with 2 options, Create a script and also execute a script.

 

My Scripts and Shared Scripts

Not only can you create your own scripts to run against objects in the web client but advanced admins can create scripts and share them with all users of the web client by storing them in the Shared Scripts section of this fling, read the documentation to find out more about how to do this.  This gives the great ability to have not only shared scripts but actually a golden set of scripts which all users of the web client can use while you keep your items in a separate area “My Scripts”, enabling each user to have their own custom actions.

 

Download and read more

Download the fling from the VMware Labs site here, also make sure you grab the document from the same site and also check out the great post on the PowerCLI Blog for more information here.

 

Check out the video for a quick introduction

To help with the details I shot a quick install and usage video that covers the basics, make sure you read the PDF that comes with the fling and make sure you are active, if you like this then let is know, if you want more then let us know…. basically give us feedback!

Deploying Log Insight with PowerCLI 5.8 R1 new features

PowerCLI 5.8 R1 was recently released and of the great new features included was the ability to now set the OVF/OVA properties of a VM or vAPP stored in an Open Virtualization format.

The new cmdlet allows us to query the file for the properties and interactively find out which entries we need to fill out, we can then fill these entries out and send them as part of the deployment of the VM when using the Import-vApp cmdlet which has been a part of PowerCLI for a while now.  For more information on how we do this check out the recent post on the PowerCLI blog here.

The bit I like about this cmdlet is that we can now dynamically work out which host, network and storage to put the VM on based on our own metrics, like say for instance I wanted to choose the host with the most amount of memory in a cluster or a datastore on that host that had the most room left:

$VMHost = Get-Cluster "Cluster Site A" | Get-VMHost | Sort MemoryGB | Select -first 1
$Datastore = $VMHost | Get-datastore | Sort FreeSpaceGB -Descending | Select -first 1

Another method to see the entries and what needs filling out as part of the OVF/OVA Deployment is to use the cmdlet to store the information inside a variable as shown in the video I created below, this can be used to browse through the properties and fill them out as needed or create a script for later deployment.  Another option for this is to load the settings into a variable and then use one of the advanced PowerShell editors like PowerGUI Script Editor to click through the variable in the variable pane and explore the options and their values as can be seen below:

image

The code

I want to automate the entire build out of my virtual infrastructure like this, I hear you say…. well why not help out, get involved, I started a new Github project where we can all contribute to make sure we are not reinventing the wheel, why not check out the github project here and add your own deployment scripts, so far there are scripts to deploy both Log Insight 2.0 (the one shown in the video) and also vShield Manager 5.5.x.

 

Video: Deploying Log Insight 2.0

WebCommander goes Open Source

You may have heard me talk about Webcommander before at VMworld or during a vBrownbag, if you haven’t and you don’t know about this project then you have been missing out!

WebCommander was designed as a framework to wrap your PowerShell and/or PowerCLI scripts into an easy-to-access web service, give each script a nice easy web form and show the results, give a simple icon on a web page to your end users and allow them to use your scripts and all they need is a web browser!

Have you ever wanted to give your users access to certain virtual infrastructure tasks instead of the entire vCenter Client? Have you ever written tasks in PowerShell which you wished you could easily hand off to the users to run on an ad-hoc basis? Now you can hand off the tasks your users need by simply adding a new script and giving them access to WebCommander.

Previously WebCommander was initially released as a VMware fling and updates were performed by VMware, the community was keen to jump on this and the feedback came in thick and fast, I was amazed to see how some customers were using WebCommander and how they took it to the next level.  The main engineer on the project (Jerry Liu) implemented the feedback fast and released update after update to bring the additions to the latest version.

The final part of this story and the great part is that Jerry has now released WebCommander to the community as an open source project on Github, this is fantastic news as it means more people can now get involved, add new contributions and make changes which can be checked back into the core code for everyone to use.  PowerCLI and PowerShell users alike.

So why wait, head over to the site here, download and contribute to this awesome project!

 

WebCommander Eye candy

image

PowerShell your world cup

A little Friday night fun here, of course the first thing i thought when i saw the below tweet from Will.I.Lam was, Ooooo that wouldnt take long to use in PowerShell.

image

And of course just 20 mins after finishing work I had a few functions, here is how you use them.

1. Go to the kimono website and sign up for an account, this will get you an APIkey, Click your name once you have logged in and select account.

2. Copy the API key and paste it below in the script and have fun!

The Functions

$global:APIkey = "YOUR-KEY-GOES-HERE-FROM-STEP-2"

Function Get-WorldCupPlayer ($Country) {
    If ($Country) {
        $TeamID = (Get-WorldCupTeam -Country $Country).id
        Invoke-WebRequest -Uri ("http://worldcup.kimonolabs.com/api/players?teamId=$($TeamID)&apikey=$($ApiKey)") | ConvertFrom-Json
    } Else {
        Invoke-WebRequest -Uri "http://worldcup.kimonolabs.com/api/players?apikey=$($ApiKey)" | ConvertFrom-Json
    }
}

Function Get-WorldCupTeam ($Country, $TeamID) {
    If ($Country) {
        Invoke-WebRequest -Uri ("http://worldcup.kimonolabs.com/api/teams?name=$($Country)&apikey=$($ApiKey)") | ConvertFrom-Json
    }
    If ($TeamID) {
        Invoke-WebRequest -Uri ("http://worldcup.kimonolabs.com/api/teams?id=$($TeamID)&apikey=$($ApiKey)") | ConvertFrom-Json
    }

    If (!$country -and !$TeamID) {
        Invoke-WebRequest -Uri "http://worldcup.kimonolabs.com/api/teams?apikey=$($ApiKey)" | ConvertFrom-Json
    }
}

Function Get-WorldCupStat {
    Invoke-WebRequest -Uri ("http://worldcup.kimonolabs.com/api/teams?sort=goalsFor,-1&apikey=$($ApiKey)") | ConvertFrom-Json
}

Bring on the World Cup Fun

#Display the best team in the World Cup

Get-WorldCupTeam -Country “England”

image

Get-WorldCupStat | Format-Table

image

Get-WorldCupPlayer | Format-Table

image

Get-WorldCupPlayer -Country “England” | Format-Table

image

#Who has scored the most goals in the Netherland team so far?

Get-WorldCupPlayer -Country “Netherlands” | Foreach { $_ | Where { $_.goals -gt 0 } | Select firstName, lastName, goals } | sort goals –Descending

image

#View the Logo of the team who is going to win
$logo = (New-Object System.Net.WebClient).DownloadFile(((Get-WorldCupTeam -Country “England”).logo), ($env:TEMP + “\Logo.png”))
Invoke-Item $env:TEMP\Logo.png

image

PowerShell User Group–San Francisco–1st April 2014

Are you interested in PowerShell?

Are you in the San Francisco area on 1st April 2014?

If the answer to both of these questions is “Hell yeah” then make your way to the “Northern California PowerShell User Group” where we will be talking general PowerShell type conversations.

I will be presenting about PowerCLI, where it fits in, what people have done with it and more about the latest release.

If you are not interested in managing VMware products with PowerShell then attend anyway, we will no doubt reference lots of PowerShell general tips and tricks and definitely have some great conversations about PowerShell in general.

The Details

Date: Tue Apr 01
Time: 6:30 PM

Location: Microsoft Office – 835 Market Street, Suite 700, San Francisco, CA (map)

More details can be found here

See you there!

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.