Home > Blogs > VMware PowerCLI Blog > Category Archives: vCenter

Category Archives: vCenter

VDS Export/Import with PowerCLI

One of the great new features introduced in vSphere 5.1 was the ability to export and import the configuration of your vSphere Distributed Switch (VDS) and port groups to a file.

This gives you a quick restore method in case of issues or misconfigurations and also allows you to copy the entire VDS or port group configuration to a new VDS.  This feature is detailed by this VMware KB and is available via the vSphere Web Client, below you can see how we would do this via the web client:

image

Exporting the configuration with PowerCLI

With the introduction of the VDS cmdlets in PowerCLI 5.1 R2 we can also automate this process using the Export-VDSwitch and

Continue reading

PowerCLI 5.1 Release 2 Now Available

PowerCLI 5.1 Release 2 has now been released and can be downloaded here.  As always you will find some great new features, bug fixes and enhancements which make PowerCLI even better than before… I know its hard to believe but its true.

VDS Cmdlets

PowerCLI SnapinsWhenever I would ask the question at VMUG’s or VMWorld, “What would you like to see next from PowerCLI” I would always get the same answer “Support for Virtual Distributed Switches!”.

Previously there has been a fling available and also some work created by community member “Luc Dekens” to use VDS, this was a great start but in some cases was not a sustainable solution as it was either not supported or more complicated to maintain or use.

I am now pleased to tell you that PowerCLI has a new snapin, this snapin is called VMware.VimAutomation.VDS and is for managing Virtual Distributed Switches.

Introduced are 14 new cmdlets which will allow you to perform actions against your VDS configuration, we can use the Get-VdsCommand cmdlet to list the new cmdlets.

An overview of these are below:

Cmdlet Name Cmdlet description
Add-VDSwitchPhysicalNetworkAdapter This cmdlet adds host physical network adapters to a distributed switch.
Add-VDSwitchVMHost This cmdlet adds hosts to the specified distributed switch.
Export-VDPortGroup This cmdlet exports the configuration of a specified distributed port group to a specified .zip file.
Export-VDSwitch This cmdlet exports the configuration of a specified vSphere distributed switch to a .zip file.
Get-VDPortgroup This cmdlet retrieves distributed port groups.
Get-VDSwitch This cmdlet retrieves vSphere distributed switches.
New-VDPortgroup This cmdlet creates distributed port groups.
New-VDSwitch This cmdlet creates vSphere distributed switches.
Remove-VDPortGroup This cmdlet removes distributed port groups.
Remove-VDSwitch This cmdlet removes vSphere distributed switches.
Remove-VDSwitchPhysicalNetworkAdapter This cmdlet removes host physical network adapters from the distributed switches they are connected to.
Remove-VDSwitchVMHost This cmdlet removes hosts from the specified distributed switches.
Set-VDPortgroup This cmdlet modifies the configuration of distributed port groups.
Set-VDSwitch This cmdlet modifies the configuration of vSphere distributed switches.

Remember each of these cmdlets has help built in, to access the help including examples of how you might use these cmdlets use the following:

Get-Help cmdletname –full

PowerShell v3 Support

As well as the new cmdlets for working with Distributed switches and all the other great enhancements in PowerCLI 5.1 Release 2 we made sure we listened to our customers and with the release of PowerShell version 3 it seams that a lot of people are already making use or planning to install PowerShell v3.

You will be happy to know that PowerCLI now supports PowerShell v3, this not only gives you support from VMware but also we are now able to take advantage of some of the great enhancements which PowerShell v3 brings.

Some PowerShell v3 examples…

Simplified Syntax… Making PowerShell and therefore PowerCLI easier to read and quicker to type we can use simplified syntax to give us even more English like statements, before we would have needed to use the {} brackets to compare a property, now we are able to type it the same way we would think it…

SNAGHTML26e15701_thumb3

The above simple example shows all virtual machines which are powered on.

vCloud Director 5.1 Support

With PowerCLI 5.1 R2 we have also added support for vCloud Director 5.1, you can now automate the latest version of vCloud director using both PowerCLI and PowerCLI for tenants in a vCloud Director 5.1 environment.

image_thumb2

PowerCLI 5.1 and the future

Recently I have had some questions from people using PowerCLI, these are mainly related to confusion around the 5.1 release of VMware products and the way that PowerCLI works so I wanted to use this blog post to answer some of these questions but also to confirm that the future for PowerCLI is bright.

