Reputation: 23
There is a script on powershell, that creates and removes vpn connection from the user. The script is a simple form with two buttons "Create" and "Delete", and the output textbox. If i run a script and click create, the connection is created. But if not closing the form, press delete, the connection is not removed. If i reopen the form, then everything works and connection delete
What could be the problem?
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
#################Main Form#################
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(552,654)
$form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "VPN create"
##########Constants and Variables##########
$IpAddress = @("172.17.0.0/16", "192.168.197.0/24", "192.168.196.0/24")
$vpnConnection = Get-VpnConnection -AllUserConnection
#########Start functions############
function CreateVPN {
if ($vpnConnection.Name -eq "ConWork") {
$outputBox.Text = "connection is already there"
} else {
Add-VpnConnection -Name "ConWork" -ServerAddress "xxx.xxx.xxx.xxx" -TunnelType IKEv2 -EncryptionLevel Required -AuthenticationMethod Eap -SplitTunneling -RememberCredential -AllUserConnection | Out-String
$outputBox.Text += ("Connection created")
$outputBox.Text += "`r`n"
$outputBox.Text += "Routes added"
foreach ($ip in $IpAddress) {
$outputBox.Text += Add-VpnConnectionRoute -ConnectionName "ConWork" -DestinationPrefix $ip -PassThru | Out-String
}
}
}
function RemoveVPN {
if ($vpnConnection.Name -eq "ConWork") {
$outputBox.Text += ("Routes delete")
foreach ($ip in $IpAddress) {
$outputBox.Text += Remove-VpnConnectionRoute -ConnectionName "ConWork" -DestinationPrefix $ip -PassThru | Out-String
}
$outputBox.Text += ("Connection delete")
$outputBox.Text += Remove-VpnConnection -Name "ConWork" -Force -PassThru -AllUserConnection | Out-String
} else {
$outputBox.text = "No such connection"
}
}
###########end functions################
############Start text fields###########
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(206,23)
$outputBox.Size = New-Object System.Drawing.Size(318,578)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$outputBox.font = "lucida console"
$Form.Controls.Add($outputBox)
###############end text fields################
##############Start buttons################
$CreateTun = New-Object System.Windows.Forms.Button
$CreateTun.Location = New-Object System.Drawing.Size(42,23)
$CreateTun.Size = New-Object System.Drawing.Size(89,43)
$CreateTun.Text = "Create"
$CreateTun.Add_Click({CreateVPN})
$Form.Controls.Add($CreateTun)
$Removetun = New-Object System.Windows.Forms.Button
$Removetun.Location = New-Object System.Drawing.Size(42,90)
$Removetun.Size = New-Object System.Drawing.Size(89,43)
$Removetun.Text = "Delete"
$Removetun.Add_Click({RemoveVPN})
$Form.Controls.Add($Removetun)
############################################## end buttons
#$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
Upvotes: 0
Views: 482
Reputation: 13483
Your problem is that you are checking for VPN connection only once, when the script is started:
$vpnConnection = Get-VpnConnection -AllUserConnection
After that you are reusing this variable to in your RemoveVPN
function. It will never find any new connections. To make it work, just move the line from above inside your RemoveVPN
function
Upvotes: 2