Monday, February 15, 2016

Powershell and PowerCLI - getting started

Here's a small collection of links and tips for getting started with PowerShell/PowerCLI

PowerCLI main link
https://www.vmware.com/support/developer/PowerCLI/

PowerCLI Change Log (great place to see the latest PowerCLI version and release notes)

https://www.vmware.com/support/developer/PowerCLI/changelog.html 

Note each release note includes a link to the software needed to run PowerCLI. It's called the compatibility matrix. 

https://www.vmware.com/support/developer/PowerCLI/doc/powercli60r3-compat-matrix.html

In my case for 6.0r3 you want to 
  • run on 2008R2 sp1, 2012R2, 7 sp1, 8.1
  • have PowerShell 3 or 4
  • Have .Net framework 4.5 or newer

The mentioned windows OSs bring a version of Powershell by default. Find out the version of powershell running on your system with this command:

$psversiontable [enter]

For example, on my Windows 7 SP1 VM, Powershell is at version 2, so it would need to be upgraded first (with a corresponding .net upgrade). Windows 8.1 has version 3 and you would be good to go.


(Server 2016 and Windows 10 have PowerShell version 5. Yes, releases have gone in quick succession, mostly because of DSC. You can read about changes here. Keep in mind it's not officially supported in PowerCLI yet, but it will be, inevitably.)



Chris Wahl has an excellent series of posts on PowerShell here. I especially found this post useful to start setting up your environment:

http://wahlnetwork.com/2015/08/03/powershell-integrated-scripting-environment-ise/


This 15 minute vBrownBag tech talk is about that post and also about version control with Git.


https://www.youtube.com/watch?v=Pgt54U21LPo


I found this post helpful as well, even if from 2012


http://www.virten.net/2012/09/getting-started-with-powercli-5-1-and-powershell-3-0/



This is my version of the startup script Chris guides you through setting up. I place it in different locations whether it's work (so it only affects my user) or home machine. 

profile.ps1 contents:

Set-ExecutionPolicy Bypass
Set-Location "E:\PowerCLI codes"
if ($psISE)
{
Clear-Host
Write-Host 'Oh it is YOU again'
}

I totally took the advice from Chris. This cracks me up since I don't use PowerCLI that often! Mad shout out to him since he was very helpful to me throughout the creation of this post :D


All nice and good in PowerShell, but by default, running the PowerCLI icon on the desktop will not obey the Set-Location, and it will complain because Set-ExecutionPolicy requires running as Administrator.

To fix this:

1) Right click the PowerCLI icon, Click Advanced, set to Run as Administrator.

2) Look at the target on the shortcut. PowerCLI runs its own initialization script, in my case:

C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1

This file is set to read-only. Make a backup, fix the permissions until you can save. In my case, for it to respect the path I want, I went to line 269 of the 6.0r3 script which has a "cd \" command. I commented it out and now my desired location in profile.ps1 works. 

If you are going to use the 32-bit version of PowerCLI, you would also have to check that shortcut and do the respectful edits. I have not yet found the need to use that link though.


I'm thinking there's probably a way to have all these little customization tasks scripted so in theory you could just open a command line with administrator privileges, paste your instructions (which would include things such as create file, insert file contents, save, change permissions and file attributes, modify shortcuts, replace string) and not have to make the process of setting up the environment so manual. The ideal scenario is after pasting the instructions, you would close that cmd, double click the PowerCLI icon and you are ready to go.

I'm only thinking this is possible because PowerShell is meant to be able to do anything you can normally do on Windows GUI. I'm sure as releases of PowerCLI change things you would need to adjust the script a bit, but if you use several jumpboxes this could save you a lot of time.

PowerShell / PowerCLI feels very powerful. Also have to say having the ISE installed by default is pretty cool. Get in on this, it's looking like it will be quite fun!

No comments:

Post a Comment