Q.  “Does PowerCLI work with the vCenter Server Appliance?”image

A. Yes it does, PowerCLI uses the vCenter APIs, in fact the best place to install PowerCLI is the same place that you would use the vSphere Client from, this is normally the vSphere admins workstation or a windows machine used solely for scheduled tasks.  PowerCLI connects to the vCenter APIs and therefore both Windows and the appliance versions of vCenter work with PowerCLI 5.1.

Q. “Does PowerCLI work with the vSphere Web Client?”

A.  A common question and yet in reality you do not need the C# vSphere Client or the vSphere Web Client in order for PowerCLI to work, everything you need is inside the installer for PowerCLI, just install it (instructions here) and your set.

Q. “With VMware releasing more and more Linux related appliances what does this mean for PowerCLI?”

A. PowerCLI is one of VMware’s most successful and well loved automation and troubleshooting tools by vSphere and vCloud Director Admins, as I said in the answer to a previous question, it connects directly to the vSphere APIs, it doesn’t care what is hosting them.

Whilst I can not discuss the roadmap for PowerCLI I can certainly say its as strong as ever and we will continue to wow the users of PowerCLI with the latest features and updates.

Q. “Does Onyx work with vSphere 5.1?”image

A. For those of you who are not aware of what Onyx is, it’s a VMware Fling which sits in-between your vCenter and your vCenter Client, it intercepts the traffic and translates it into PowerShell code, not nicely formatted PowerCLI type cmdlets but still a great place to look if you need to find a way to do something quickly.

You hit play on the Onyx client and the fun begins, anything you do in the vSphere client from then on is captured and represented as code, and not just PowerShell code but also C#, vCO JavaScript and  Raw SOAP messages.

So does it work with 5.1 ?  Yes of course it does, but only when using the C# client, obviously the vSphere Web Client works differently and therefore Onyx is unable to capture the actions you take in the web client and turn them into code, as this is a fling I can not make any promises of if or when this will be updated to work with the web client but obviously the challenges are different.  In the meantime make sure you use the C# client when you want to capture the code.


Hopefully this will help eliminate any questions about PowerCLI and how it works with 5.1 and the future, if you have any further questions please add a comment to this post and we will do our best to answer them.

PowerCLI 5.1 now available

PowerCLI 5.1 is now generally available, as a reminder, in the previous post we gave you an update of what was new.  You can view that post here.

You can view the official PowerCLI release documentation here or the documentation for the Tenant snapin here.

From now on you will see two download links for PowerCLI, one for the updated version of PowerCLI and another for the Cloud Tenants, make sure you choose the correct one as they will not install together.

As you can see from the download page, the updated PowerCLI for vSphere and vCD Administrators is on the left hand side and the PowerCLI for Tenants of vCloud Director is on the right.

Stay tuned for more posts showing exactly what is possible with these new releases soon.Download PowerCLI

Download

Use the following link to be directed to the download page:  http://communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli?view=overview

PowerCLI 5.1 – What’s New ?

With the announcement of vSphere 5.1 from Steve Herrod at VMworld 2012 – San Francisco I wanted to highlight the new features which will be available as part of VMware vSphere PowerCLI 5.1 Release 1.

Background

image

As with previous releases there have been many enhancements to PowerCLI, as users of PowerCLI will know, as part of the default PowerCLI 5.0.1 install we already had 5 PowerShell Snapin’s available to use, these enabled you to manage the following products and features:

  • vCenter and vSphere (VMware.VimAutomation.Core Snapin)
  • vCenter and vSphere Licensing (VMware.VimAutomation.License Snapin)
  • Image Builder (VMware.ImageBuilder Snapin)
  • Auto Deploy (VMware.DeployAutomation Snapin)
  • vCloud Director (VMware.Vimautomation.Cloud Snapin)

As you will also know, most of the cmdlets for the Cloud snapin were “Get-“ cmdlets, these allowed us to focus on retrieving data from vCloud Director, access to more advanced functions were available via the Get-CIView cmdlet but this was for the advanced PowerCLI users.

What’s New for vSphere Users ?

