Skip to content

Tag: PowerCLI

Finding a VM’s Port-ID with PowerCLI

A question was recently brought up over finding a VM’s Port-ID with PowerCLI.

Right off the bat, I assumed the ask was for the distributed switch port. That wasn’t the case though. In this situation, the ask was to identify the Port-ID that was being displayed in ESXTOP via the Network view.

Example:
ESXTOP Network View Example

The Port-ID that’s being displayed is local to the host. That information isn’t available through the VM object, at least as far as I could find. In order to get the Port-ID through PowerCLI, we have to get a little crafty. This is going to require some ESXCLI usage.

Since we’re dealing with networking, we can automatically jump to the ESXCLI Network documentation on VMware Code. Luckily, there’s really only two commands that deal with VMs and we’re going to need both of them:

  • network vm list
  • network vm port list

We’ll use the network vm list command to obtain the VM’s World-ID and then use that as an input to the network vm port list command to receive back the local Port-ID for the VM!

Since the goal was to do this in PowerCLI, I created a script and added it to the PowerCLI Community Repo. The script is titled: Get-VMNetworkPortId.ps1

It can be used in a couple different ways, so use it however makes sense to you.

Example Usage:
Get-VMNetworkPortId Usage

After feeding in a VM by pipeline, making use of the VM parameter, and even passing multiple VMs to the script, we can see how the Port-ID does in fact match up with what’s displayed in the ESXTOP example from above.

The script is available on GitHub and looks like the following:

So You’re Thinking About Learning PowerShell…

This year has been a wild ride for me. Earlier this year, I offered to do a couple community sessions based on automation with PowerShell. Unbelievably, every session was accepted and, even more so, every session was well attended. It really seemed like the attendees took something away from the session regardless of their experience level.

More recently, I was allowed the chance to present a session at VMworld about some fundamentals of getting started with PowerShell and PowerCLI. The session, which was co-presented with fellow PowerShell enthusiast Chris Wahl, ended up having three separate sessions that week and the rooms were full for all three sessions. The response was incredible. We even earned one of the Top Ten Sessions of the day that week!

I’m not the only one seeing this trend, there’s been a couple other community members also seeing the popularity of the topic. It’s exciting to have all these people new to PowerShell and see them have that “lightbulb moment” where all the possibilities click.

Getting Started

Getting back to the purpose of this post, why and how should you get started with PowerShell. I’ve collected a couple resources to help people do just that. One side note, you’ll notice I talk about PowerCLI a bit. Personally, I learned PowerShell in order to use PowerCLI. It was really the gateway into the much larger PowerShell ecosystem.

At the last UK Northwest VMUG meeting, David Hocking presented a community session on PowerShell for Beginners. Unfortunately, I couldn’t be there but he did post the deck on Twitter and I’ve embedded a link to below. It covers some of the reasons why he started with PowerShell, what his background was, and some great ways to get started. If you go to the last slide, there’s a link to download his example scripts and checkout some of the demo videos.

I mentioned the VMworld session earlier, it was posted to YouTube so you can access that directly. Chris and I will walk you through some of the lingo involved in getting started, then help get you started on writing code and some logic statements.

If you’re ready to get some hands-on time without impacting your own environment, there’s resources such as the VMware Hands-On Labs. It’s worth mentioning that there is an enrollment process, but it’s completely free. Couple lab examples:

  • HOL-SDC-1607 From Beginner to Advanced Features with PowerCLI – This lab will provide you with an environment and a lab guide to walk you through performing some tasks with PowerShell and PowerCLI.
  • HOL-1710-USE-4 – vSphere with Operations Management: Advanced Use Cases – This lab, again, provides you with an environment but the lab guide is targeted to be using the PowerCLI module with vRealize Operations Manager (vR Ops). As a beginner I wouldn’t advise following the lab guide, but instead using the lab to explore some of the cmdlets in an updated environment.
  • HOL-1721-SDC-6 – Introduction to VMware PowerCLI – If you happen to be at VMworld Europe, this is a huge recommendation. When this lab is posted to the public Hands-On Lab page, this will be my recommended go-to lab. The lab walks users through some of the basic configuration tasks a vSphere Administrator would do and how to automate those tasks. The lab captain, Vinith Menon, did a fantastic job on it.

Other Resources

Some other resources out there that have helped me:

  • Book: Learn Windows PowerShell in a Month of Lunches – Great resource that helps readers step through the fundamentals. Even after finishing it, you keep it as a reference. I can’t tell you how many times I’ve gone back to it as a refresher to something I forgot.
  • Book: Learning PowerCLI – Another great book, this one more focused on the vSphere Administrator. There’s a good overview of PowerShell in general and then it dives right into automating normal vSphere tasks.

If you’re a more visual learner, Pluralsight have a giant section of PowerShell and PowerCLI based videos. Here’s just a couple of them that helped me get started:

