VMware Cloud Community
juliennas
Contributor
Contributor

Powercli to set ESXi free license

Hi all,

I'm trying to generalyze my ESXi free intallation. I'm running 5.1

I've done most of the work, but I'm still facing issue while rtying to set the free license information on my ESXi Servers.

Here is the part of the script :

##### Licence
Write-Host "Setting ESXi License on Server"
$LicMgr = Get-View LicenseManager
$TESXi  = Get-VMHost $ESXi | Get-View
$LicKey = New-Object VMware.Vim.LocalLicenseSource
$LicKey.licenseKeys = "free license number"
$LicMgr.ConfigureLicenseSource($TESXi.MoRef, $LicKey)

When I execute this, I get this error :

Setting ESXi License on Server
Get-View : 07/03/2013 15:40:19    Get-View        View with Id  'LicenseManager-LicenseManager' was not found on the server(s).   
At line:45 char:19
+ $LicMgr = Get-View <<<<  LicenseManager
    + CategoryInfo          : ObjectNotFound: (:) [Get-View], VimException
    + FullyQualifiedErrorId : Core_GetView_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

You cannot call a method on a null-valued expression.
At line:49 char:31
+ $LicMgr.ConfigureLicenseSource <<<< ($TESXi.MoRef, $LicKey)
    + CategoryInfo          : InvalidOperation: (ConfigureLicenseSource:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Is this due to a limitation in ESXi hypervisor or am I not coding correctly ?

Thanks for your help,

Julien

0 Kudos
6 Replies
RvdNieuwendijk
Leadership
Leadership

Hi Julien,

you are probably running the "$LicMgr = Get-View LicenseManager" command while connected to an ESXi server. Then this command will not work. It will only work when connected to a vCenter server.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
juliennas
Contributor
Contributor

I'm running all the script directly on the free hypervisor, that's right.

This command only work while attached to a VC ?

That's weird ... Any way to adapt this for a free ESXi ?

btw, thanks for this quick reply.

Julien

0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can use:

$ServiceInstance = Get-View ServiceInstance
$LicenseManager = $ServiceInstance.Content.LicenseManager
$LicenseManagerView = Get-View $LicenseManager
$esxLicense = $LicenseManagerView.Licenses 

This script will work when connected to an ESXi server.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
juliennas
Contributor
Contributor

Hi Robert,

Sorry to bother you again, but I really don't know how to set license key ...

I've rtied your commands, and it shows no more errors, they gave me this :

$ServiceInstance     = Get-View ServiceInstance

     ServerClock : 08/03/2013 07:32:48
     Capability  : VMware.Vim.Capability
     Content     : VMware.Vim.ServiceContent
     MoRef       : ServiceInstance-ServiceInstance
     Client      : VMware.Vim.VimClient

$LicenseManager      = $ServiceInstance.Content.LicenseManager

     Type                                                                           Value                                                                        
     ----                                                                               -----                                                                        
     LicenseManager                                                           ha-license-manager

$LicenseManagerView = Get-View $LicenseManager

     Source                   :
     SourceAvailable          : False
     Diagnostics              :
     FeatureInfo              : {}
     LicensedEdition          :
     Licenses                 : {Evaluation Mode}
     LicenseAssignmentManager :
     Evaluation               : VMware.Vim.LicenseManagerEvaluationInfo
     LinkedView               :
     MoRef                    : LicenseManager-ha-license-manager
     Client                   : VMware.Vim.VimClient

$ESXLicense         = $LicenseManagerView.Licenses

     LicenseKey      : 00000-00000-00000-00000-00000
     EditionKey      : eval
     Name            : Evaluation Mode
     Total           : 1
     Used            : 1
     CostUnit        : host
     Properties      : {ProductName, ProductVersion, evaluation, expirationHours...}
     Labels          :
     DynamicType     :
     DynamicProperty :

When I try

$esxLicense.Set           = "My_ESXi_License_Number"

     error :

     Cannot set the Value property for PSMemberInfo object of type "System.Management.Automation.PSMethod".
     At line:40 char:13
     + $esxLicense. <<<< SetValue = "XXXX-XXXXX-xxxxx-xxxxx-XXXXX"
         + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
         + FullyQualifiedErrorId : PropertyAssignmentException

$esxLicense.SetValue   = "My_ESXi_License_Number"

     error:

     Cannot set the Value property for PSMemberInfo object of type "System.Management.Automation.PSMethod".
     At line:40 char:13
    + $esxLicense. <<<< Set = "XXXX-XXXXX-xxxxx-xxxxx-XXXXX"
         + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
         + FullyQualifiedErrorId : PropertyAssignmentException

I'm also trying $LicenseManagerView.AddLicense("XXXX-XXXXX-xxxxx-xxxxx-XXXXX", ??,??)

and $LicenseManagerView.UpdateLicense("XXXX-XXXXX-xxxxx-xxxxx-XXXXX", ??,??)

this ?? means I don't know how to fill ... 😕

On the others ESXi manually installed, I have this :

LicenseKey        : XXXX-XXXXX-xxxxx-xxxxx-XXXXX
EditionKey         : esxBasic.vram
Name                 : VMware vSphere 5 Hypervisor
Total                     : 0
Used                      : 1
CostUnit             : cpuPackage
Properties           : {ProductName, ProductVersion, count_disabled, feature...}
Labels                    :
DynamicType          :
DynamicProperty      :

Thanks,

Julien

0 Kudos
juliennas
Contributor
Contributor

Hi all,

Think I got it :

$LicenseManagerView = Get-View -Id "LicenseManager-ha-license-manager"
$LicenseManagerView.UpdateLicense($Lic,$null)

I'll test on a fresh install cause I've tested so many times ....

Let you know Smiley Happy

Julien

0 Kudos
juliennas
Contributor
Contributor

Hi all,

If interested, here is the full script to configure my ESXi 5.1 host. Fell free to modify, adapt or improve ...

I put the licence at the last step because, when it s done, you loose a lot of functionnality 😕

###########################################################################
# NAME                : ESXi Agencies Post-Configuration Script
# AUTHOR            : JLE
# COMMENT            :
# VERSION HISTORY   : 1.1 07/03/2013 Added SanpIn check + DNS & Routing Tab
###########################################################################
CLS
#### Custom Properties
If ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ){Add-PSSnapIn VMware.VimAutomation.Core}

