Virtually Sober

If there is free booze and Virtualization; I'm there!

Tag Archives: PowerCLI

Creating a vSphere Change Management DB for free – Step 4 of 4

For my final blog post in the 4-part series on how to create a vSphere Change Management Database for free, I’m going to show you how to prune the data. If you missed the first 3 posts check them out here:

Step 1 – Creating the SQL Database
Step 2 – Inserting data into the SQL Database
Step 3 – Querying the SQL Database

Depending on the size of your vSphere environment or your data retention policies you probably don’t want to log vCenter settings indefinitely. Because we designed the database to always contain a RecordID in each table along with a LastUpdated datetime, we can use this to prune the data.

Read more of this post

Creating a vSphere Change Management DB for free – Step 3 of 4

In my previous 2 blog posts I took you through creating a vSphere Change Management Database, how to insert data using PowerCLI, and in this post, I’ll show you some example queries you can run once you have the data. If you missed the first 2 posts check them out here:

Step 1 – Creating the SQL Database
Step 2 – Inserting data into the SQL Database

There are probably hundreds of different things you can derive from the data captured. So here is just a sample of what I’m going to show you:

  • Show all configurations of a VM across all data logged
  • Taking oldest date available and showing all VM configs on that datetime
  • Taking the most recent data available and showing total VM size provisioned and used (useful for Rubrik sizing)
  • Using the oldest and most recent VM used size to calculate the total VM used storage growth % (also useful for Rubrik sizing!)
  • Show all VMDisk and VMNIC configurations for a VM
  • Show all configurations for a VMDK or VMNIC
  • Show all configurations for a Port Group on a host
  • Show all configurations of a Cluster and Host
  • Taking the oldest date available and showing all original Cluster and Host configs
  • Show all current and historical stats for a Datacenter (including cluster, host and VM counts etc)

Read more of this post

Creating a vSphere Change Management DB for free – Step 2 of 4

In the last blog we created a SQL database in which to store data from PowerCLI in order to create a vSphere Change Management Database for free. If you missed it, check it out here:

Step 1 – Creating the SQL Database

In this second post we are now going to insert data into the database and configure it to run on a schedule. To do this we are first going to use a PowerCLI cmdlet with variables to store the vSphere data, then we will use ForEach to insert each row of data into a corresponding SQL table row.

Read more of this post

Creating a vSphere Change Management DB for free – Step 1 of 4

In my last blog post I gave an introduction to Working with SQL Databases using PowerShell. What I’d now like to do is share an example of a cool use case for combining PowerShell, SQL and PowerCLI.

After drinking a few beers (this blog is called virtually sober for a reason!) I started to think about different problems that I could solve by logging information into a SQL database. What info could I pull and store that would be useful to a virtualization admin? Then the answer hit me. Every setting in the vCenter from VMs to port groups, clusters, hosts to datastores! Why? Because the vCenter database only stores the latest setting.

Read more of this post

Automatically Updating To PowerCLI 6.5.1

I’m blogging live from the Boston Summer VMUG Usercon 2017 where I’m sharing my top 10 PowerShell tips and tricks. A big topic at the show, for anyone interested in PowerShell and PowerCLI, is the change with PowerCLI 6.5.1 in how you install, update and load it. This is pretty important because all your scripts are going to need updating! Otherwise, they will potentially stop working if you are about to or have already upgraded to vSphere 6.5.x. There’s no reason that I’m aware of that you can’t upgrade in advance and still interact with vSphere 6.0 etc. So now is as good as time as any to start planning your upgrade.

Before PowerCLI 6.5.1 you would’ve used this at the start of each script:

Add-PSSnapin VMware.VimAutomation.Core

If you attempt this command on a computer running PowerCLI 6.5.1 it will result in a lovely “The Windows PowerShell snap-in ‘VMware.VimAutomation.Core’ is not installed on this computer.” error:

 

From now on you need to use:

Import-Module VMware.PowerCLI

But wait, you can’t just start using this new command otherwise you will get another error “The specified module ‘VMware.PowerCLI’ was not loaded because no valid module file was found in any module directory.”:

PSSnapInError

