-
-
Save materro/f7a97f7771acde20a65e5da7561e769d to your computer and use it in GitHub Desktop.
Connect to VPN and activate Office using KMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param([switch]$elevated) | |
| # 💡 Enter your VPN and KMS server addresses: | |
| $VPN_ADDRESS = 'vpn.address.com' | |
| $KMS_ADDRESS = 'kms.address.com' | |
| $OFFICE_DIR = 'C:\Program files\Microsoft Office\Office16\' | |
| # 👏 Based on: | |
| # https://gist.github.com/ALiangLiang/b66bc6eaeab420a5ff0c2b934c15451e | |
| # https://superuser.com/questions/108207/how-to-run-a-powershell-script-as-administrator | |
| function Is-Admin-Scope { | |
| $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) | |
| $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) | |
| } | |
| if (-Not (Is-Admin-Scope)) { | |
| if (-Not $elevated) { | |
| $arguments = '-noprofile -file "{0}" -elevated' -f $myinvocation.MyCommand.Definition | |
| Start-Process powershell -Verb RunAs -ArgumentList $arguments | |
| } | |
| exit | |
| } | |
| $EpochSeconds = [int][double]::Parse((Get-Date (Get-Date).ToUniversalTime() -UFormat %s)) | |
| $VPN_NAME = 'VPN_' + $EpochSeconds.ToString() | |
| function Connect { | |
| # Get password from prompt | |
| Write-Host 'Enter VPN details...' | |
| $account = Read-Host '> What is your username?' | |
| $pass = Read-Host '> What is your password?' -AsSecureString | |
| $pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)) | |
| # Establish VPN configuration | |
| # Ref: https://docs.microsoft.com/en-us/powershell/module/vpnclient/add-vpnconnection?view=win10-ps | |
| Add-VpnConnection -Name "$VPN_NAME" -ServerAddress "$VPN_ADDRESS" -TunnelType PPTP -EncryptionLevel Required -RememberCredential -PassThru | |
| # Connect the VPN configuration | |
| $code = (Start-Process rasdial -NoNewWindow -ArgumentList "$VPN_NAME $account $pass" -PassThru -Wait).ExitCode | |
| if ("$code" -eq "0") { | |
| Write-Host "Create and connect to VPN server success" -ForegroundColor DarkGreen | |
| return $true | |
| } else { | |
| # Error codes: https://support.microsoft.com/en-us/help/824864/list-of-error-codes-for-dial-up-connections-or-vpn-connections | |
| if ("$code" -eq "691") { | |
| Write-Host "Create and connect to VPN server failed with wrong username or password" -ForegroundColor DarkRed | |
| return $false # return and try againg | |
| } else { | |
| Write-Host "Create and connect to VPN server failed with error code: $($code)" -ForegroundColor DarkRed | |
| throw "$code" | |
| } | |
| } | |
| } | |
| function Disconnect { | |
| # Disconnect | |
| rasdial "$VPN_NAME" /DISCONNECT | |
| # Remove it | |
| Remove-VpnConnection -Name "$VPN_NAME" -Force | |
| Write-Host "Disconnect and remove VPN connection success" -ForegroundColor Magenta | |
| } | |
| function Pause { | |
| param($text) | |
| Write-Host "$text" | |
| [void][System.Console]::ReadKey($true) | |
| } | |
| function Activate-Office { | |
| cd "$OFFICE_DIR" | |
| cscript c:\windows\system32\slmgr.vbs -skms $KMS_ADDRESS | |
| cscript ospp.vbs /sethst:$KMS_ADDRESS | |
| cscript ospp.vbs /act | |
| } | |
| # If no connection created before... | |
| if (-Not [bool] (Get-VpnConnection -Name "$VPN_NAME" -ErrorAction SilentlyContinue)){ | |
| while (-Not (Connect)) {} | |
| Activate-Office | |
| # Pause the progress | |
| Pause -text "Press any key to stop VPN..." | |
| } | |
| Disconnect | |
| # Pause the progress | |
| Pause -text "Press any key to close window..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment