0 0

Silent install OpenVPN

Find latest version, download and install:

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main" -Name "DisableFirstRunCustomize" -Value 2
$key = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $key -Name "WarnonBadCertRecving" -Value 0
Set-ItemProperty -Path $key -Name "WarnonZoneCrossing" -Value 0
Set-ItemProperty -Path $key -Name "WarnOnPostRedirect" -Value 0
$ieKey = "HKCU:\Software\Microsoft\Internet Explorer\Main"
if (Test-Path $ieKey) { Set-ItemProperty -Path $ieKey -Name "DisableFirstRunCustomize" -Value 2 }
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$ProgressPreference = "SilentlyContinue"
$webClient = New-Object System.Net.WebClient
$url = "https://build.openvpn.net/downloads/releases/"
$html = $webClient.DownloadString($url)
$pattern = 'href="(OpenVPN-(\d+\.\d+\.\d+)-I\d+-amd64\.msi)"'
$matches = [regex]::Matches($html, $pattern)
if ($matches.Count -eq 0) {
    $pattern2 = 'OpenVPN-(\d+\.\d+\.\d+)-I(\d+)-amd64\.msi'
    $matches2 = [regex]::Matches($html, $pattern2)
    if ($matches2.Count -gt 0) {
        $latestVersion = $matches2 | ForEach-Object { 
            [PSCustomObject]@{
                FullMatch = $_.Value
                Version = [version]($_.Groups[1].Value)
                BuildNumber = [int]($_.Groups[2].Value)
            }
        } | Sort-Object Version, BuildNumber -Descending | Select-Object -First 1
        $fileName = $latestVersion.FullMatch
        $latestVersionLink = "https://build.openvpn.net/downloads/releases/$fileName"
    }
} else {
    $latestVersionLink = $matches | ForEach-Object { 
        [PSCustomObject]@{
            Link = "https://build.openvpn.net/downloads/releases/" + $_.Groups[1].Value
            Version = [version]($_.Groups[2].Value)
            FileName = $_.Groups[1].Value
        }
    } | Sort-Object Version -Descending | Select-Object -First 1 | ForEach-Object { $_.Link }
    $fileName = ($latestVersionLink -split '/')[-1]
}
Write-Host "Found: $latestVersionLink" -ForegroundColor Green
if ($latestVersionLink) {
    $Path = "C:\Users\Public\Downloads"
    if (!(Test-Path $Path)) {
        New-Item -ItemType Directory -Path $Path -Force
    }
    $webClient.DownloadFile($latestVersionLink, "$Path\$fileName")
    taskkill /f /im openvpn-gui.exe 2>$null
    Start-Process -FilePath "msiexec.exe" -ArgumentList "/i `"$Path\$fileName`" /qb" -Wait
    REG ADD "HKLM\Software\OpenVPN" /v "disable_save_passwords" /t REG_DWORD /d 1 /f
}

Download:
$Link = "https://swupdate.openvpn.org/community/releases/OpenVPN-2.6.11-I001-amd64.msi"
$Path = "C:\Users\Public\Downloads"
$File = "openvpn-setup.msi"
$ProgressPreference = "SilentlyContinue"
Invoke-WebRequest -Uri $Link -OutFile $Path\$File -UseBasicParsing
msiexec.exe /i "$Path\$File" /qb
REG ADD "HKLM\Software\OpenVPN" /v "disable_save_passwords" /t REG_DWORD /d 1 /f

New-LocalGroup "OpenVPN Administrators"
net localgroup "OpenVPN Administrators" AzureAD\User@domain.com /add

Download config:
$ProgressPreference = "SilentlyContinue"
$Link = "https://insert_link_to/config.zip"
$User = ""
$Pass = ""
$File = "ovpn.zip"
$Path = "C:\Users\Public\Downloads"
$ConfigPath = "C:\Program Files\OpenVPN\config"
if (Test-Path $Path\$File) {
  Remove-Item $Path\$File
}
Invoke-WebRequest -Uri $Link -OutFile $Path\$File -UseBasicParsing -Credential (New-Object PSCredential($User, (ConvertTo-SecureString -AsPlainText -Force -String $Pass)))
Expand-Archive $Path\$File $ConfigPath