Mass Reconfigure of Virtual Machine vCPU Sockets/Cores using VMware PowerCLI

powercli

Increasing the amount of vCPUs in ESXi is a simple admin task when working with a single or low number of virtual machines. However, performing this on a large number of virtual machines can be an intensive task.

This article is about my use case and how I resolved this with a script, which can be found at the bottom.

Continue reading

Exporting and Importing vSphere Distributed Switch Permissions using PowerCLI

powercli.png

At my current assignment, I got busy with the replacement of an existing vCenter Server and migration of all linked objects. This included migrating the vSphere Distributed Switch Port Group objects, settings and permissions.

Exporting the vSphere Distributed Switch configuration is an easy-to-do job using the GUI. However permissions are not included. This post is dedicated to the script I wrote to export and import these permissions.

Continue reading

VMworld 2013: PowerCLI Best Practices – A Deep Dive #VSVC4944

My first scheduled session was with Alan Renouf and Luc Dekens, the PowerCLI gurus of VMware and always busy writing scripts and code to make the life of a VMware professional easier.

DSC03006

Alan Renouf and Luc Dekens slicing the PowerCLI cake.

During this session, Alan and Luc were sharing their best practices when using PowerCLI 5.5 Release 1 and PowerShell v3. I basically wrote down a list of these best practices and specifically the ones that come in handy during my activities. Hopefully it will come to good use for others.

  • Use tags (introduced in vSphere 5.1). Tags can be read and written, which was not possible in earlier releases of PowerCLI
  • vDS support. Now you can configure distributed switches
  • Open your VM console using PowerCLI and share the URL with administrators to enable them to directly login to the VM console without logging into vSphere Web Client.
  • vCloud Director 5.5 support
  • Configure ESXi host licenses
  • Make sure when processing data, that you filter the search before filtering the results to speed up the processing of your command or script.
  • Don’t use Write-Host, instead use Write-Verbose or Write-Warning
  • Use venter Alert Actions to automate in a very easy way. Let’s say after deploying a VM, you want to automatically run a script in the VM that installs AntiVirus software, or your CMDB gets updated automatically. There are numerous amount of alerts available in vCenter which you can use out-of-the-box
  • WebCommander, a VMware fling which presents PowerCLI scripts, for any kind of product (View, vCloud Director, vSphere etcetera) in a web-based view available to you, the scripter/programmer and also your users. It’s even possible to give users a direct link to your scripts. Looks like an App store for PowerCLI scripts 🙂
  • Check out PS-Remoting to re-use PowerCLI sessions open to your vCenter Server. This decreases load on your vCenter server and increases the speed of your scripts, since the initial load only happens once, when you open up the session.
WebCommander, a fling by VMware

WebCommander, a fling by VMware

Thanks to Alan and Luc for their session and dose of humour 🙂

Get-vSocket PowerCLI script

I just created a PowerCLI script for exporting all your VMs with vCPU and vSocket count to a CSV file.
You can download the script using the link below.
Script download

#General information
#Date: June 6th, 2013
#Author: Rene Bos
#URL: http://blog.stormdesigns.nl
#Script version: 0.1

#Script summary
#Tested on vCenter Server and ESXi 5.1
#This script will retreive the vCPUs and vSockets for each VM and will export this information in a table-like format to a CSV file.
#You need VMware PowerCLI installed on your machine and an active connection to one or more vCenter servers.

#Disclaimer
#Please use this script at your own risk and test it out in your testlab first before using it in production
#When using my script, please leave the general information in place. And let me know if my scripts needs improvement!

#Customizations
$CsvPath = "C:TempGet-vSocket.csv"

#Variables
$VMs = Get-VM

foreach ($VM in $VMs)
{
Write-Host "Getting information for $VM.name"
$VMview = Get-VM $VM | Get-View

$a = [PSCustomObject]@{
Hostname = $VM.name
NumCPU = $VMview.Config.Hardware.NumCPU
NumCoresPerSocket = $VMview.Config.Hardware.NumCoresPerSocket
}
$a | Export-CSV -Path $CsvPath -Append
}
Write-Host "CSV file is ready"

Get-IQN script for getting all IQNs of managed ESXi hosts

Because of an error in the past, I wanted to make sure that all IQNs were stored in our CMDB and available at times of disaster.

I created a script for this, which will get the IQNs for all ESXi hosts under your vCenter server and export the information to a CSV file. It can be downloaded from here or you can use the text below and save it as *.ps1 file.

–Update on 9th of May, 2014: Keith (bouldergeek) supplied code that works with PowerCLI 5.5, check it out in the comments. Thanks for your feedback Keith!

#General information
#Date: May 24th, 2013
#Author: Rene Bos
#URL: https://snowvm.com
#Script version: 0.1

#Script summary
#Tested on vCenter Server and ESXi 5.1
#This script will retreive the IQNs of all ESXi hosts under your vCenter Server and will export this information in a table-like format to a CSV file.
#You need VMware PowerCLI installed on your machine and an active connection to one or more vCenter servers.