#### Functions
Function Sleep-Time {
##==============================================
## Pause Program for 15 min - Matt Brown, 2008
##==============================================
    $x = 900 ; $length = $x / 100
    While($x -gt 0) {
          $min = [Int](([String]($x/60)).split('.')[0]) ; $text = " " + $min + " Minutes " + ($x % 60) + " Seconds Left"
          Write-Progress "Pausing Script" -status $text -perc ($x/$length) ;     Start-Sleep -S 1 ; $x--}}

#### Variables
$ESXi = "IP or Hostname"
$AD   = "Domain.com"
$DNS1 = "DNSIP1"

$DNS2 = "DNSIP2"
$User = "root"
$Pass = ""
$NTP1 = "NTP1.Domain.com"

$NTP2 = "NTP2.Domain.com"

$NTP3 = "NTP3.Domain.com"
$MNic = "vmnic0","vmnic1"

$VNic = "vmnic2","vmnic3"
$Vs1N = "LAN Name"
$DS1  = "Datastore Name1"

$DS2  = "Datastore Name2"
$Lic  = "Licence Number"

## Start Of The Script
Write-Host "Connecting to $ESXi Server as Root with a Blank Password" ; Connect-VIServer $ESXi  -User $User -Password ""
Sleep -Seconds 5

##### Set DNS & Routing Tab
Write-Host "Setting Up DNS & Routing Tab "
Get-VMHost | Get-VMHostNetwork | Set-VMHostNetwork -SearchDomain $AD -DomainName $AD -DnsAddress $DNS1, $DNS2 -IPv6Enabled:$false -Confirm:$false | Out-Null

##### Adding ESXi to AD Domain
Write-Host "Adding $ESXi to $AD ( ADMxxxxxx/password) "
$CredAD   = $Host.UI.PromptForCredential("AD Credentials", "Enter a login/password to join $AD", "", "")
$CredUser = $CredAD.GetNetworkCredential().UserName ; $CredPass = $CredAD.GetNetworkCredential().Password
Get-VMHostAuthentication | Set-VMHostAuthentication -Domain $AD -JoinDomain -Confirm:$false -Username $CredUser -Password $CredPass | Out-Null

