Skip to content

Commit fa1498d

Browse files
authored
Merge pull request #3 from Walmann/VPN-Password-Fixes
Fix for parameters with Spaces
2 parents 6f16e42 + 31137d6 commit fa1498d

2 files changed

Lines changed: 28 additions & 25 deletions

File tree

Tools/Create New Connection.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ if ($a.ToLower() -eq "e" -or $null -eq $a) {
1212

1313
$New_VPN_Config_Name = "RDP_TO_VPN_$New_VPN_Name"
1414

15-
15+
if ([string]::IsNullOrEmpty($New_RDP_Username)){
16+
$New_RDP_Username = $New_VPN_Username
17+
}
1618

1719
if ($New_VPN_And_RDP_PW_Same.ToLower -eq "y"){
1820
$New_VPN_And_RDP_PW_Same = $true
@@ -41,7 +43,7 @@ if ($a.ToLower() -eq "e" -or $null -eq $a) {
4143

4244
[System.Collections.ArrayList]$Template_Content_mid = @()
4345
if (![string]::IsNullOrEmpty($New_VPN_Name)){
44-
$Template_Content_mid.Add(' VPN_Name = ' + "`"$New_VPN_Name`"" + "`n")
46+
$Template_Content_mid.Add(' VPN_Name = ' + "`"`'$New_VPN_Name`'`"" + "`n")
4547
}
4648
if (![string]::IsNullOrEmpty($New_RDP_Server_IP)){
4749
$Template_Content_mid.Add(' RDP_Server_IP = ' + "`"$New_RDP_Server_IP`"" + "`n")
@@ -59,7 +61,7 @@ if ($a.ToLower() -eq "e" -or $null -eq $a) {
5961
$Template_Content_mid.Add(' VPN_And_RDP_PW_Same = ' + "`"$New_VPN_And_RDP_PW_Same`"" + "`n")
6062
}
6163
if (![string]::IsNullOrEmpty($New_VPN_Config_Name)){
62-
$Template_Content_mid.Add(' VPN_Config_Name = ' + "`"$New_VPN_Config_Name`"" + "`n")
64+
$Template_Content_mid.Add(' VPN_Config_Name = ' + "`"`'$New_VPN_Config_Name`'`"" + "`n")
6365
}
6466
$Template_Content_end = @"
6567

dep/_MainConnect.ps1

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,28 @@ param (
3131

3232
#Debug:
3333
# $VPN_Name = ""
34-
# $RDP_Server_IP = ""
3534
# $VPN_User = ""
3635
# $VPN_And_RDP_PW_Same = ""
3736
# $VPN_Config_Name = ""
37+
# $RDP_Server_IP = ""
3838
#Debug End
3939

4040

4141
$PhoneBookLocation = ".\dep\Phonebooks\$VPN_Config_Name.pbk"
42+
if ($VPN_And_RDP_PW_Same -eq "n"){
43+
$VPN_And_RDP_PW_Same = $false
44+
}
45+
if ($VPN_And_RDP_PW_Same -eq "y"){
46+
$VPN_And_RDP_PW_Same = $true
47+
}
48+
49+
4250
function New-Credidential {
4351

4452
#RDP Passord
45-
$RDPPasswordsec = Read-Host -Prompt "Passord for $VPN_Name (RDP og VPN)" -AsSecureString
53+
$RDPPasswordsec = Read-Host -Prompt "Passord for $VPN_Name (RDP)" -AsSecureString
4654
$RDPPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($RDPPasswordsec))
47-
New-StoredCredential -Target $RDP_Server_IP -UserName $RDP_Username -Password $RDPPassword -Persist 'LocalMachine' -Comment $VPN_Name
48-
49-
# # #VPN Passord
50-
# $VPNPWSameAsRDP = Read-Host -Prompt "Er passordet for VPN det samme som RDP? Y/N"
51-
# if (($VPNPWSameAsRDP.ToLower -eq "n") -or $VPNPWSameAsRDP.ToLower -eq "no") {
52-
53-
# $RDPPasswordsec = Read-Host -Prompt "Passord for $VPN_Name (VPN)" -AsSecureString
54-
# $RDPPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($RDPPasswordsec))
55-
# New-StoredCredential -Target $RDP_Server_IP -UserName $RDP_Username -Password $RDPPassword -Persist 'LocalMachine' -Comment $VPN_Name
56-
# }
55+
New-StoredCredential -Target $RDP_Server_IP -UserName "$RDP_Username" -Password $RDPPassword -Persist 'LocalMachine' -Comment "$VPN_Name"
5756

5857
Return $RDPPassword
5958
}
@@ -96,13 +95,7 @@ if ([string]::IsNullOrEmpty($VPN_User) -or [string]::IsNullOrEmpty($VPNPassword)
9695
Write-Host "Using RDP Username as VPN Username"
9796
}
9897
# if ([string]::IsNullOrEmpty($VPNPassword)) {
99-
if ($New_VPN_And_RDP_PW_Same -eq $true) {
100-
$VPNPassword = Get-StoredCredential -Target $RDP_Server_IP -AsCredentialObject
101-
# $VPNPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($VPNPassword.Password))
102-
$VPNPassword = $VPNPassword.Password
103-
Write-Host "Using RDP Password as VPN Password"
104-
}
105-
if ($New_VPN_And_RDP_PW_Same -eq $false) {
98+
if ($VPN_And_RDP_PW_Same -eq $true) {
10699
$VPNPassword = Get-StoredCredential -Target $RDP_Server_IP -AsCredentialObject
107100
# $VPNPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($VPNPassword.Password))
108101
$VPNPassword = $VPNPassword.Password
@@ -111,9 +104,17 @@ if ([string]::IsNullOrEmpty($VPN_User) -or [string]::IsNullOrEmpty($VPNPassword)
111104
}
112105

113106
# Koble til VPN
114-
Write-Host "Connecting to VPN"
115-
rasdial.exe "$VPN_Name" $VPN_User $VPNPassword "/phonebook:$PhoneBookLocation"
116-
# Write-Host $VPN_Name.ConnectionStatus
107+
Write-Host "Connecting to VPN with stored credentials"
108+
rasdial.exe "$VPN_Name" "$VPN_User" $VPNPassword "/phonebook:$PhoneBookLocation"
109+
110+
111+
112+
if (-Not (Test-Connection -ComputerName $RDP_Server_IP -Count 1 -Quiet)){
113+
[System.Windows.MessageBox]::Show(" Kunne ikke koble til VPN. `nDette betyr enten feil i instillinger, eller ingen lagret passord. `n I neste vindu velger du 'Koble til' deretter fyller du ut innloggings informasjonen.")
114+
rasphone -FilePath $PhoneBookLocation
115+
$RasPid = (Get-Process rasphone).Id
116+
Wait-Process -Id $RasPid
117+
}
117118

118119

119120
#Kobler til RPD

0 commit comments

Comments
 (0)