SonicWallVpn.ps1
$userFileName = "vpn.usr" $pwdFileName = "vpn.pwd" $nameFileName = "vpn.nme" function Enable-SonicVpn { [CmdletBinding()] Param ( ) Write-Verbose -Message "Connecting to VPN..." $name = Get-VpnName $user = Get-User $pwd = Get-Password & "C:\Program Files\Dell SonicWALL\Global VPN Client\swgvc.exe" /E $name /U $user /P $pwd } function Disable-SonicVpn { [CmdletBinding()] Param ( [string] $name = "VPN" ) Write-Verbose -Message "Disonnecting VPN..." & "C:\Program Files\Dell SonicWALL\Global VPN Client\swgvc.exe" /D $name } function Get-Password { $pwdFilePath = "$(Get-ModulePath)\$pwdFileName" if(Test-Path -Path $pwdFilePath) { $pwdFile = Get-Content -Path $pwdFilePath $secured = $pwdFile | ConvertTo-SecureString } else { $secured = Read-Host -Prompt "Enter password for VPN" -AsSecureString $secured | ConvertFrom-SecureString | Out-File -FilePath $pwdFilePath } $user=[System.Security.Principal.WindowsIdentity]::GetCurrent().Name $creds = new-object -TypeName PSCredential -ArgumentList $user, $secured $pwd = $creds.GetNetworkCredential().Password return $pwd } function Get-User { $usrFilePath = "$(Get-ModulePath)\$userFileName" Write-Verbose -Message $usrFilePath if(Test-Path -Path $usrFilePath) { $usr = Get-Content -Path $usrFilePath } else { $usr = Read-Host -Prompt "Enter user name for VPN" $usr | Out-File -FilePath $usrFilePath } return $usr } function Get-VpnName { $nameFilePath = "$(Get-ModulePath)\$nameFileName" Write-Verbose -Message $nameFilePath if(Test-Path -Path $nameFilePath) { $name = Get-Content -Path $nameFilePath } else { $name = Read-Host -Prompt "Enter name of your VPN profile" $name | Out-File -FilePath $nameFilePath } return $name } function Reset-SonicVpnUser { Remove-Item -Path "$(Get-ModulePath)\$userFileName" } function Reset-SonicVpnPassword { Remove-Item -Path "$(Get-ModulePath)\$pwdFileName" } function Get-ModulePath { Split-Path -Path "$PSScriptRoot\SonicWallVPN\" } New-Alias -Name evpn -Value Enable-SonicVpn Export-ModuleMember -Function Enable-SonicVpn -Alias evpn New-Alias -Name dvpn -Value Disable-SonicVpn Export-ModuleMember -Function Disable-SonicVpn -Alias dvpn Export-ModuleMember -Function Reset-SonicVpnUser Export-ModuleMember -Function Reset-SonicVpnPassword |