#Disclaimer
#Please use this script at your own risk and test it out in your testlab first before using it in production
#When using my script, please leave the general information in place. And let me know if my scripts needs improvement!

#Customizations
$CsvPath = "C:TempIQN-Export.csv"

#Variables
$ESXiHosts = Get-VMHost | Sort-Object

foreach ($ESXiHost in $ESXiHosts)
{
$h = Get-VMhost $ESXiHost.Name
Write-Host "Getting IQN for $h"
$hostview = Get-View $h.id
$storage = Get-View $hostview.ConfigManager.StorageSystem

$a = [PSCustomObject]@{
Hostname = $h.Name
IQN = $storage.StorageDeviceInfo.HostBusAdapter.iScsiName
}
$a | Export-CSV -Path $CsvPath -Append
}
Write-Host "CSV exported to $CsvPath"

Exchange 2013 Management Shell from Windows 8

I’m testing out the new functionalities of Exchange 2013 in my testlab and get familiar with the product as we are probably going to use this in production.

While testing, I was wondering if it would be possible to manage Exchange 2013 remotely from my Windows 8 client.
Ofcourse you can use the ECP (Exchange Control Panel) but managing your environment with Powershell is something ‘more compliant’ with the management ways Microsoft sees it (And it’s cooler).

Installing the Exchange Management Shell on Windows 8 is not going to work (unless you are in the same AD domain as the Exchange server, correct me if I’m wrong).

So here I am, wanting to remotely manage Exchange 2013 and having a Windows 8 client in a workgroup.

Back in the office, my Exchange-guru colleague sees me stumble and mumble and quietly sent me a mail message with a script included. He created it on the fly and was hoping my cranky face would turn into a happy face =)
He succeeded! And that only by using 11 lines of code.. What a boss!

With all credits going to my colleague Jens Giessler, I am posting his created script on my blog, hoping other people’s faces will turn into happy ones.
Replace the bold parts with your own credentials and Exchange server FQDN, and save as a .ps1 file to run it with PowerShell.

Before running the script, you also need to enable Basic Authentication on the PowerShell virtual directory, using the ECP (Servers menu, virtual directories tab).

Oh; as with all my previous and future script postings; use them at your own risk.

#Functions
Function Query-Credentials
{
$Global:Cred = Get-Credential -Credential <b>domainuser</b>
}

Function Connect-Exchange
{
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://exchangeserverfqdn/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
}

#Establish connection
Query-Credentials
Connect-Exchange

Configuring SNMP on multiple ESXi hosts

While talking with my colleague from the networking department, it seemed nice to have our ESXi NICs in Cacti for performance/load logging and troubleshooting.
As apposed to me complaining about a slow network, he can now complain about NIC utilization at my desk =)

Configuring the necessary SNMP parameters for each ESXi host can be a time consuming task.
The PowerCLI command used for this (Set-VMHostSnmp) does not support connecting to vCenter and configuring multiple hosts at one time.

Because of this I created a script which configures these parameters on multiple ESXi hosts, including separate user accounts, passwords and SNMP community strings. I made use of a foreach loop and each host configured in the config.txt file will run through the same set of commands:

Connect to ESXi host
Configure SNMP (Enable and configure SNMP string)
Disconnect ESXi host

For this, the script consists of two files: the script itself and a config.txt file where you can input multiple hosts in CSV format.

Use the script at your own risk! I have used it in my test environment and will try the script tomorrow in our acceptation and production environment.

The script files can be downloaded from here.

Let me know if you have any improvements or want to share your experience with the script!

#General information
#Date: April 24nd, 2013
#Author: Rene Bos
#URL: http://blog.stormdesigns.nl
#Script version: 0.1

#Script summary
#This script is created for configuring SNMP on multiple ESXi 5.1 hosts using PowerCLI so your monitoring tool can request SNMP information.
#The environment I had to configure this in, used different root passwords for each ESXi host and required a different approach which resulted in this script.
#Use the supplied config.txt file to enter ESXi hostnames, passwords and SNMP community strings before running the script.
#Each line in this file represents one ESXi host.

#Disclaimer
#Please use this script at your own risk and test it out in your testlab first before using it in production
#When using my script, please leave the general information in place. And let me know if my scripts needs improvement!

$caption = "This script assumes you have yet filled in the required fiels in the 'config.txt' file."
$message = "Do you want to run this script now?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","This will run the script now based on the data in the config.txt file"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Do not run this script now"
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
$answer = $host.ui.PromptForChoice($caption,$message,$choices,0)

switch ($answer)
{
    0 {"The script will execute NOW"; }
    1 {"The script will NOT execute"; Exit}
}

$ESXiHosts = Import-Csv "config.txt"
foreach ($ESXiHost in $ESXiHosts)
{
Connect-VIServer $ESXiHost.ESXi_Host -User $ESXiHost.Username -Password $ESXiHost.Password
Get-VMHostSnmp | Set-VMHostSnmp -Enabled:$True -ReadOnlyCommunity $ESXiHost.Snmp_String
Disconnect-VIServer -Server * -Force -Confirm:$False
}