The PowerCLI Core Snapin introduces a number of improvements and new features, you will of course find many bug fixes and speed enhancements as part of this release, as well as this the following enhancements have been included:

  • You can use Kerberos for pass-through authentication with vCenter Server, ESX/ESXi, and vCenter Virtual Appliance systems.
  • You can create linked clones with New-VM.
  • You can pass datastore clusters to the Datastoreparameters.
  • You can retrieve vSphere objects from vCloud Director objects with the RelatedObjectparameter.
  • You can manage resources more efficiently with Storage DRS (SDRS) support added to a number of cmdlets.
  • You can retrieve, create, modify, and remove VMHost, VM, and SDRS advanced settings with the the Get-AdvancedSetting, New-AdvancedSetting, Set-AdvancedSetting, and Remove-AdvancedSetting cmdlets.

vSphere PowerCLI 5.1 Release 1 also brings a set of improvements that enhance security and customization, these include:

  • You can set the scope of your settings with the Scope parameter of Set-PowerCLIConfiguration.
  • You can initialize custom vSphere PowerCLI scripts automatically by storing them in the Initialize-PowerCLIEnvironment_Custom.ps1 script configuration file.

What’s New for vCloud Admins ?

vCloud Director Admins can take advantage of over 60 cmdlets to manage vCloud Director, these have been enhanced from the “Get-“ cmdlets and now include cmdlets to help you modify and automate your vCloud Directory infrastructure, the following enhancements have been made:

  • You can create, modify, manage, and remove organizations.
  • You can create and manage permissions.
  • You can assign computing and networking resources.
  • You can create, modify, and remove organization networks.
  • You can create, modify, and remove vApp networks.
  • You can create, modify, manage, and remove vApps.
  • You can manage virtual machines and their guest operating systems within vApps.

What’s New for vCloud Tenants ?

With PowerCLI 5.1 Release 1 we now have a new snapin specifically for vCloud Tenants, in fact this is a completely separate installation with its own set of cmdlets which can not be installed on the same machine as vSphere PowerCLI 5.1 Release 1, this is called VMware vSphere PowerCLI Release 1 for Tenants and is designed to aid the vCloud Tenants in performing automated tasks in their hosted vCloud organization.

Why would we do this ?

We wanted to bring the power of automation and the ease of reporting that PowerCLI gives the current vSphere and vCloud Admins to the tenants of the cloud.  All this whilst giving them an easy to use simplified subset of cmdlets specific to the tasks they can do within their vCloud Organization.

What’s available to tenants ?

VMware vSphere PowerCLI 5.1 Release 1 for Tenants consists of two components:

  • vSphere PowerCLI common snapin for providing common PowerCLI configuration and usage based tasks, these cmdlets are part of the VMware.VimAutomation.Commonsnapin.
  • vCloud Director PowerCLI snapin for providing tenant-based reporting and automation, these cmdlets are part of the VMware.VimAutomation.Cloud snapin and can be used with vCloud Director 1.5.1 Tenant tasks.

These components will allow the vCloud Tenants to perform the following tasks:

  • You can list, modify, and manage organizations.
  • You can list, create, and manage permissions.
  • You can list and assign networking resources.
  • You can list organization networks.
  • You can list, create, modify, and remove vApp networks.
  • You can list, create, modify, manage, and remove vApps.
  • You can list and manage virtual machines and their guest operating systems within vApps.

Summary

So where are we now ? What can we actually do with PowerCLI and the different VMware installations of snapins ?

image

As you can see, we have a number of different snapins which enable us to add the products and areas we are interested in using with Windows PowerShell, we have the ability to not only enable our admin users but also our private and public cloud users, with the VMware snapins all users of the VMware products can achieve the level of automation, integration and reporting that PowerShell and PowerCLI gives.

More Information

Keep tuned for more blog posts and information or if you are visiting VMworld Europe in 2012 make sure you add  INF-VSP1252 – What’s New with vSphere 5.1 – ESXCLI & PowerCLI to your session list.

Updating your hosts file

AlanFeb2012_thumb_thumb1_thumb_thumb[2]
Posted by
Alan Renouf
Technical Marketing

How many times have you built a test lab, perhaps you are trying out nested virtualization, which is very cool by the way!

Whilst doing this myself I normally come across an issue where my DNS is not 100% correct, normally I end up editing my hosts file to add entries which define my vSphere hosts just so I can open a console…

Is this a familiar sight ?

TinyGrab Screen Shot 27-06-2012 14.51.12

