Author Posts

February 15, 2018 at 7:55 pm

I am trying to get my script to tell the user to manually change the encrytptionlevel for a vpn connection since Microsoft decided to make it to where Set-VPNConnection cannot change a "PAP" connection to "required" encryption.

So, when I try a while loop, it checks the value for Get-VPNConnection.encrytionlevel and if it does NOT equal "Required", it opens the network connections and tells the user to manually change encryptionlevel from "Optional", to "Required", then to press Enter when done, then rechecks and if it is still not set to loop the process. I want it to check the property and once it reads that its set to "Required", exit the loop and continue.

My problem is that even when its set to "required" it will still keep looping no matter what and never exit.

Heres my code (Dont mind the many extra words, I was playing around with other coding to try:

$Shell = $Host.UI.RawUI
$size = $Shell.WindowSize
$size.width=100
$size.height=35
$Shell.WindowSize = $size
$size = $Shell.BufferSize
$size.width=100
$size.height=35
$Shell.BufferSize = $size

Function 3BlankLines{

    Write-Host ""
    Write-Host ""
    Write-Host ""

}



if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }


3BlankLines
3BlankLines  
Write-Host "                          Just getting some tools ready before we begin"
$null = Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
$null = Install-Module -Name VPNCredentialsHelper -Force
cls
3BlankLines 
3BlankLines 
Write-Host "                             Thank You for your patience, lets begin!"

Start-Sleep -seconds 3



#$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForeGroundColor = "White"


$global:vName = $null
$global:vServer = $null
$global:vUser = $null
$global:vPassword = $null
$global:vSecret = $null
$global:vPassFile = "C:\VPN\VPN"
$global:vDIR = "C:\VPN"
$global:vGVPN = Get-VpnConnection -Name $global:vName
$global:vELevel = Write-Host $global:vGVPN.EncryptionLevel



If(!(test-path $global:vDIR))
{
      New-Item -ItemType Directory -Force -Path $global:vDIR
}


Function HostUIBW {
    #$Host.UI.RawUI.BackgroundColor = "Black"
    $Host.UI.RawUI.ForeGroundColor = "White"
}

Function HostUIBR {
    #$Host.UI.RawUI.BackgroundColor = "Black"
    $Host.UI.RawUI.ForeGroundColor = "Red"
}

Clear-Host
HostUIBW




Function vpnWelcome {
    cls
    HostUIBW
    3BlankLines
    Write-Host "                                Welcome to the"-ForegroundColor White -nonewline
    Write-Host " Computer Concern " -ForegroundColor Red
    Write-Host ""
    Write-Host "                      Client VPN and Remote Desktop Connection Setup tool" -ForegroundColor White
    3BlankLines
    HostUIBW
}

Function vpnName {
    HostUIBW
    Write-Host "                      What is the name of the VPN Connection?     "  -nonewline
    HostUIBR
    $global:vName = Read-Host
    Write-Host ""
    Write-Host ""
    
}

Function vpnAddress {
    HostUIBW
    Write-Host "                      What is the Server Address for the vpn you are setting up?      "  -nonewline
    HostUIBR
    $global:vServer = Read-Host
    Write-Host ""
    Write-Host ""
    
}

Function vpnUsername {
    HostUIBW

    Write-Host "                      What is the username for the vpn you are setting up?      "  -nonewline
    HostUIBR
    $global:vUser = Read-Host
    Write-Host ""
    Write-Host ""
    
}

Function vpnPassword {
    HostUIBW

    Write-Host  "                      What is the password for the user?       "  -nonewline
    HostUIBR
    $global:vPassword = Read-Host
    Write-Host ""
    Write-Host ""
    
}

Function vpnSecret {
    HostUIBW
    Write-Host  "                      What is the Secret Key for the VPN Connection?        "  -nonewline
    HostUIBR
    $global:vSecret = Read-Host
    Write-Host ""
    Write-Host ""
    
}

#Function vpnDone {
#}





HostUIBW
Clear-Host

