-
Notifications
You must be signed in to change notification settings - Fork 94
/
VSCELicense.psm1
293 lines (237 loc) · 7.86 KB
/
VSCELicense.psm1
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#region Constants
New-Variable -Name VSCELicenseMap -Value @{
'2013' = 'Licenses\E79B3F9C-6543-4897-BBA5-5BFB0A02BB5C\06177'
'2015' = 'Licenses\4D8CFBCB-2F6A-4AD2-BABF-10E28F6F2C8F\07078'
'2017' = 'Licenses\5C505A59-E312-4B89-9508-E162F8150517\08878'
'2019' = 'Licenses\41717607-F34E-432C-A138-A3CFD7E25CDA\09278'
} -Option Constant
#endregion
#region Internal functions
<#
.Synopsis
Test if PowerShell is running as elevated process
#>
function Test-Elevation {
[bool](
(
[System.Security.Principal.WindowsIdentity]::GetCurrent()
).Groups -contains 'S-1-5-32-544'
)
}
<#
.Synopsis
Converts VS CE binary date format to [datetime]
#>
function ConvertFrom-BinaryDate {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[ValidateCount(6, 6)]
[uint16[]]$InputObject
)
End {
Get-Date -Year (
[System.BitConverter]::ToInt16(
$InputObject[0..1],
0
)
) -Month (
[System.BitConverter]::ToInt16(
$InputObject[2..3],
0
)
) -Day (
[System.BitConverter]::ToInt16(
$InputObject[4..6],
0
)
) -Hour 0 -Minute 0 -Second 0
}
}
<#
.Synopsis
Convert [datetime] to VS CE binary date format
#>
function ConvertTo-BinaryDate {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[datetime]$Date
)
Process {
$Date.Year, $Date.Month, $Date.Day | ForEach-Object {
[System.BitConverter]::GetBytes([uint16]$_)
}
}
}
<#
.Synopsis
Open registry subkey in HKCR for read(write) access
#>
Function Open-HKCRSubKey {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string]$SubKey,
[switch]$ReadWrite
)
Begin {
if ($ReadWrite -and -not (Test-Elevation)) {
throw 'This action requires elevated permissions. Run PowerShell as Administrator.'
}
}
Process {
try {
$HKCR = [Microsoft.Win32.RegistryKey]::OpenBaseKey(
[Microsoft.Win32.RegistryHive]::ClassesRoot,
[Microsoft.Win32.RegistryView]::Default
)
$LicenseKey = $HKCR.OpenSubKey(
$SubKey,
$ReadWrite
)
}
catch {
throw $_
}
finally {
$HKCR.Dispose()
}
if ($null -ne $LicenseKey) {
$LicenseKey
}
}
}
#endregion
#region External functions
<#
.Synopsis
Get Visual Studio Community Edition license expiration date
.Parameter Version
String array. One ore more of the supported Visual Studio Community Edition versions.
Default: '2013', '2015', '2017', '2019'
.Example
Get-VSCELicenseExpirationDate -Version 2017
Get expiration date for all supported versions of Visual Studio.
.Example
Get-VSCELicenseExpirationDate -Version 2017
Get expiration date for Visual Studio 2017.
#>
function Get-VSCELicenseExpirationDate {
[CmdletBinding()]
Param (
[ValidateSet('2013', '2015', '2017', '2019')]
[string[]]$Version = @('2013', '2015', '2017', '2019')
)
End {
foreach ($v in $Version) {
if ($LicenseKey = Open-HKCRSubKey -SubKey $VSCELicenseMap.$v) {
try {
$LicenseBlob = [System.Security.Cryptography.ProtectedData]::Unprotect(
$LicenseKey.GetValue($null),
$null,
[System.Security.Cryptography.DataProtectionScope]::LocalMachine
)
}
catch {
throw $_
}
finally {
$LicenseKey.Dispose()
}
[PSCustomObject]@{
Version = $v
ExpirationDate = ConvertFrom-BinaryDate $LicenseBlob[-16..-11] -ErrorAction Stop
}
}
}
}
}
<#
.Synopsis
Set Visual Studio Community Edition license expiration date
.Description
Set Visual Studio Community Edition license expiration date.
Will add 31 day from current date by default.
This is max allowed number of days, otherwise your license will be deemed invalid.
.Parameter Version
String array. One ore more of the supported Visual Studio Community Edition versions.
Default: '2013', '2015', '2017', '2019'
.Parameter AddDays
Int. Number of days to add. 31 is max allowed and default.
.Example
Set-VSCELicenseExpirationDate
Set license expiration date to current date + 31 day for all supported versions of Visual Studio.
.Example
Set-VSCELicenseExpirationDate -Version 2019
Set license expiration date to current date + 31 day for Visual Studio 2019 only.
.Example
Set-VSCELicenseExpirationDate -AddDays 10
Set license expiration date to current date + 10 days for all supported versions of Visual Studio.
.Example
Set-VSCELicenseExpirationDate -Version 2019 -AddDays 0
Set license expiration date to current date for Visual Studio 2019 only.
This will immediately expire your license and you wouldn't be able to use Visual Studio 2019.
#>
function Set-VSCELicenseExpirationDate {
[CmdletBinding()]
Param (
[ValidateSet('2013', '2015', '2017', '2019')]
[string[]]$Version = @('2013', '2015', '2017', '2019'),
[ValidateRange(0, 31)]
[int]$AddDays = 31
)
End {
foreach ($v in $Version) {
if ($LicenseKey = Open-HKCRSubKey -SubKey $VSCELicenseMap.$v -ReadWrite) {
try {
$LicenseBlob = [System.Security.Cryptography.ProtectedData]::Unprotect(
$LicenseKey.GetValue($null),
$null,
[System.Security.Cryptography.DataProtectionScope]::LocalMachine
)
$NewExpirationDate = [datetime]::Today.AddDays($AddDays)
$LicenseKey.SetValue(
$null,
[System.Security.Cryptography.ProtectedData]::Protect(
@(
$LicenseBlob[ - $LicenseBlob.Count..-17]
$NewExpirationDate | ConvertTo-BinaryDate -ErrorAction Stop
$LicenseBlob[-10..-1]
),
$null,
[System.Security.Cryptography.DataProtectionScope]::LocalMachine
),
[Microsoft.Win32.RegistryValueKind]::Binary
)
}
catch {
throw $_
}
finally {
$LicenseKey.Dispose()
}
[PSCustomObject]@{
Version = $v
ExpirationDate = $NewExpirationDate
}
}
}
}
}
#endregion
#region Init
if (-not ([System.Management.Automation.PSTypeName]'System.Security.Cryptography.ProtectedData').Type) {
$ModulePath = $Script:MyInvocation.MyCommand.Path
$ModuleFolder = [System.IO.Path]::GetDirectoryName($ModulePath)
$ModuleManifest = Join-Path $ModuleFolder ([System.IO.Path]::GetFileNameWithoutExtension($ModulePath) + '.psd1')
@(
"Loading 'System.Security' assembly."
'To avoid this message, import module using manifest or folder name w/o trailing slash:'
"Import-Module -Name '$ModuleManifest'"
"Import-Module -Name '$ModuleFolder'"
) -join [System.Environment]::NewLine | Write-Warning
Add-Type -AssemblyName 'System.Security' -ErrorAction Stop
}
#endregion