-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_license.ps1
85 lines (74 loc) · 3.31 KB
/
function_license.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<#
.SYNOPSIS
These functions can be used to manipulate vSphere licenses
Many thanks to Michael Munk Lassen (@munklarsen - https://twitter.com/munklarsen) for below functions
.DESCRIPTION
Below functions can
- View Licenses
- Add Licenses
- Remove Licenses
- Add custom named labels to vSphere licenses
- Remove custom named labels
.INPUTS
n/a
.OUTPUTS
Outputs to screen
.NOTES
Website: www.amikkelsen.com
Author: Anders Mikkelsen
Creation Date: 2019-01-28
Known bugs:
n/a
.EXAMPLE
N/A
#>
function vSphere-LicenseView {
param([string]$licenseKey)
$servInstance = Get-View ServiceInstance
$licenseManager = Get-View $servInstance.content.licenseManager
if($licenseKey) {
return $licenseManager.Licenses | Where-Object ({$_.LicenseKey -eq $licenseKey})
} else {
return $licenseManager.Licenses
}
}
function vSphere-LicenseAdd {
param([Parameter(Mandatory=$true)] [string]$licenseKey)
$servInstance = Get-View ServiceInstance
$licenseManager = Get-View $servInstance.content.licenseManager
$licenseManager.AddLicense($licenseKey,$null)
}
function vSphere-LicenseRemove {
param([Parameter(Mandatory=$true)] [string]$licenseKey)
$licenseUsage = vSphere-LicenseView -licenseKey $licenseKey
if($licenseUsage.Used -eq "0") {
$servInstance = Get-View ServiceInstance
$licenseManager = Get-View $servInstance.content.licenseManager
$licenseManager.RemoveLicense($licenseKey)
} else {
return "Error: License in use"
}
}
function vSphere-LicenseLabelSet {
param([Parameter(Mandatory=$true)] [string]$licenseKey, [Parameter(Mandatory=$true)] [string]$licenseKeyLabelKey, [Parameter(Mandatory=$true)] [string]$licenseKeyLabelValue)
$servInstance = Get-View ServiceInstance
$licenseManager = Get-View $servInstance.content.licenseManager
($licenseManager.Licenses | Where-Object ({$_.LicenseKey -eq $licenseKey})).licenseKey
$licenseManager.UpdateLicenseLabel(($licenseManager.Licenses | Where-Object ({$_.LicenseKey -eq $licenseKey})).licenseKey, $licenseKeyLabelKey, $licenseKeyLabelValue)
}
function vSphere-LicenseLabelRemove {
param([Parameter(Mandatory=$true)] [string]$licenseKey, [Parameter(Mandatory=$true)] [string]$licenseKeyLabelKey)
$servInstance = Get-View ServiceInstance
$licenseManager = Get-View $servInstance.content.licenseManager
($licenseManager.Licenses | Where-Object ({$_.LicenseKey -eq $licenseKey})).licenseKey
$licenseManager.RemoveLicenseLabel(($licenseManager.Licenses | Where-Object ({$_.LicenseKey -eq $licenseKey})).licenseKey, $licenseKeyLabelKey)
}
Connect-VIServer 192.168.1.200 -User "[email protected]" -Password "xxxxxx"
## Uncomment the function you need and replace the licensekey "00000-00000-00000-00000-00000" with your own ##
#vSphere-LicenseView
#vSphere-LicenseView -licenseKey "00000-00000-00000-00000-00000"
#vSphere-LicenseAdd -licenseKey "00000-00000-00000-00000-00000"
#vSphere-LicenseRemove -licenseKey "00000-00000-00000-00000-00000"
#vSphere-LicenseLabelSet -licenseKey "00000-00000-00000-00000-00000" -licenseKeyLabelKey "MyCustomNote" -licenseKeyLabelValue "TEST1234"
#vSphere-LicenseLabelRemove -licenseKey "00000-00000-00000-00000-00000" -licenseKeyLabelKey "MyCustomNote"
Disconnect-VIServer -Confirm:$false