$UUID = (get-wmiobject Win32_ComputerSystemProduct).UUID
$ServerEdition = (Get-WindowsEdition -Online).Edition
$TDIR="UNKNOWN"

echo "Checking System..."
Switch ($UUID)
{
  #Hyper-V
  "D9035B45-37D3-45D7-A7DF-4B3C5691B264" {$TDIR="tokens\HYPERV\"+$ServerEdition}
  #Virtual Box
  "E8B3D01C-AD3C-9745-8D7E-C1B612962519" {$TDIR="tokens\VBOX\"+$ServerEdition}
  #VmWare
  "18DC4D56-7557-78E5-28BE-C17DD2677372" {$TDIR="tokens\VMWARE\"+$ServerEdition}
  Default {echo "System UUID is unknown. Exiting..."; exit}
}

echo ""
Get-NetAdapter | ForEach-Object {echo "Disabling $($_.Name)..."; Disable-NetAdapter $_.Name -Confirm:$false}
If (Get-NetAdapter | where {$_.Status -eq "Up"}) {echo "Error, Some of the interfaces hasn't been disabled"; exit}

echo "" "Clearing product key..."
$SPPService = Get-WmiObject SoftwareLicensingService
$SPPService.ClearProductKeyFromRegistry() | Out-Null

echo "Stopping software protection service..."
Stop-Service sppsvc

echo "Updating Registry..."
New-PSDrive -Name HKU -PSProvider registry -root HKEY_USERS | Out-Null
Set-ItemProperty "HKU:\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform" -name "KmsHostConfig" -value 1
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform" -name "KeyManagementServiceVersion" -value "6.2.9200.16384"
Set-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform" -name "KeyManagementServiceVersion" -value "6.2.9200.16384"

Stop-Service sppsvc

echo "Replacing tokens..."
Copy-Item -Path "$TDIR\*" -Destination "$((get-item Env:\SystemRoot).value)\system32\spp\store\" -Confirm:$false -Force:$true

echo "Starting software protection service..."
Start-Service sppsvc

echo ""
Get-NetAdapter | ForEach-Object {echo "Enabling $($_.Name)..."; Enable-NetAdapter $_.Name -Confirm:$false}

echo "" "Checking Activation State..." ""
$SPPService = Get-WmiObject SoftwareLicensingService
$SPPService.ClearProductKeyFromRegistry() | Out-Null

$SPPService = Get-WmiObject SoftwareLicensingProduct

$WinLS = ($SPPService | where {$($_.ApplicationID -eq "55c92734-d682-4d71-983e-d6ec3f16059f") -and $($_.PartialProductKey)}).LicenseStatus
$Off2010LS = ($SPPService | where {$_.ID -eq "bfe7a195-4f8f-4f0b-a622-cf13c7d16864"}).LicenseStatus
$Off15LS = ($SPPService | where {$_.ID -eq "2e28138a-847f-42bc-9752-61b03fff33cd"}).LicenseStatus

$Product = "Windows"
Switch ($WinLS)
{
  0 {echo "$Product License Status: Unlicensed"}
  1 {echo "$Product License Status: Licensed"}
  2 {echo "$Product License Status: Initial grace period"}
  3 {echo "$Product License Status: Additional grace period (KMS license expired or hardware out of tolerance)"}
  4 {echo "$Product License Status: Non-genuine grace period."}
  5 {echo "$Product License Status: Notification"}
  6 {echo "$Product License Status: Extended grace period"}
  Default {echo "$Product License Status: Unknown"}
}

$Product = "Office 2010"
Switch ($Off2010LS)
{
  0 {echo "$Product License Status: Unlicensed"}
  1 {echo "$Product License Status: Licensed"}
  2 {echo "$Product License Status: Initial grace period"}
  3 {echo "$Product License Status: Additional grace period (KMS license expired or hardware out of tolerance)"}
  4 {echo "$Product License Status: Non-genuine grace period."}
  5 {echo "$Product License Status: Notification"}
  6 {echo "$Product License Status: Extended grace period"}
  Default {echo "$Product License Status: Unknown"}
}

$Product = "Office 2013"
Switch ($Off15LS)
{
  0 {echo "$Product License Status: Unlicensed"}
  1 {echo "$Product License Status: Licensed"}
  2 {echo "$Product License Status: Initial grace period"}
  3 {echo "$Product License Status: Additional grace period (KMS license expired or hardware out of tolerance)"}
  4 {echo "$Product License Status: Non-genuine grace period."}
  5 {echo "$Product License Status: Notification"}
  6 {echo "$Product License Status: Extended grace period"}
  Default {echo "$Product License Status: Unknown"}
}

echo "" "Enable KMS Rule in firewall..."
Show-NetFirewallRule | where {$_.DisplayName -eq "Key Management Service (TCP-In)"} | Enable-NetFirewallRule