Whilst doing this recently I had to add a large number of entries to my hosts file so that I could open the console on some of the VMs, as always I found an easy way than manually adding each host reference one by one.  PowerShell.

With the following script I was easily able to pull each host name and IP address and update the hosts file automatically:

The Script

Connect-ViServer MyvCenter

$hostsfilelocation = $env:SystemRoot + "\System32\Drivers\etc\hosts"
$hostsfile = Get-Content $hostsfilelocation
Foreach ($line in (Get-VMHost | Get-VMHostNetworkAdapter -Name vmk0 | Foreach { Write "$($_.IP)`t$($_.VMhost)"})){
    If ($Hostsfile -notcontains $line) {
        $hostsfile = $Hostsfile + $line
        Write-Host "Adding… $line"
    }
}
$hostsfile | Set-Content $hostsfilelocation –Force

In Action

TinyGrab Screen Shot 27-06-2012 15.00.03

Ultimately I ended up with a hosts file which included an entry for each of my hosts and a working console screen !

TinyGrab Screen Shot 27-06-2012 15.01.54

TinyGrab Screen Shot 27-06-2012 15.01.07

Note:  If you have UAC enabled on windows you will need to right click the PowerCLI Icon and choose “Run as Administrator” so it has the permission to edit the hosts file.

I hope this helps anyone who is manually still adding each entry into their hosts file.

Get notification of new blog postings and more by following VMware PowerCLI on Twitter: @PowerCLI

List and Disconnect vCenter Sessions

One of the first cmdlets people learn when starting out in PowerCLI is Connect-VIServer, this as you probably know allows you to connect to a vCenter server or a ESX/ESXi Host directly, this creates a session on the vCenter server until it has been disconnected using the Disconnect-VIServer cmdlet.

But who uses the Disconnect-VIServer cmdlet ? Come on truthfully, do you use it at the end of each script or PowerCLI session you use ?

This is obviously best practice but in my travels and from the code examples I have seen on the internet people seam to miss this out, on the face of it this is not an issue, it has “no impact” that you can see, but wait……

If you are in vCenter and go to the Sessions screen you will see that our sessions which were opened are now in an idle state:

image_thumb4

So why do we care ?

Did you know there is actually a limit on the amount of sessions which can connect to vCenter, this is set at 500 concurrent session, both Idle and active sessions count.

You would be surprised how quickly this can fill up in a corporate environment, especially if you have multiple scripts which run as scheduled tasks and do not disconnect correctly.

This was the case at a recent customer site, they needed a quick way out of this situation to enable them the time to go back and adjust the multiple scripts they had connecting and not disconnecting and the multiple staff they had who would RDP to a server and leave the vCenter client running and connected.

The following two PowerShell Advanced Functions were written to help with this situation:

List all sessions with PowerCLI

Function Get-ViSession {
    <#
        .SYNOPSIS
            Lists vCenter Sessions.

        .DESCRIPTION
            Lists all connected vCenter Sessions.

        .EXAMPLE
            PS C:\> Get-VISession

        .EXAMPLE
            PS C:\> Get-VISession | Where { $_.IdleMinutes -gt 5 }
    #>
    $SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager
    $AllSessions = @()
    $SessionMgr.SessionList | Foreach {   
        $Session = New-Object -TypeName PSObject -Property @{
            Key = $_.Key
            UserName = $_.UserName
            FullName = $_.FullName
            LoginTime = ($_.LoginTime).ToLocalTime()
            LastActiveTime = ($_.LastActiveTime).ToLocalTime()
           
        }
            If ($_.Key -eq $SessionMgr.CurrentSession.Key) {
                $Session | Add-Member -MemberType NoteProperty -Name Status -Value "Current Session"
            } Else {
                $Session | Add-Member -MemberType NoteProperty -Name Status -Value "Idle"
            }
            $Session | Add-Member -MemberType NoteProperty -Name IdleMinutes -Value ([Math]::Round(((Get-Date) – ($_.LastActiveTime).ToLocalTime()).TotalMinutes))
    $AllSessions += $Session
    }
    $AllSessions
}

 

When this function is run it will give you a list of the connected sessions and various useful properties:

SNAGHTML366d1b1_thumb2

This can obviously be filtered using the standard PowerShell cmdlets to list any sessions which have been idle for over 30 minutes:

