@@ -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+
4250function 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. `n Dette 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