You’ll first need to uninstall your current version of PowerCLI from Programs and Features, download the PowerCLI module from the PSGallery then save a copy for future offline use (if needed). While this is certainly much easier than before, it still sounds awfully manual to me! To save you time and effort why not use PowerShell to automate this process? Here is a simple script I wrote to do just that:

#######################
# Configure the variable below
#######################
# Specify a directory to download and install the new PowerCLI module to for future offline access
$PSModulePath = "c:\PowerCLIModule\"
#####################################################################
# Nothing to change below this line, commented throughout to explain
#####################################################################
#######################
# Testing if PS Module path exists, if not creating it
#######################
$PSModulePathTest = test-path $PSModulePath
if ($PSModulePathTest -eq $False)
{
New-Item -ItemType Directory -Force -Path $PSModulePath
}
#######################
# Checking to see if PowerCLI is installed in Program Files, takes 5-30 seconds
#######################
write-host "Checking if PowerCLI is installed in Program Files, wait 5-30 seconds"
$PowerCLIInstalled = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "VMware vSphere PowerCLI"}
#######################
# If PowerCLI is installed then removing it, so we can run from the module instead
#######################
if ($PowerCLIInstalled -ne $null)
{
write-host "PowerCLI Installed - Uninstalling to allow for new PowerCLI module"
# Uninstalling PS module
$PowerCLIUninstall = $PowerCLIInstalled.Uninstall()
# Checking return value for success
$PowerCLIUninstallValue = $PowerCLIUninstall.ReturnValue
if ($PowerCLIUninstallValue -ne 0)
{
write-host "Uninstall Of PowerCLI Failed - Most likely due to not running as administrator"
}
# Finished uninstall
}
#######################
# Checking to see if the PowerCLI module is already installed
#######################
$PowerCLIModuleCheck = Get-Module -ListAvailable VMware.PowerCLI
#######################
# If PowerCLI module is not installed, nothing found, then running install...
#######################
if ($PowerCLIModuleCheck -eq $null)
{
write-host "PowerCLI Module Not Found - Installing"
# Not installed, finding module online
Find-Module -Name VMware.PowerCLI
# Trusting PS Gallery to remove prompt on install
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
# Installing module
Install-Module -Name VMware.PowerCLI –Scope AllUsers -Confirm:$false
# If running this is a repeat demo/test, you can uninstall the module using the below:
# Uninstall-Module -Name VMware.PowerCLI -Confirm:$false
# Saving to folder
Save-Module -Name VMware.PowerCLI -Path $PSModulePath
}
#######################
# Testing import of PowerCLI module
#######################
Import-Module VMware.PowerCLI
Try
{
$PowerCLICommandTest = Get-VICommand
$PowerCLIImportTest = $True
}
Catch
{
$PowerCLIImportTest = $False
}
#######################
# Outputting result
#######################
if ($PowerCLIImportTest -eq $True)
{
write-host "New PowerCLI Module Successfully Installed"
}
else
{
write-host "Something went wrong! Maybe you, maybe me. Does this computer have internet access and did you run as administrator?"
}
#######################
# End of script
#######################

Make sure your computer has internet access and you are running the script as administrator, otherwise you can’t download the latest version, uninstall the old or import the module for all users. You can download the full example from here:

AutoUpdatingToPowerCLI6.5.1.zip

Going forward you can now update your PowerCLI version with minimal fuss using:

Update-Module -Name VMware.PowerCLI

If you found this useful please like and share. Happy scripting,

Joshua

Automated vSphere VM sizing & cost calculator for DR to Microsoft Azure

When planning DR or migrations from VMware vSphere to Microsoft Azure, one very important task is to configure the appropriate VM/instance sizing to match your current VM settings. This needs to be done in advance for the following key reasons:

  • Ensure adequate performance of the VM when running in Azure
  • Select the VM size that supports the number of vNICS and vDisks required
  • Perform a cost analysis of VM runtime and storage costs

In ZVR 5.0 selecting the Azure VM size is very easy in the GUI:

automated-vsphere-vm-sizing-ss1

But what this doesn’t do is tell you exactly which one to pick and neither does it tell you how much it is going to cost!

So how are you going to select the correct instance size and calculate the cost for all your VMware VMs? That sounds like a manual and tedious task that thousands of people are going to have to tackle over the next few years. Of course, you already know I have something that is going to help you otherwise you wouldn’t be reading this far!

Read more of this post