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: http://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