##### Change root Password
Write-Host "Changing Root Password on $ESXi"
Set-VMHostAccount -UserAccount $User -Password $Pass -Confirm:$false | Out-Null

##### Configure vSwitch0 & vSwitch1
Write-Host "Configuring vSwitch0"

$Vs0 = Get-VirtualSwitch -Name vSwitch0
Write-Host "Adding $MNic to vSwitch0"

Set-VirtualSwitch -VirtualSwitch $Vs0 -Nic $MNic -Confirm:$false | Out-Null
Write-Host "Removing VM Network from vSwitch0"

Get-VirtualPortGroup | Where { $_.Name -like "VM Network"} | Remove-VirtualPortGroup -Confirm:$false | Out-Null
Write-Host "Creating vSwitch1 and Add $VNic"

$Vs1 = New-VirtualSwitch -Name vSwitch1 -nic $VNic -Confirm:$false
Write-Host "Adding $Vs1N to vSwitch1"

New-VirtualPortGroup -VirtualSwitch $Vs1 -Name $Vs1N -Confirm:$false | Out-Null

##### Configure NTP
Write-Host "Configuring NTP Servers"
Write-Host "Adding NTP Servers"

Add-VmHostNtpServer -NtpServer $NTP1,$NTP2,$NTP3 -Confirm:$false
Write-Host "Set NTP Daemon to Start with Host and Starting it"
$NTP = Get-VMHostService | where {$_.Key -eq "ntpd"}

Set-VMHostService -HostService $NTP -Policy "On" -Confirm:$false | Out-Null

Restart-VMHostService -HostService $NTP -Confirm:$false | Out-Null

##### Rename Local Datastore1 & Create Local Datastore2
Write-Host "Rename default local datastore1 to $DS1"

Get-Datastore -Name "datastore1" | Set-Datastore -Name $DS1 | Out-Null
Write-Host "Creating VM Datastore on RAID5, Datastore name will be $DS2 "
New-Datastore -Name $DS2 -Vmfs -FileSystemVersion 5 -Path (Get-ScsiLun -LunType disk | Where {$_.CapacityGB -cgt 300}).CanonicalName | Out-Null

##### Adding AD Groups to Server
Write-Host "Add AD's Domain Groups to ESXi Permission"
Write-Host "Pausing Script for 15 Minutes for AD replication to be Done ... " ; Sleep-Time
Write-Host "Add AD's Domain Admins to Admin Role"
New-VIPermission -Principal "Domain\Domain Admins"         -Role "Admin"     -Confirm:$false -Propagate:$true -Entity "ha-folder-root" -ErrorAction SilentlyContinue |Out-Null
Write-Host "Add AD's vSphere.ReadOnly to Read Only Role"
New-VIPermission -Principal "Domain\vsphere.readonly"     -Role "ReadOnly"-Confirm:$false -Propagate:$true -Entity "ha-folder-root" -ErrorAction SilentlyContinue |Out-Null

##### Licence
Write-Host "Setting ESXi License on Server"
$LicenseManagerView = Get-View -Id "LicenseManager-ha-license-manager" ; $LicenseManagerView.UpdateLicense($Lic,$null) |Out-Null
<#VC PART
$LicMgr = Get-View LicenseManager
$TESXi  = Get-VMHost $ESXi | Get-View
$LicKey = New-Object VMware.Vim.LocalLicenseSource
$LicKey.licenseKeys = "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
$LicMgr.ConfigureLicenseSource($TESXi.MoRef, $LicKey)#>

## End Of The Script
Write-Host "Move $ESXi from OU Computers to the right OU " -BackgroundColor Red -ForegroundColor Green
Write-Host "Don't forget to Reboot $ESXi before doing anything else !!!! " -BackgroundColor Red -ForegroundColor Green
Write-Host "Disconnecting ESXi Server" ; Disconnect-VIServer $ESXi -Confirm:$false -Force

Ce message a été modifié par: juliennas

0 Kudos