Powershell脚本无法正常运行

时间:2016-10-25 07:22:18

标签: powershell

powershell上有一个脚本,用于创建和删除用户的vpn连接。 该脚本是一个简单的表单,有两个按钮“Create”和“Delete”,以及输出文本框。 如果我运行脚本并单击“创建”,则会创建连接。但是如果没有关闭表单,请按删除,不会删除连接。如果我重新打开表单,那么一切正常,连接删除

可能是什么问题?

[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 个答案:

答案 0 :(得分:2)

您的问题是,在脚本启动时,您只检查一次VPN连接:

$vpnConnection = Get-VpnConnection -AllUserConnection

之后,您将在RemoveVPN函数中重复使用此变量。它永远不会找到任何新的连接。要使其正常工作,只需在RemoveVPN函数

中从上方移动线条即可