0

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()

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.