Lastly, and I can’t stress this enough, get involved in the community. Connect and network with people that you can bounce ideas off of and learn from. There’s a wide variety of ways to do so. User groups are a fantastic way to get started. There are PowerShell User Groups, VMware User Groups, and most any other technology based user group as well. If there isn’t one local to you, join a slack team. There’s a PowerShell Slack team, VMware Code Slack team, and so forth.

If you’ve found something else to be a great resource to help learn PowerShell, or PowerCLI, please let me know in the comments.

EUC Day 2016: PowerCLI for View

The Minneapolis VMUG had a terrific idea to put together a specialized event based around End User Computing (EUC) and I was lucky enough to be asked to come and present something around automation.

That brings me to the session I chose, which is an introduction to PowerCLI for View. Horizon View and automation (Powershell, specifically) aren’t something that go together all that well at this point in time. PowerCLI for View is a great utility, don’t think I’m taking anything away from it nor bashing it, but it does take some understanding to kinda set the groundwork before you dive-in head first. This session is my attempt to help set that groundwork.

Thanks again to Matt Heldstab and the rest of the MN VMUG team for having me out, the event was awesome. I also want to call out the rest of the sessions from that day, which can be found on YouTube: https://www.youtube.com/channel/UCMHCjDBAx2NhBlTRi2ud90A/videos?shelf_id=0&view=0&sort=dd

PoSh – vCenter License Handling

It’s that time of year again for vExperts… Time to replace the NFR licenses the vExpert program has graciously supplied us.

Was given a gentle reminder of this the other day while preparing for a vBrownbag presentation:
Expiring Licenses

I’ve been on a big Powershell module making binge lately and noticed there really isn’t much for handling vCenter’s licensing, so I created one and published it out on Github.

An overview of what’s currently included in the module:

Function Name Description
Get-VILicenses Gathers information on all VI licenses availabe in the vCenter Server
Get-VILicenses
Get-VILicenseInfo Gathers information on the supplied license key
Get-VILicenseInfo
Add-VILicense Adds the supplied license key to the vCenter Server license inventory
Add-VILicense
Remove-VILicense Removes the supplied license key from the vCenter Server license inventory
Remove-VILicense
Set-VILicense Sets the supplied license key to the desired VI Object
Set-VILicense
Get-VILicense Gathers information on the supplied license key from the VIObject
Get-VILicense

A general walk-through of what’s occurring within each function is Powershell interfacing with the License Manager object via VMware’s SDK.

Requirements:
PowerCLI modules and/or PSSnapins
Active connection to a vCenter Server

An automated way of downloading the files into a dedicated directory and importing the module into the current session:

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.

PowerCLI – Quick Stats Not Up To Date Error

Recently rebooted one of my vCenters and came across an error on my ESXi hosts stating “Quick stats on *vmhost* is not up-to-date”. A couple seconds worth of googling brought me to VMware KB2061008 which helped to resolve the issue.
Quick Stats Error Message

However, the KB only went through the GUI process of adding in the requisite parameters but there’s no fun in clicking through the GUI so I came up with a short script that’s applicable to vCenter 6.0 which can also accomplish performing the parameter creation/updates for the KB’s workaround.

Once you run the script, you’ll still need to restart the vCenter service on your VCSA or Windows server. If you happen to be in the web client:
Go to “Administration” then to the “Deployment” area to select “System Configuration”
Select “Services”, then select “VMware vCenter Server”
Select “Actions” followed by “Restart”

Once the service is back up and running the error will no longer be present.
vCenter Service Restart

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.

PowerCLI 6.0 R2 – Announcing vROps Integration!

For those that have been requesting some vCOps and/or vROps integration into PowerCLI, as I have been, we finally have our solution! PowerCLI 6.0 R2 brings that to the table in the form of a new module: VMware.VimAutomation.vROps

The following functions are featured in the module:
Connect-OMServer
Disconnect-OMServer
Get-OMAlert
Get-OMAlertDefinition
Get-OMAlertSubType
Get-OMAlertType
Get-OMRecommendation
Get-OMResource
Get-OMStat
Get-OMStatKey
Set-OMAlert

Here’s a couple tips to get started using it:
To begin pulling stats you will need to authenticate to the vROps instance via Connect-OMServer:
Connect-OMServer

After getting connected, and being connected to an associated vCenter, check out the resources via Get-OMResource by piping it a VMHost, VM, or so forth:
get-omresource

As you can see, I pulled the resources for a VMHost. Some of the basic, higher level, information is in there such as Health status and value, whether or not vROps is receiving any data about it, and how far back data has been collecting for the resource.

The following is pulling the same information for the vROps VM in my lab environment:
get-omresource

If you want to go straight to the stats, I don’t blame you one bit. However just running the Get-OMStat command is going to fill your PoSh window with every single stat available for that resource at all the time points available. Overwhelming would be putting it lightly, so you’ll want to also familiarize yourself with Get-OMStatKey. This function allows you to see what all stats you can actually pull:
get-omstatkey

Couple things I feel the need to point out:

  • Include both the AdapterKind and ResourceKind objects when calling this function, it returns every possible stat key possible without it… All 17,646 of them.
  • Even when including the AdapterKind and ResourceKind, I would highly recommend filtering out what what you’re looking for. The HostSystem ResourceKind has 2225 stat keys alone.

