##### Script for starting and Stopping OpenVPN-Service as NON-Admin User ##### ##### (C) Henrik Noack 2016 ##### Rechnerbetriebsgruppe der Fakultäten Mathematik und Informatik ##### Technische Universität München ##### V 1.0 22.11.2016 $global:authfile="C:\Program Files\OpenVPN\config\auth.txt" $ServiceName ="OpenVPNService" $OpenVPNService=get-Service $Servicename #if (-not $OpenVPNService) {throw "$ServiceName not installed properly"} ### shows the Dialog window function ShowSetUserForm () { function Connect(){ echo $Username.Text $Password3.Text | Out-File -FilePath $authfile -Encoding ascii $OpenVPNService.start() start-job -ArgumentList ($authfile) {sleep 5;"">$args[0]} #if (-not ($OpenVPNService.status -match "Running")){Stop-Process $(Get-Process "OpenVPN")} } function Disconnect(){ $OpenVPNService.stop() } function UpdateServiceStatus(){ $OpenVPNService=get-Service $Servicename if (-not $OpenVPNService) {$label4.Text = "Service not installed properly";return} if ($OpenVPNService.status -match "Running"){$OKButton.Enabled=$false;$DisconnectButton.Enabled= $true} if ($OpenVPNService.status -match "Stopped"){$OKButton.Enabled=$true;$DisconnectButton.Enabled= $false} $label4.Text = $(Get-NetAdapter | where InterfaceDescription -match "TAP"|%{"$($_.InterfaceDescription) ... $($_.status)`r`n"}) } [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.ComponentModel") $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Start VPN Connection" $objForm.Size = New-Object System.Drawing.Size(300,300) $objForm.StartPosition = "CenterScreen" $objForm.WindowState = "Normal" $objForm.SizeGripStyle = "Hide" $objForm.FormBorderStyle = "Fixed3d" $label1 = New-Object System.Windows.Forms.Label $label1.Location = New-Object System.Drawing.Size(50,150) $label1.Size = New-Object System.Drawing.Size(150,20) $label1.AutoSize = $true $label1.Text = "Status:" $label2 = New-Object System.Windows.Forms.Label $label2.Location = New-Object System.Drawing.Size(50,80) $label2.Size = New-Object System.Drawing.Size(150,20) $label2.AutoSize = $true $label2.Text = "Enter User Password:" $label3 = New-Object System.Windows.Forms.Label $label3.Location = New-Object System.Drawing.Size(50,20) $label3.Size = New-Object System.Drawing.Size(150,20) $label3.AutoSize = $true $label3.Text = "Enter Username:" $label4 = New-Object System.Windows.Forms.Label $label4.Location = New-Object System.Drawing.Size(10,170) $label4.Size = New-Object System.Drawing.Size(150,50) $label4.AutoSize = $true $label4.TextAlign =[System.Drawing.ContentAlignment]::TopLeft $label4.Text = "" $Password3 = New-Object System.Windows.Forms.TextBox $Password3.Location = New-Object System.Drawing.Size(50,100) $Password3.Size = New-Object System.Drawing.Size(150,40) $Password3.AcceptsReturn = $true $Password3.AcceptsTab = $false $Password3.Multiline = $false $Password3.Text = "" $Password3.UseSystemPasswordChar = $True $Username = New-Object System.Windows.Forms.TextBox $Username.Location = New-Object System.Drawing.Size(50,40) $Username.Size = New-Object System.Drawing.Size(150,40) $Username.AcceptsReturn = $true $Username.AcceptsTab = $false $Username.Multiline = $false $Username.Text = $env:USERNAME $Username.UseSystemPasswordChar = $false $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(50,220) $OKButton.Size = New-Object System.Drawing.Size(80,23) $OKButton.Text = "Connect" $OKButton.Enabled=$false $OKButton.Add_Click({connect}) $DisconnectButton = New-Object System.Windows.Forms.Button $DisconnectButton.Location = New-Object System.Drawing.Size(150,220) $DisconnectButton.Size = New-Object System.Drawing.Size(80,23) $DisconnectButton.Text = "Disconnect" $DisconnectButton.Enabled= $false $DisconnectButton.Add_Click({disconnect}) $objForm.Controls.Add($Username) $objForm.Controls.Add($Password3) $objForm.Controls.Add($OKButton) $objForm.Controls.Add($AutoLogin) $objForm.Controls.Add($label1) $objForm.Controls.Add($label2) $objForm.Controls.Add($label3) $objForm.Controls.Add($label4) $objForm.Controls.Add($OKButton) $objForm.Controls.Add($DisconnectButton) $timer=New-Object System.Windows.Forms.Timer $timer.Interval = 1000 $timer.add_tick({UpdateServiceStatus}) $timer.Enabled=$true $timer.Start() #UpdateServiceStatus [void] $objForm.ShowDialog() } ShowSetUserForm