Good day!

There is a powershell script that creates and removes the vpn connection from the user.

The script is a simple form with two buttons "Create" and "Delete", as well as a window for displaying service information.

The essence of the question: if you run the script and click create, the connection is created. But if, without closing the form, click delete, the connection is not deleted. If the form is rediscovered, then everything works.

Question: 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" ##########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 = "Соединение уже есть" } else { Add-VpnConnection -Name "ConWork" -ServerAddress "xxx.xxx.xxx.xxx" -TunnelType IKEv2 -EncryptionLevel Required -AuthenticationMethod Eap -SplitTunneling -RememberCredential -AllUserConnection | Out-String $outputBox.Text += ("Соединение создано") $outputBox.Text += "`r`n" $outputBox.Text += "Добавлены маршруты" foreach ($ip in $IpAddress) { $outputBox.Text += Add-VpnConnectionRoute -ConnectionName "ConWork" -DestinationPrefix $ip -PassThru | Out-String } } #[System.Windows.Forms.MessageBox]::Show("Подключение создано") } function RemoveVPN { if ($vpnConnection.Name -eq "ConWork") { $outputBox.Text += ("Удалены маршруты") foreach ($ip in $IpAddress) { $outputBox.Text += Remove-VpnConnectionRoute -ConnectionName "ConWork" -DestinationPrefix $ip -PassThru | Out-String } $outputBox.Text += ("Удалено соединение") $outputBox.Text += Remove-VpnConnection -Name "ConWork" -Force -PassThru -AllUserConnection | Out-String } else { $outputBox.text = "Такого соединения нет" } } ###########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 = "Создать" $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 = "Удалить" $Removetun.Add_Click({RemoveVPN}) $Form.Controls.Add($Removetun) ############################################## end buttons #$Form.Add_Shown({$Form.Activate()}) $Form.ShowDialog() 

    1 answer 1

    Without extreme error messages, it's hard to understand the reason. But I still try to assume that this is due to the fact that the RemoveVPN function is unknown about the $ IpAddress variable, as well as $ vpnConnection. they are in different "space" of the script. I replaced scope with a script for these variables. Here is the code:

     [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" ##########Constants and Variables########## $script:IpAddress = @("172.17.0.0/16", "192.168.197.0/24", "192.168.196.0/24") $script:vpnConnection = Get-VpnConnection -AllUserConnection #########Start functions############ function CreateVPN { if ($script:vpnConnection.Name -eq "ConWork") { $outputBox.Text = "Соединение уже есть" } else { Add-VpnConnection -Name "ConWork" -ServerAddress "xxx.xxx.xxx.xxx" -TunnelType IKEv2 -EncryptionLevel Required -AuthenticationMethod Eap -SplitTunneling -RememberCredential -AllUserConnection | Out-String $outputBox.Text += ("Соединение создано") $outputBox.Text += "`r`n" $outputBox.Text += "Добавлены маршруты" foreach ($ip in $script:IpAddress) { $outputBox.Text += Add-VpnConnectionRoute -ConnectionName "ConWork" -DestinationPrefix $ip -PassThru | Out-String } } #[System.Windows.Forms.MessageBox]::Show("Подключение создано") } function RemoveVPN { if ($script:vpnConnection.Name -eq "ConWork") { $outputBox.Text += ("Удалены маршруты") foreach ($ip in $script:IpAddress) { $outputBox.Text += Remove-VpnConnectionRoute -ConnectionName "ConWork" -DestinationPrefix $ip -PassThru | Out-String } $outputBox.Text += ("Удалено соединение") $outputBox.Text += Remove-VpnConnection -Name "ConWork" -Force -PassThru -AllUserConnection | Out-String } else { $outputBox.text = "Такого соединения нет" } } ###########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 = "Создать" $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 = "Удалить" $Removetun.Add_Click({RemoveVPN}) $Form.Controls.Add($Removetun) ############################################## end buttons #$Form.Add_Shown({$Form.Activate()}) $Form.ShowDialog()