VMware Cloud Community
Paulk99
Enthusiast
Enthusiast

PowerCLI Reset ESXi Eval licences

Hi All

Looking for a way to reset the evaluation licences on my Lab Nested ESXi hosts [via a scripted method like PowerShell].

I have the CLI commands to reset them which of course work fine.

I've tried setting this up as an .SH script run as chron job but it seems with ESXi 7 the configuration required  to set a chron job as persistent across Host reboots has been removed. [Where UEFI boot is enabled] .

Any ideas if there is another way to achieve the same end so it can be run say once a week etc..

Kr

P

0 Kudos
8 Replies
LucD
Leadership
Leadership

If you have the Posh-Ssh module installed, you could try with

$esxName = 'MyEsxServer'
$user = 'root'
$pswd = 'VMware1!'

$cmdSub = @'
cp /etc/vmware/license.cfg /etc/vmware/license.old
rm -r /etc/vmware/license.cfg
cp /etc/vmware/.#license.cfg /etc/vmware/license.cfg
/etc/init.d/vpxa restart
'@

$cred = New-Object -TypeName PSCredential -ArgumentList $user,(ConvertTo-SecureString -String $pswd -AsPlainText -Force)

$sshService = Get-VMHostService -VMHost $esxName | where{$_.Label -eq 'SSH'}
if(-not $sshService.Running){
    Start-VMHostService -HostService $sshService -Confirm:$false
}

$session = New-SSHSession -ComputerName $esxName -Credential $cred -AcceptKey

$result = Invoke-SSHCommand -SSHSession $session -Command $cmdSub.Replace("`r", '')

Remove-SSHSession -SSHSession $session | Out-Null

if (-not $sshService.Running) {
    Stop-VMHostService -HostService $sshService -Confirm:$false
}

 


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

sdtslmn
Enthusiast
Enthusiast

Connect-VIServer -Server <ESXi_hostname> -User <username> -Password <password>
Get-VMHost | Get-VMHostLicense | Set-VMHostLicense -NewLicenseKey (Get-VMHostLicense | Select -ExpandProperty LicenseKey)

 

you can automate the above script by using 

either Windows Task Scheduler or VMware vCenter Orchestrator (vCO)

to Schedule the workflow to run weekly

0 Kudos
Paulk99
Enthusiast
Enthusiast

Hi Luc

Was hacking around with something similar but haven't been able to get it to work yet.

Thanks for the tip will carry on for the day then look ad the module you recommend

 

P

0 Kudos
Paulk99
Enthusiast
Enthusiast

Hi Luc

I gave up with mine in the end...

Loaded in Posh-SSH and the script below looks good and it works (where the Eval Licence is extended)

Except it generates an error cant (Communicate with Host) and leaves the SSH service still running.

 

P

0 Kudos
LucD
Leadership
Leadership

That might be because of the vcsa service restart.
You might want to add a sleep before trying to stop the SSH service.
Or wait in a loop till the ESXi node states "connected'


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

0 Kudos
Paulk99
Enthusiast
Enthusiast

Hi Luc 

Indeed a Sleep statement sorted it.

Sorry should of thought of that myself I've seen similar things happen many times before when pause was required.

.

Almost perfect now I think however I think I need a "ForEach" in there some where to cover the 3 hosts in the cluster.

Have tried manually entering the host names but it doesn't seem to want to play nice.

Kr

0 Kudos
LucD
Leadership
Leadership

Assuming that the credentials on each ESXi node in the cluster are the same, you could do something like this

PS: add a Sleep if required

$clusterName = 'MyCluster'
$user = 'root'
$pswd = 'VMware1!'

$cmdSub = @'
cp /etc/vmware/license.cfg /etc/vmware/license.old
rm -r /etc/vmware/license.cfg
cp /etc/vmware/.#license.cfg /etc/vmware/license.cfg
/etc/init.d/vpxa restart
'@

$cred = New-Object -TypeName PSCredential -ArgumentList $user, (ConvertTo-SecureString -String $pswd -AsPlainText -Force)

Get-Cluster -Name $clusterName | 
    Get-VMHost -PipelineVariable esx |
    ForEach-Object -Process {
        $sshService = Get-VMHostService -VMHost $esx | where { $_.Label -eq 'SSH' }
        if (-not $sshService.Running) {
            Start-VMHostService -HostService $sshService -Confirm:$false
        }

        $session = New-SSHSession -ComputerName $esx.Name -Credential $cred -AcceptKey

        $result = Invoke-SSHCommand -SSHSession $session -Command $cmdSub.Replace("`r", '')

        Remove-SSHSession -SSHSession $session | Out-Null

        Start-Sleep -Seconds 30 # If required

        if (-not $sshService.Running) {
            Stop-VMHostService -HostService $sshService -Confirm:$false
        }
    }


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

Paulk99
Enthusiast
Enthusiast

Looks like that could well be it !

.

Will rerun again tomorrow to check.

Why are these scripts never as simple as they first seem  🙂

Thanks for all your help !

0 Kudos