#Do{
    vpnWelcome
    vpnName
    vpnAddress
    vpnUsername
    vpnPassword
    vpnSecret
    HostUIBW

                Write-Host "                      Are these settings correct? Y or N   " -nonewline
    HostUIBR
    $vConfirm = Read-Host

    while ($vConfirm -ne "y") {

        3BlankLines
        HostUIBW
        Write-Host "                     Which setting do you need to change? "
        Write-Host "                     1 - Name of VPN"
        Write-Host "                     2 - Server Address"
        Write-Host "                     3 - Username"
        Write-Host "                     4 - Password"
        Write-Host "                     5 - Secret Key"
        HostUIBR
        $vChange = Read-Host

                        switch ($vChange) {

                            1 {
                                Clear-Host
                                HostUIBW
                                3BlankLines
                                vpnName
                                
                            }

                            2 {
                                Clear-Host
                                HostUIBW
                                3BlankLines
                                vpnAddress
                                
                            }

                            3 {
                                Clear-Host
                                HostUIBW
                                3BlankLines
                                vpnUsername
                                
                            }

                            4 {
                                Clear-Host
                                HostUIBW
                                3BlankLines
                                vpnPassword
                                
                            }

                            5 {
                                Clear-Host
                                HostUIBW
                                3BlankLines
                                vpnSecret
                                
            }
        }
                HostUIBW
                Write-Host "                      Are these settings correct? Y or N   " -nonewline
    HostUIBR
    $vConfirm = Read-Host 
    }
#} Until ($vConfirm -eq "y")



HostUIBW
3BlankLines
3BlankLines
Write-Host "                          Creating VPN Connection Now!"


    $null = Add-VpnConnection -Name $vName -ServerAddress $vServer -TunnelType "L2tp" -EncryptionLevel "Optional" -AuthenticationMethod Pap -L2tpPsk $vSecret -RememberCredential -Force -Passthru
    Start-Sleep -seconds 2
    cls
    Start-Sleep -seconds 2
    3BlankLines
    3BlankLines
    Write-Host "                  Please change the encryption level to ~Require Encryption~"








############################  THIS IS THE SECTION OF CODE THAT I CANNOT GET TO WORK PROPERLY  #############################################









do{
                
               $null = $global:vGVPN.EncryptionLevel
               $global:vELevel
               3BlankLines
               3BlankLines
               Write-Host "You need to change the encryption level to REQUIRED"
               Start-Sleep -seconds 3
               cls
               HostUIBW
               ncpa.cpl
               Read-Host -Prompt "Press ENTER to continue..."
               cls
               $global:vELevel
               Write-Host "Checking encryption level now..."
               






}while($global:vELevel -NotMatch "Required")






##############################################################################################################################################









    









ConvertTo-SecureString -string $global:vPassword -asplaintext -force | convertfrom-securestring | out-file $global:vPassFile




  
  











#Start-Sleep -seconds 2

  








    $null = Set-VpnConnectionUsernamePassword -connectionname $vName -username $vUser -password $vPassword

HostUIBW
3BlankLines
3BlankLines
Write-Host "                                   Creating Additional Files Now....."


Start-Sleep -Seconds 2


New-Item $global:vDIR\Remote.rdp -ItemType file


$vFile = "C:\VPN\VPNConnect.ps1"

$vScript = @'

$Shell = $Host.UI.RawUI
$size = $Shell.WindowSize
$size.width=40
$size.height=15
$Shell.WindowSize = $size
$size = $Shell.BufferSize
$size.width=40
$size.height=15
$Shell.BufferSize = $size
$Host.UI.RawUI.ForeGroundColor = "White"
Clear-Host





#Put the name of the VPN Connection here (it's whatever you named it when you set it up, quotes must be added)
$vName ="VPN Name"


#Put the VPN username here (Quotes included)
$vUser="VPN User"


#Put the VPN password here (Quotes included)
$vPass="C:\VPN\VPN"


#Put name of RDP client here
$rdpClient="Remote.rdp"


$securestring = convertto-securestring -string (get-content $vPass)
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securestring)
$vDePass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)



#This is the If-ElseIf statement to connect/disconenct the VPN Connection
    $vpn = Get-VpnConnection -Name $vpnName
		If($vpn.ConnectionStatus -eq "Disconnected"){
		Write-Host "Connecting VPN Now..."
    $null = rasdial $vName $vUser $vDePass;
	cls
	Write-Host "...VPN now connected!"
	cls
	Write-Host "Opening Remote Connection...One Moment!"
	Start-Sleep -seconds 3
	Get-ChildItem -Path C:\ -Filter $rdpClient -Recurse -ErrorAction SilentlyContinue -Force | % {Start-Process $_.FullName}
	}
	
	    If($vpn.ConnectionStatus -eq "Connected"){
	$null = rasdial /disconnect:$vName
	Write-Host "Terminating VPN Connection"
	} 



	Start-Sleep -seconds 1
	
    
	exit


'@


$vScript| Out-File $vFile




$vVBS = @'










'@
















(Get-Content -Path $vFile) | ForEach-Object {$_ -Replace 'VPN Name', $global:vName } | Set-Content -Path $vFile
(Get-Content -Path $vFile) | ForEach-Object {$_ -Replace 'VPN User', $global:vUser } | Set-Content -Path $vFile



ii $global:vDIR


Start-Sleep -Seconds 3