Finally, lets select a stat key and pull some detailed stats by way of the Get-OMStat function. In this scenario, I’m pulling CPU Ready Summation values:
Get-OMStat

Some of the other functions included surround alerting.

The Get-OMAlert function pulls all of the vROps alerts:
get-omalert

Since there happens to be some alerts, let’s check out two abilities of the Set-OMAlert function. The first option being cancelling of an alert:
set-omalert -Cancel

If you notice in the example, the alert still exists but it’s status is changed to “Inactive”.

Next up, let’s check out the suspend function of the Set-OMAlert function:
Set-OMAlert -SuspendMinutes

Overall, the new module definitely gets my stamp of approval! Thanks to the Automation and PowerCLI team for listening to the users and getting this integrated!

Additional information is available at the following link: https://blogs.vmware.com/PowerCLI/2015/09/powercli-6-0-release-2-is-now-generally-available.html

PowerCLI – View – Pool Health Check Report

I’ve been given the chance to work with View a little more here recently and focus in on some of the gaps that are missing, specifically around reporting and alerting. vC Ops and the View adapter are in use, however the alerts leave much to be desired. I’m glad to be able to say that the gaps are filled in with the new version, and new title, of vR Ops and the View adapter but this environment is not yet to that point.

Examples of the vR Ops alerts which are included by default:

vR Ops Default View Alerts

What gaps am I referring to? I’m aiming specifically at the View Pool state, provisioning state and the amount of available desktops compared to the headroom configuration.

To do this, I enlisted the help of the View PowerCLI cmdlets and the View Connection server ADAM database to create the script below that creates an output similar to this:
(Note: the asterics around the value that caused the report to be sent)

View Pool Report Output

Before getting to the script, lets cover some of the requirements of the script:

  • This script is to be run from a Connection server.
  • This script is currently formatted to have the report be consumed by way of email.
  • The email variables need to be filled in to match the environment it’s being run in.
  • The email is sent only if the following criteria are met:
    • A Pool’s state is false.
    • A Pool’s provisioning state is false.
    • A Pool’s available desktops are less than the headroom setting by the amount set in the desktopthreshold variable.
  • The default desktop threshold is meant to be a percentage and is set to a default of 90%, or 0.9. This is easily modified on line 22.

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.

PowerCLI – Automate the vROps OVA Deployment

For those that haven’t heard, vCenter Operations (known better as vC Ops) has been not only updated but renamed. It’s new name is vRealize Operations (or, probably, vR Ops) and it is officially version 6.0. Previously the system was deployed in a vApp containing a UI VM and an Analytics VM. The new systems incorporates a roles for the VMs such as Master, Master Replica, Data and Remote Collecter.

However, this blog post isn’t to focus on what’s been updated. It’s to take a look at making the deployment a little easier since most installs will have at least two nodes. If you would like some further information on the update, please check out VMware’s website: http://www.vmware.com/products/vrealize-operations

After running the script below, you’ll find an output similar to the following:

vROps-Deploy

Before getting to the script, lets cover some of the assumptions and requirements made in the script:

  • The OVA has already been downloaded and is placed somewhere available to the system running the script. (Example: local disk, SMB or DFS share, etc.)
  • The VM is being placed in a resource pool.
  • The local system is able to successfully run and resolve the VM name via Resolve-DnsName cmdlet which is available in PowerShell v4.
  • The subnet being used is a /24 or 255.255.255.0. This is easily modified on line 126.
  • The VLAN ID is based on the third octet of the system’s IP address. This information is set on line 128 and the portgroup is set on line 133.
  • The gateway ends with ‘.254’. This is easily modified on line 129.
  • DNS should be modified to match your environment which is based on line 130.

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.

PowerCLI – Process of Adding Notes to VMs

Documentation is a good thing. No matter how big or small. One of the easiest methods of documentation is to use the built in annotation section for “Notes”.

Annotation Notes

What I’ve come up with is a fairly basic script to go through all the VM objects that do not contain notes and allow the user to insert notes on a line by line basis. Here’s an example:

Running the script

Here’s the script to accomplish it:

If a one-liner is more your speed, give this one a go… Note: you’ll have to specify your own cluster, if that’s a desired use.

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.

View – Using Powershell to Check ADAM Replication Status

Recently ran into some issues where replication on the View ADAM database wasn’t going too well. Instead of checking the replication status by RDPing into each View Connection and running the repadmin command, I created a Powershell script to do it for me. This script also helped with verification due to being able to report all the Connection servers’ status within a single window.

Only item to note: you will have to prepopulate the script with the desired Connection servers

I also converted the above into a little more automated script that’s usable only from one of the Connection servers, thanks to how the View API ties into PowerCLI…

Items to note:

  • This has to be run through the View PowerCLI module, which only works on Connection servers.
  • It’s designed to be run as a one liner.
  • It will report back the replication status on all the Connection servers involved in that particular View environment.

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.