admin
Immortal
Immortal

How To: Configure freshly booted ESX with PowerShell

Jump to solution

As part of a larger project I've been working on a set of scripts which configure a freshly-installed ESX (or ESXi) system into Virtual Center (after first attempting to remove previous VC state and clean state from the host). Currently they follow a typical post-boot configuration pattern:

  1. Set ESX password

  2. Add to Virtual Center cluster

  3. Configure Virtual Switches

  4. Configure Port Groups

  5. Add NFS partitions

  6. Add iSCSI partitions

  7. Configure NTP

  8. Configure VMotion

Before trying, modify esx-master.ps1 and esx-profile.ps1 to local conditions (or your password will be set to "CHANGEME").

Command: esx-autoconfigure.ps1 "hostname|ip-address"

As this is my initial foray into PowerShell, please excuse any idiosyncracies.

This is a work-in-progress - feedback welcome. Points awarded for the best suggestions in the next few days.

lance

(2008-Sep-02 - I've moved the script files to )

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Some suggestions:

- configure or check DNS and gateways before steps 3/4/7/8. Critical for clustering, HA and DRS!

- configure the firewall

- (eventually) configure some advanced ESX settings. See for example a recent discussion about NFS.MaxVolumes

- check (and eventually configure) licensing before using clustering with HA/DRS/vMotion

- configure (or adjust) resource pool(s). Adding an ESX server will increase the available resources.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
29 Replies
halr9000
Commander
Commander

My comments are as follows:

1. http://www.vmware.com/landing_pages/toolkit_contest.html

2. Cool, Smiley Happy I've been working on something similar for a project at work (not to mention for my book).

3. I'm a judge for #1, so I'll refrain from commenting directly on your code in case you decide to submit it, which I suggest you do.

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
LucD
Leadership
Leadership

Some suggestions:

- configure or check DNS and gateways before steps 3/4/7/8. Critical for clustering, HA and DRS!

- configure the firewall

- (eventually) configure some advanced ESX settings. See for example a recent discussion about NFS.MaxVolumes

- check (and eventually configure) licensing before using clustering with HA/DRS/vMotion

- configure (or adjust) resource pool(s). Adding an ESX server will increase the available resources.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
alanrenouf
VMware Employee
VMware Employee

Very nice, you should definately enter the comp.

Alan Renouf

VMware, Citrix, Microsoft Consultant

UK

My Blog

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
olan025
Enthusiast
Enthusiast

iberc,

I'm doing a large roll-out of esxi. I have been working on the exact same as what you're doing. If I can add anything across what you got i may post it.

You're scripts look to be more portable. This is my first dive into significant scripting not to mention powershell. The envronment i'm building for is very homogenous so the work reflects it. this is definately work in progress. these files on their own need to be edited etc and linked.

I've been looking for awhile now for configuration of ESXi via powershell and it seems very limited. Most resources are directed at management of existing.

Message was edited by: olan025

0 Kudos
admin
Immortal
Immortal

Lance works for VMware, no $5000 for him, for us 3rd prize is we're fired Smiley Sad

0 Kudos
admin
Immortal
Immortal

My cover has been blown. - l

0 Kudos
depping
Leadership
Leadership

Looks great!

Lance, do you know Aiden Dalgleish? He's a PSO Consultant from the UK and already created a hell of a lot scripts for installing. Talk to him, he's got some cool stuff!

Duncan

My virtualisation blog:

If you find this information useful, please award points for "correct" or "helpful".

0 Kudos
olan025
Enthusiast
Enthusiast

Hal,

How about making an exception to allow me a sneak peak at your new book? My scripting needs to be tuned up as you can tell i'm a beginner if you looked at my earlier attachments.

I'm making changes and updates to those, if anyone does care for them post it. otherwise my stuff is pretty basic compared to Lance's functionalized code.

0 Kudos
olan025
Enthusiast
Enthusiast

Updated and named to be different than the other here.

  • recent - I have a newer version that takes hosts from a CSV file. This eliminates the need to create a hostname.ps1 for each server. when you get over 90 servers those files are unmanageable, and any changes made to the template means a rebuild of them.... So I now use a CSV.

I'm thinking of rearchitecting it again... if there's any interest i'll look at posting it. I have to de customize if for the job i'm doing here to post it which is some extra work....

Unpublished updates.

- fixed NTP auto start and DNS Search Suffix- Thanks LucD

- added blat email option to email log or report

- added csv function for hosts to replace having individual host-template.ps1 files for each host.

- fixed a few minor issues

0 Kudos
LeeTheMC
Enthusiast
Enthusiast

I am able to configure the NTP Servers (as many of you have done in your scripts), but how do I check the "NTP Client Enabled" checkbox? I presume that this is required to tell the ESX server to use the NTP servers configured. Thanks in advance.

Lee

0 Kudos
LucD
Leadership
Leadership

You could use the Set-VMHostService cmdlet.

Set-VMHostService -HostService (Get-VMHostservice -VMHost (Get-VMHost esx1.test.local) | Where-Object {$_.key -eq "ntpd"}) -policy "automatic"

A bit of a cryptic command I admit, but it seems to work.

For -policy you can use "on", "off" or "automatic"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

admin
Immortal
Immortal

My ESXi hosts run NTP when I configure the server addresses without having to explicitly enable the service, but LucD's command should work if necessary.

0 Kudos
SCampbell1
Enthusiast
Enthusiast

Has anyone succeeded in configuring the license server and edition on an ESX server using Powershell?

I am about to embark on using the Java sample in the SDK to try to do it. I have also looked at the C++ code posted in the community.

Thanks

Update2: Sep 23. This is extracted from code that I believe is working properly, even though are likely more efficient ways. Some of these are used for other aspects of my provisioning script so there may be more than you need. Note in particular the use of the $ESXHostView.MoRef. -- Start of rant -- It cost an hour of head scratching and random guesses and finally get-member to find that. Sure would be nice to have some documentation of the VMware.VIM .Net Classes. (Hint. Hint.) Note that cmdlet help and that SDK website don't count for newbie PS/VIM people who need to access the .Net libraries. The .Net libraries help is a good start with its 5 classes. I think we need to see the same thing for all the "real" classes too. Of course, maybe it's all there, and I haven't been able to find it. If so, please accept my apologies. -- End of rant --

$LicenseServer="27000@myVCS.myDomain.local"

$HostPassword=Read-Host "Enter the password to the root account on the ESX Host" -asSecureString

$ESXConnect=Connect-VIServer -Server $HostIP -User $HostUser `

-Password (\[System.Runtime.InteropServices.Marshal\]::PtrToStringAuto( `

\[System.Runtime.InteropServices.Marshal\]::SecureStringToBSTR($HostPassword))) `

-ErrorAction Inquire

$ESXHost=Get-VMHost -Name $HostFQDN

$ESXHostView=$ESXHost | Get-View

$ServiceInstance=Get-View -Server $ESXConnect ServiceInstance

$LicManRef=$ServiceInstance.Content.LicenseManager

$LicManView=Get-View -Server $ESXConnect $LicManRef

$LicServerSource=New-Object VMWARE.VIM.LicenseServerSource

$LicServerSource.LicenseServer=$LicenseServer

$LicManView.SetLicenseEdition($ESXHostView.MoRef,"esxFull")

$LicManView.ConfigureLicenseSource($ESXHostView.MoRef,$LicServerSource)

0 Kudos
halr9000
Commander
Commander

This:

$HostView=Get-View -Viewtype HostSystem -Filter @{"Name"=($HostName + "." + $HostPath)}

Can simply be replaced by:

$HostView = Get-VMHost $HostName | Get-View

Otherwise, looks ok but I haven't tested it. I have an aversion to messing around with licensing ever since a bad 48 hours when I downed some production terminal servers several years ago. Smiley Happy






Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
240Zeus
Enthusiast
Enthusiast

I know this goes against the OP's needs, but why not just perform the majority of this in a kickstart file for installation? The only thing I do not perform in my kickstarts is adding the host to a cluster and a few other small(ish) VC-related tasks, but vSwitch setup/port group assignment, enabling vMotion, vmnic speed/duplex settings, licensing, hostname configuration, IP/DNS assignment, etc... can all be done via VIM commands in the post-processing section of the Kickstart file....

Z

0 Kudos
halr9000
Commander
Commander

Well, there's several reasons. The first and most important (to me) is that I don't know a darn thing about kickstart beyond its purpose. Smiley Happy While I know other platforms pretty well, I'm only an expert at one, and that's Windows. I'm sure I'm not the only one in this boat.

Second, this will likely prove to be easier to maintain. The tasks you can do in PowerShell will be more consistent than those done without.

It's probably easier to do it via kickstart for someone who knows bash scripting well, as well as the Linux command-line utilities on the COS.






[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
240Zeus
Enthusiast
Enthusiast

Fair enough. :smileyblush: I came from a Windows background as well but projects in the past 3-4 years have necessitated Linux use. I'm all about automation and the kickstart files are simple text-based files that the ESX installer uses to automate installs.

I simply have a template ks file in the source control repository, check it out, modify IP's, VLAN information, DNS, etc... for the specific host I want to build, add it back to the repository, and deploy it to an internal http server that my ESX servers can see on the service console VLAN. A "ks=" option with the proper information entered at the ESX install screen and ten minutes later I have a fully configured ESX host ready to add to VirtualCenter. If I ever need to rebuild the host as it originally was six months later, I just boot off of that kickstart file again.

Sorry if I've hijacked the thread here. I've been doing VMware for a few years now and am starting to port some of my historical perl scripts over to PS - I'm glad to have found this community.

If you'd like to see a ks file you can use for automated install/configure just let me know!

Z

0 Kudos
halr9000
Commander
Commander

Why not automate the ks file checkout from revision control and the file modifications as well? Smiley Happy






[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
SCampbell1
Enthusiast
Enthusiast

I think the main reason I prefer PS to Kickstart customizations is that the PS stuff can also configure ESXi servers; so you can have a very similar/identical customization script no matter which target you are aiming for.

Also there's a lot less infrastructure. No website, kickstart customizations, etc.

I would consider a static kickstart to create a baseline ESX DHCP-based server, then use the PS script to complete the customization of static addresses, root passwords, DNS names, etc.

That's the nice thing about computers: there are 100 good ways to solve any problem, and 100 people with think their way is best. Smiley Happy

0 Kudos