SNAGHTML367dc9a_thumb2

A further function can be used to take the input from Get-VISession and disconnect these sessions where needed:

Disconnect sessions with PowerCLI

Function Disconnect-ViSession {
    <#
        .SYNOPSIS
            Disconnects a connected vCenter Session.

        .DESCRIPTION
            Disconnects a open connected vCenter Session.

        .PARAMETER  SessionList
            A session or a list of sessions to disconnect.

        .EXAMPLE
            PS C:\> Get-VISession | Where { $_.IdleMinutes -gt 5 } | Disconnect-ViSession

        .EXAMPLE
            PS C:\> Get-VISession | Where { $_.Username -eq "User19" } | Disconnect-ViSession
    #>
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline=$true)]
        $SessionList
    )
    Process {
        $SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager
        $SessionList | Foreach {
            Write "Disconnecting Session for $($_.Username) which has been active since $($_.LoginTime)"
            $SessionMgr.TerminateSession($_.Key)
        }
    }
}

The following example shows how to use these functions to disconnect any sessions with over 30 minutes of idle time:

SNAGHTML36c8019_thumb2

Both of these functions show that even if there is not a built in cmdlet for a certain feature in PowerCLI there is always an easy way to create your own cmdlet like functions to work the way you need them.

Alan Renouf

Technical Marketing

PowerCLI at VMworld 2011 (Las Vegas) UPDATE

VSP2296 What everybody ought to know about reporting vSphere and vCloud environment with PowerCLI session has a repetition!

If you haven’t managed to subscribe for it in the first time slot due to the high interest level from the audience, now you have the opportunity to attend the second one on Tuesday 4:30 PM.

All PowerCLI Presentations

InventorySnapshot – Automatic PowerCLI code

ISFadeAt VMware, our engineers work on lots of pet projects in their spare time, and are always looking to get feedback on their projects, recently we started a new area of the VMware Labs site to allow them to share these with you.  We call these flings !

Why flings?

A fling is a short-term thing, not a serious relationship but a fun one. Likewise, the tools that are offered in the flings area are intended to be played with and explored. None of them are guaranteed to become part of any future product offering and there is no support for them. They are, however, totally free for you to download and play around with them!

InventorySnapshot

Balaji Parimi

Works in the Ecosystem Engineering group.

Ravi Soundararajan

Works in the Performance group

A recent fling which has just been released by the above two engineers is called InventorySnapshot, this fling allows a user to "snapshot" a given vCenter inventory configuration and then reproduce it. The "inventory" includes the Datacenter folders, datacenters, clusters, resource pools, vApps, hierarchy, roles and permissions, configuration settings, and custom fields. In other words, if you have an inventory with a given set of hosts and VMs organized into a group of clusters, we can faithfully reproduce this environment, including the cluster settings and custom roles you may have defined.

As a simple example, suppose you have an inventory with one datacenter (DC A), one cluster (Cluster A), and two hosts (Host A and Host B). At a high level, the fling emits a PowerCLI script that, when executed, does the following:

  1. Creates Datacenter “DC A.”
  2. Creates cluster “Cluster A.”
  3. Adds host “Host A” to “Cluster A.”
  4. Adds host “Host B” to “Cluster A.”

Notice that this can be helpful for a variety of reasons. For example, suppose you’ve spent a lot of time creating a development vCenter environment, and now you wish to deploy it in production. Using this fling, you can snapshot your “dev” environment and then run it against the “production” vCenter server, saving you the task of laboriously adding each host, creating the proper clusters and resource pools, etc.

Watch the following video to learn more about the power of this fling or head over to the InventorySnapshot page for more information.

Get your Free PowerCLI Fan Pack (Update Offer has expired)

Folks, we are sorry to say offer is now over. We will look into expanding this offer in the future but as of today we are no longer shipping the PowerCLI Fan Packs.

Thanks to everyone who registered for the Free PowerCLI Fan Pack. We will be shipping your PowerCLI Fan Packs within the next 1 – 3 weeks.

What is in a PowerCLI Fan Pack ?

  • 1 – Full size vSphere PowerCLI 4.1.1 Poster
  • 1 – PowerCLI Button
  • 1 – PowerCLI Bumper Sticker

Powercli-fan-pack

 

Pablo Roesch

vSphere Product Marketing – Script Automation