Skip to content

Commit e2cf7c4

Browse files
authored
Merge pull request #2 from Walmann/Error-on-Spaces-in-names
Error in spaces in names.
2 parents 6e24cfa + 04163e9 commit e2cf7c4

2 files changed

Lines changed: 66 additions & 25 deletions

File tree

Tools/Create New Connection.ps1

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,28 @@ if ($a.ToLower() -eq "e" -or $null -eq $a) {
88
$New_RDP_Server_IP = Read-Host -Prompt "Lokal-IP til server (RDP)"
99
$New_RDP_Server_Port = Read-Host -Prompt "RDP Port (La være blank for standard RDP port)"
1010
$New_RDP_Username = Read-Host -Prompt "Brukernavn for RDP (La være blank viss lik VPN bruker)"
11+
$New_VPN_And_RDP_PW_Same= Read-Host -Prompt "Er VPN og RDP Passord det samme? (Y/n)"
1112

13+
$New_VPN_Config_Name = "RDP_TO_VPN_$New_VPN_Name"
14+
15+
16+
17+
if ($New_VPN_And_RDP_PW_Same.ToLower -eq "y"){
18+
$New_VPN_And_RDP_PW_Same = $true
19+
}
20+
if ($New_VPN_And_RDP_PW_Same.ToLower -eq "n"){
21+
$New_VPN_And_RDP_PW_Same = $false
22+
}
1223

1324
#Lag PBK filen, inneholder VPN instillinger
14-
$Path_Phonebook = ($New_VPN_Name + ".pbk")
25+
$Path_Phonebook = ($New_VPN_Config_Name + ".pbk")
1526
New-Item -Path $Path_Phonebook -ItemType File
1627
$Path_Full_Phonebook = Get-ChildItem -Path "./" -Filter $Path_Phonebook
1728
$Path_Full_Phonebook = $Path_Full_Phonebook.FullName
1829
# Write-Host $Path_Full_Phonebook.FullName
1930
# $abcd = $env:SystemRoot + "\System32\rasdial.exe"
20-
Start-Process -FilePath "$env:SystemRoot\System32\rasphone.exe" -ArgumentList "-f $Path_Phonebook","-a $New_VPN_Name" -Wait
21-
Start-Process -FilePath "$env:SystemRoot\System32\rasphone.exe" -ArgumentList "-f $Path_Phonebook","-e $New_VPN_Name" -Wait
31+
Start-Process -FilePath "$env:SystemRoot\System32\rasphone.exe" -ArgumentList "-f `"$Path_Phonebook`"","-a `"$New_VPN_Name`"" -Wait
32+
Start-Process -FilePath "$env:SystemRoot\System32\rasphone.exe" -ArgumentList "-f `"$Path_Phonebook`"","-e `"$New_VPN_Name`"" -Wait
2233

2334
#Lag en kopi av template og bytt ut informasjon
2435
New-Item -Path "." -Name ($New_VPN_Name + ".ps1") -ItemType "file"
@@ -30,21 +41,26 @@ if ($a.ToLower() -eq "e" -or $null -eq $a) {
3041

3142
[System.Collections.ArrayList]$Template_Content_mid = @()
3243
if (![string]::IsNullOrEmpty($New_VPN_Name)){
33-
$Template_Content_mid.Add(' VPN_Name = ' + "`"$New_VPN_Name`"" + "`n")
44+
$Template_Content_mid.Add(' VPN_Name = ' + "`"$New_VPN_Name`"" + "`n")
3445
}
3546
if (![string]::IsNullOrEmpty($New_RDP_Server_IP)){
36-
$Template_Content_mid.Add(' RDP_Server_IP = ' + "`"$New_RDP_Server_IP`"" + "`n")
47+
$Template_Content_mid.Add(' RDP_Server_IP = ' + "`"$New_RDP_Server_IP`"" + "`n")
3748
}
3849
if (![string]::IsNullOrEmpty($New_RDP_Server_Port)){
39-
$Template_Content_mid.Add(' RDP_Port = ' + "`"$New_RDP_Server_Port`"" + "`n")
50+
$Template_Content_mid.Add(' RDP_Port = ' + "`"$New_RDP_Server_Port`"" + "`n")
4051
}
4152
if (![string]::IsNullOrEmpty($New_RDP_Username)){
42-
$Template_Content_mid.Add(' RDP_Username = ' + "`"$New_RDP_Username`"" + "`n")
53+
$Template_Content_mid.Add(' RDP_Username = ' + "`"$New_RDP_Username`"" + "`n")
4354
}
4455
if (![string]::IsNullOrEmpty($New_VPN_Username)){
45-
$Template_Content_mid.Add(' VPN_User = ' + "`"$New_VPN_Username`"" + "`n")
56+
$Template_Content_mid.Add(' VPN_User = ' + "`"$New_VPN_Username`"" + "`n")
57+
}
58+
if (![string]::IsNullOrEmpty($New_VPN_And_RDP_PW_Same)){
59+
$Template_Content_mid.Add(' VPN_And_RDP_PW_Same = ' + "`"$New_VPN_And_RDP_PW_Same`"" + "`n")
60+
}
61+
if (![string]::IsNullOrEmpty($New_VPN_Config_Name)){
62+
$Template_Content_mid.Add(' VPN_Config_Name = ' + "`"$New_VPN_Config_Name`"" + "`n")
4663
}
47-
4864
$Template_Content_end = @"
4965
5066
}
@@ -64,7 +80,7 @@ Powershell.exe -executionpolicy Bypass -Command ".\dep\_MainConnect.ps1" @Argume
6480

6581
#Copy files to right folders
6682
Move-Item -Path $Path_Connect_Template -Destination "../$Path_Connect_Template"
67-
Move-Item -Path $Path_Phonebook -Destination "../dep/Phonebooks/$Path_Phonebook"
83+
Move-Item -Path $Path_Full_Phonebook -Destination "../dep/Phonebooks/$Path_Phonebook"
6884

6985

7086
}

dep/_MainConnect.ps1

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,40 @@ param (
1919
[string]$VPN_Name,
2020

2121
[Parameter()]
22-
[string]$VPN_User
22+
[string]$VPN_User,
23+
24+
[Parameter()]
25+
[string]$VPN_And_RDP_PW_Same,
26+
27+
[Parameter()]
28+
[string]$VPN_Config_Name
2329
)
24-
$PhoneBookLocation = ".\dep\Phonebooks\$VPN_Name.pbk"
2530

2631

2732
#Debug:
28-
# $RDP_Address = ""
29-
# $RDP_Username = ""
30-
# $RDP_Port = "" #Viss denne er tom brukes standard port
31-
# $RDP_Server_IP = "" #Må være IP addresse
32-
# $VPN_Name = "" #Navnet til VPN tilkoblingen i .dep\Phonebook.pbk
33-
# $VPN_User = "" #Viss denne er tom brukes RDP_Username
33+
$VPN_Name = "Nett-Opp IT"
34+
$RDP_Server_IP = "10.0.100.35"
35+
$VPN_User = "nett-opp"
36+
$VPN_And_RDP_PW_Same = "y"
37+
$VPN_Config_Name = "RDP_TO_VPN_Nett-Opp IT"
38+
#Debug End
3439

3540

41+
$PhoneBookLocation = ".\dep\Phonebooks\$VPN_Config_Name.pbk"
3642
function New-Credidential {
3743

3844
#RDP Passord
3945
$RDPPasswordsec = Read-Host -Prompt "Passord for $VPN_Name (RDP og VPN)" -AsSecureString
4046
$RDPPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($RDPPasswordsec))
4147
New-StoredCredential -Target $RDP_Server_IP -UserName $RDP_Username -Password $RDPPassword -Persist 'LocalMachine' -Comment $VPN_Name
4248

43-
# #VPN Passord
49+
# # #VPN Passord
4450
# $VPNPWSameAsRDP = Read-Host -Prompt "Er passordet for VPN det samme som RDP? Y/N"
45-
# if (($VPNPWSameAsRDP -eq "Y") -or $VPNPWSameAsRDP -eq "yes") {
46-
# $VPNPassword = $RDPPassword
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
4756
# }
4857

4958
Return $RDPPassword
@@ -54,11 +63,21 @@ function New-Credidential {
5463
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
5564

5665
#Installerer CredidentialManager
66+
# https://www.powershellgallery.com/packages/CredentialManager/2.0
67+
# https://github.com/davotronic5000/PowerShell_Credential_Manager
5768
if (-Not (Get-Module -ListAvailable -Name CredentialManager)) {
5869
Write-Host "CredidentialManager er ikke installert. Installerer (Dette kan ta noen sekunder) "
5970
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
6071
Install-Module -Name CredentialManager
6172
}
73+
#Installerer VPNCredentialsHelper
74+
# https://www.powershellgallery.com/packages/VPNCredentialsHelper
75+
# https://github.com/paulstancer/VPNCredentialsHelper
76+
if (-Not (Get-Module -ListAvailable -Name VPNCredentialsHelper)) {
77+
Write-Host "VPNCredentialsHelper er ikke installert. Installerer (Dette kan ta noen sekunder) "
78+
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
79+
Install-Module -Name VPNCredentialsHelper
80+
}
6281

6382

6483
#Lager en ny credential i Windows Legitimasjonsenter, viss den ikke finnes fra før.
@@ -76,17 +95,24 @@ if ([string]::IsNullOrEmpty($VPN_User) -or [string]::IsNullOrEmpty($VPNPassword)
7695
$VPN_User = $RDP_Username
7796
Write-Host "Using RDP Username as VPN Username"
7897
}
79-
if ([string]::IsNullOrEmpty($VPNPassword)) {
98+
# if ([string]::IsNullOrEmpty($VPNPassword)) {
99+
if ($New_VPN_And_RDP_PW_Same -eq $true) {
80100
$VPNPassword = Get-StoredCredential -Target $RDP_Server_IP -AsCredentialObject
81101
# $VPNPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($VPNPassword.Password))
82102
$VPNPassword = $VPNPassword.Password
83103
Write-Host "Using RDP Password as VPN Password"
84104
}
105+
if ($New_VPN_And_RDP_PW_Same -eq $false) {
106+
$VPNPassword = Get-StoredCredential -Target $RDP_Server_IP -AsCredentialObject
107+
# $VPNPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($VPNPassword.Password))
108+
$VPNPassword = $VPNPassword.Passwordras
109+
Write-Host "Using RDP Password as VPN Password"
110+
}
85111
}
86112

87113
# Koble til VPN
88114
Write-Host "Connecting to VPN"
89-
rasdial.exe $VPN_Name $VPN_User $VPNPassword "/phonebook:$PhoneBookLocation"
115+
rasdial.exe "$VPN_Name" $VPN_User $VPNPassword "/phonebook:$PhoneBookLocation"
90116
# Write-Host $VPN_Name.ConnectionStatus
91117

92118

@@ -96,6 +122,5 @@ $RDPid = (Get-Process mstsc).Id
96122
Wait-Process -Id $RDPid
97123

98124
Write-Host "RDP Avsluttet. Avslutter og rydder opp."
99-
rasdial.exe $VPN_Name /DISCONNECT
125+
rasdial.exe "$VPN_Name" /DISCONNECT
100126

101-
# Remove-StoredCredential -Target $RDPConnection

0 commit comments

Comments
 (0)