Im trying to understand why my loops are not working properly. Its supposed to execute the block of code (each function I created), then ask if the settings are correct or not and if they are not, its supposed to ask which setting I need to change, let me change it then ask for confirmation again. Allow me to keep changing things until I say “y” im done, then take the variables from each one to execute the next section of code.
It works all the way up until it asks me what changes I want to make, then once I make the first one, it automatically loops the question about which change I want to make instead of confirming if im done making changes.
Function HostUIBW {
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForeGroundColor = "White"
}
Function HostUIBR {
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForeGroundColor = "Red"
}
HostUIBW
Clear-Host
Function vpnWelcome {
cls
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
Write-Host " Welcome to the"-ForegroundColor White -nonewline
Write-Host " Computer Concern " -ForegroundColor Red
Write-Host `n
Write-Host " Client VPN and Remote Desktop Connection Setup tool" -ForegroundColor White
Write-Host `n
Write-Host `n
Write-Host `n
HostUIBW
}
Function vpnName {
HostUIBW
Write-Host " What is the name of the VPN Connection? " -nonewline
HostUIBR
$vName = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnAddress {
HostUIBW
Write-Host " What is the Server Address for the vpn you are setting up? " -nonewline
HostUIBR
$vServer = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnUsername {
HostUIBW
Write-Host " What is the username for the vpn you are setting up? " -nonewline
HostUIBR
$vUser = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnPassword {
HostUIBW
Write-Host " What is the password for the user? " -nonewline
HostUIBR
$vPassword = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnSecret {
HostUIBW
Write-Host " What is the Secret Key for the VPN Connection? " -nonewline
HostUIBR
$vSecret = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnDone {
}
Do{
vpnWelcome
vpnName
vpnAddress
vpnUsername
vpnPassword
vpnSecret
HostUIBW
$vConfirm = Read-Host -Prompt "Are these settings correct? Y or N"
#while ($vConfirm -ne "y") {
If ($vConfirm -ne "y") {
Write-Host `n
Write-Host `n
Write-Host `n
Write-Host "Which setting do you need to change? 1, 2, 3, 4 or 5"
Write-Host "1 - Name of VPN"
Write-Host "2 - Server Address"
Write-Host "3 - Username"
Write-Host "4 - Password"
Write-Host "5 - Secret Key"
$vChange = Read-Host
If ($vChange -eq "1"){
cls
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnName
$vConfirm
}
ElseIf ($vChange -eq "2"){
cls
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnAddress
$vConfirm
}
ElseIf ($vChange -eq "3"){
cls
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnUsername
$vConfirm
}
ElseIf ($vChange -eq "4"){
cls
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnPassword
$vConfirm
}
ElseIf ($vChange -eq "5"){
cls
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnSecret
$vConfirm
}
Else{
Write-Host "Creating VPN Connection Now!"
}
}
}Until ($vConfirm -eq "y")
This code works great when asking for new input, but when I try to take those variables and use them in the next line of code, they dont actually work.
Here is the full code (Edited with yours( After “Creating VPN Connection Now!”, code does not create VPN)):
Also, every output displayed on the screen is also showing a square box and “n” all the way to the left. It acts as if its trying to execute the “Write-Host `n” command but not actually doing it correctly.
Function HostUIBW {
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForeGroundColor = "White"
}
Function HostUIBR {
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForeGroundColor = "Red"
}
Function vpnWelcome {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
Write-Host " Welcome to the"-ForegroundColor White -nonewline
Write-Host " Computer Concern " -ForegroundColor Red
Write-Host `n
Write-Host " Client VPN and Remote Desktop Connection Setup tool" -ForegroundColor White
Write-Host `n
Write-Host `n
Write-Host `n
HostUIBW
}
Function vpnName {
HostUIBW
Write-Host " What is the name of the VPN Connection? " -nonewline
HostUIBR
$vName = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnAddress {
HostUIBW
Write-Host " What is the Server Address for the vpn you are setting up? " -nonewline
HostUIBR
$vServer = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnUsername {
HostUIBW
Write-Host " What is the username for the vpn you are setting up? " -nonewline
HostUIBR
$vUser = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnPassword {
HostUIBW
Write-Host " What is the password for the user? " -nonewline
HostUIBR
$vPassword = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnSecret {
HostUIBW
Write-Host " What is the Secret Key for the VPN Connection? " -nonewline
HostUIBR
$vSecret = Read-Host
Write-Host `n
Write-Host `n
}
Function vpnDone {
}
HostUIBW
Clear-Host
Do{
vpnWelcome
vpnName
vpnAddress
vpnUsername
vpnPassword
vpnSecret
HostUIBW
$vConfirm = Read-Host -Prompt "Are these settings correct? Y or N"
while ($vConfirm -ne "y") {
Write-Host `n
Write-Host `n
Write-Host `n
Write-Host "Which setting do you need to change? 1, 2, 3, 4 or 5"
Write-Host "1 - Name of VPN"
Write-Host "2 - Server Address"
Write-Host "3 - Username"
Write-Host "4 - Password"
Write-Host "5 - Secret Key"
$vChange = Read-Host
switch ($vChange) {
1 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnName
$vConfirm
}
2 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnAddress
$vConfirm
}
3 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnUsername
$vConfirm
}
4 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnPassword
$vConfirm
}
5 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnSecret
$vConfirm
}
}
$vConfirm = Read-Host -Prompt "Are these settings correct? Y or N"
}
} Until ($vConfirm -eq "y")
Write-Host "Creating VPN Connection Now!"
Add-VpnConnection -Name $vName -ServerAddress $vServer -TunnelType "L2tp" -EncryptionLevel "Optional" -AuthenticationMethod Pap -L2tpPsk $vSecret -RememberCredential -Force -Passthru
cls
Write-Host "Please change the encryption level to ~Require Encrption~"
ncpa.cpl
Cls
do {
cls
ncpa.cpl
$vContinue = Read-Host -Prompt "Did you change it? Y or N"
}Until ($vContinue -eq "y")
cls
Write-Host "YAY you changed it"
Start-Sleep -Seconds 800
vpnName : The term ‘vpnName’ is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path
is correct and try again.
At C:\Users\Dennis\Documents\VPN_Project\VPN_Creation - Copy.ps1:12 char:10
vpnAddress : The term ‘vpnAddress’ is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that
the path is correct and try again.
At C:\Users\Dennis\Documents\VPN_Project\VPN_Creation - Copy.ps1:13 char:12
vpnUser : The term ‘vpnUser’ is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path
is correct and try again.
At C:\Users\Dennis\Documents\VPN_Project\VPN_Creation - Copy.ps1:14 char:10
And even when it gets passed the first part and asks the questions, it still doesnt deliver the variable outputs to the code below it to actually create the vpn connection itself
Heres the code:
$vName = vpnName
$vServer = vpnAddress
$vUser = vpnUser
$vPassword = vpnPassword
$vSecret = vnpSecret
Function HostUIBW {
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForeGroundColor = "White"
}
Function HostUIBR {
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForeGroundColor = "Red"
}
Function vpnWelcome {
#Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
Write-Host " Welcome to the"-ForegroundColor White -nonewline
Write-Host " Computer Concern " -ForegroundColor Red
Write-Host `n
Write-Host " Client VPN and Remote Desktop Connection Setup tool" -ForegroundColor White
Write-Host `n
Write-Host `n
Write-Host `n
HostUIBW
}
Function vpnName {
HostUIBW
Write-Host " What is the name of the VPN Connection? " -nonewline
HostUIBR
$vName = Read-Host
Write-Host `n
Write-Host `n
Write-Output $vName
}
Function vpnAddress {
HostUIBW
Write-Host " What is the Server Address for the vpn you are setting up? " -nonewline
HostUIBR
$vServer = Read-Host
Write-Host `n
Write-Host `n
Write-Output $vServer
}
Function vpnUsername {
HostUIBW
Write-Host " What is the username for the vpn you are setting up? " -nonewline
HostUIBR
$vUser = Read-Host
Write-Host `n
Write-Host `n
Write-Output $vUser
}
Function vpnPassword {
HostUIBW
Write-Host " What is the password for the user? " -nonewline
HostUIBR
$vPassword = Read-Host
Write-Host `n
Write-Host `n
Write-Output $vPassword
}
Function vpnSecret {
HostUIBW
Write-Host " What is the Secret Key for the VPN Connection? " -nonewline
HostUIBR
$vSecret = Read-Host
Write-Host `n
Write-Host `n
Write-Output $vSecret
}
#Function vpnDone {
#}
HostUIBW
Clear-Host
Do{
vpnWelcome
vpnName
vpnAddress
vpnUsername
vpnPassword
vpnSecret
HostUIBW
$vConfirm = Read-Host -Prompt "Are these settings correct? Y or N"
while ($vConfirm -ne "y") {
Write-Host `n
Write-Host `n
Write-Host `n
Write-Host "Which setting do you need to change? 1, 2, 3, 4 or 5"
Write-Host "1 - Name of VPN"
Write-Host "2 - Server Address"
Write-Host "3 - Username"
Write-Host "4 - Password"
Write-Host "5 - Secret Key"
$vChange = Read-Host
switch ($vChange) {
1 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnName
$vConfirm
}
2 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnAddress
$vConfirm
}
3 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnUsername
$vConfirm
}
4 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnPassword
$vConfirm
}
5 {
Clear-Host
HostUIBW
Write-Host `n
Write-Host `n
Write-Host `n
vpnSecret
$vConfirm
}
}
$vConfirm = Read-Host -Prompt "Are these settings correct? Y or N"
}
} Until ($vConfirm -eq "y")
Write-Host "Creating VPN Connection Now!"
Add-VpnConnection -Name $vName -ServerAddress $vServer -TunnelType "L2tp" -EncryptionLevel "Optional" -AuthenticationMethod Pap -L2tpPsk $vSecret -RememberCredential -Force -Passthru
cls
Write-Host "Please change the encryption level to ~Require Encrption~"
ncpa.cpl
Cls
do {
cls
ncpa.cpl
$vContinue = Read-Host -Prompt "Did you change it? Y or N"
}Until ($vContinue -eq "y")
cls
Write-Host "YAY you changed it"
Start-Sleep -Seconds 800
I learned from my mistakes and was able to correct it. I had to set global variables and call those variables in the functions for them to change in the switch statement.
THANK YOU SO MUCH FOR YOUR HELP!
$Shell = $Host.UI.RawUI
$size = $Shell.WindowSize
$size.width=100
$size.height=40
$Shell.WindowSize = $size
$size = $Shell.BufferSize
$size.width=100
$size.height=40
$Shell.BufferSize = $size
$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
Function 3BlankLines{
Write-Host ""
Write-Host ""
Write-Host ""
}
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
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? 1, 2, 3, 4 or 5"
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")
Write-Host "Creating VPN Connection Now!"
Add-VpnConnection -Name $vName -ServerAddress $vServer -TunnelType "L2tp" -EncryptionLevel "Optional" -AuthenticationMethod Pap -L2tpPsk $vSecret -RememberCredential -Force -Passthru | Out-Null
cls
Write-Host "Please change the encryption level to ~Require Encryption~"
ncpa.cpl
do {
cls
ncpa.cpl
HostUIBW
Write-Host "Did you change it? Y or N " -nonewline
HostUIBR
$vContinue = Read-Host
}Until ($vContinue -eq "y")
cls
Write-Host "YAY you changed it"
Start-Sleep -Seconds 800