virus1988
Goto Top

PowerShell VPN Monitoring

Guten Abend an die ganze Profis da draußen,

ich habe ein kleines anliegen. Es geht darum ein kleines Monitoring Tool für die den RAS-Server.

Ich würde mir wünschen das man die abfrage die man ins cmd/Powershell machen kann in einer kleinen GUI einbetten lassen könnte.

In Powershell hat man ja die Möglichkeit" netsh -> ras -> show client " abzufragen. Nun habe ich leider so gar keine bis wenig Ahnung von PS.

Ein Traum wäre es wenn man das Ergebnis dieser abfrage in einer GUI einfließen lassen könnte und sich das ganze zur Krönung auch noch alle 2-3 min Aktualisiert.


Ich habe mich schon durch ein paar Seiten gelesen und kann echt jeden nur beneiden der diesen bereich drauf hat.

Vielen Dank im Voraus

Content-ID: 252324

Url: https://administrator.de/contentid/252324

Ausgedruckt am: 22.11.2024 um 14:11 Uhr

colinardo
colinardo 19.10.2014 aktualisiert um 16:59:11 Uhr
Goto Top
Hallo Virus1988, Willkommen auf Administrator.de!
Ich habe dir mal schnell zwei Varianten gebaut. Die eine ist universell und ließt die Daten aus den Ausgaben von NETSH aus, und die zweite Variante welche aber erst ab SERVER2012/WIN8 funktioniert, nutzt das CMDLet Get-RemoteAccessConnectionStatistics welches mehr Informationen zur Verbindung liefert (Eine Auswahl der wichtigsten habe ich übernommen). Zusätzlich kannst du einen Client mit einem Button ins Jenseits befördern face-wink

Wichtige Info: Die NETSH Variante habe ich hier nur an eine deutsche Ausgabe des Befehls angepasst, hatte die Englische gerade nicht zur Verfügung, falls sie einer postet trage ich die Anpassung nach.

back-to-topVorschau NETSH GUI

f01cf0d25a290edd6726793de48b6aeb
back-to-topVorschau SERVER2012/WIN8.x GUI

9f2071f4cffc123c4a79ca8db7607d5f

Falls du noch an einer anderen GUI interessiert bist die Offene Netzwerksessions und Dateihandles anzeigt seist du an meinen anderen Post verwiesen:
Powershell - GUI für offene Netzwerk-Sessions (net session) und offene Dateihandles (net file)

back-to-top[CODE] Variante mit Analyse der Ausgabe von NETSH RAS SHOW CLIENT
$showWindowAsync = Add-Type –memberDefinition @” 
[DllImport("user32.dll")]   
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 
“@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru

function Hide-PowerShell() { 
    [void]$showWindowAsync::ShowWindowAsync((Get-Process –id $pid).MainWindowHandle, 2) 
}

function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$label3 = New-Object System.Windows.Forms.Label
$lblRefresh = New-Object System.Windows.Forms.Label
$txtRefresh = New-Object System.Windows.Forms.NumericUpDown
$btn_disconnect_all_session = New-Object System.Windows.Forms.Button
$btn_disconnect_session = New-Object System.Windows.Forms.Button
$dg_sessions = New-Object System.Windows.Forms.DataGridView
$label1 = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$timer = New-Object System.Windows.Forms.Timer
#endregion Generated Form Objects

$refresh_sessions = {
    # get open network sessions
    $sessions = @()
  
    $pinfo = New-Object System.Diagnostics.ProcessStartInfo
    $pinfo.FileName = "netsh"  
    $pinfo.Arguments = "ras show client"  
    $pinfo.UseShellExecute = $false
    $pinfo.CreateNoWindow = $true
    $pinfo.RedirectStandardOutput = $true
    $pinfo.RedirectStandardError = $true
    $pinfo.StandardOutputEncoding = [System.Text.Encoding]::GetEncoding(850)

    $process = New-Object System.Diagnostics.Process
    $process.StartInfo = $pinfo
    $process.Start() | out-null
    $raw = $process.StandardOutput.ReadToEnd()
    $process.Dispose()

    $langID = [System.Globalization.CultureInfo]::CurrentUICulture.LCID
    switch($langID){
        1031{$matches = ([regex]'(?ms)^Benutzer:\s*([^\r\n]*)\s*^Domäne:\s*([^\r\n]*).*?^Verbindung von:\s*([^\r\n]*).*?^Dauer:\s*([^\r\n]*)').Matches($raw)}  
    }
    $matches | %{
        $sessions += new-Object PSObject -Property @{'Benutzer'=$_.Groups[1];'Domäne'=$_.Groups[2];'Verbindung von'=$_.Groups[3];'Dauer'=$_.Groups[4]}  
    }
    $sessions = $sessions | select Benutzer,Domäne,'Verbindung von',Dauer | sort Benutzer  
    $arrList = New-Object System.Collections.ArrayList
    if ($sessions.Length -gt 1){
        $arrList.AddRange($sessions)
    }else{
        $arrList.Add($sessions)
    }
    $dg_sessions.DataSource = $arrList
}

#----------------------------------------------
#Event Script Blocks
#----------------------------------------------

$handler_txtRefresh_ValueChanged= 
{
    $timer.Interval = $txtRefresh.Value * 1000
}

$handler_form1_Load= 
{
    &$refresh_sessions
    $timer.Interval = $txtRefresh.Value * 1000
    $timer.Start()
}

$handler_btn_disconnect_session_Click= 
{
    if ($dg_sessions.SelectedRows.Count -gt 0 ){
        $username = $dg_sessions.Rows[$dg_sessions.SelectedRows[0].Index].Cells['Benutzer'].Value  
        start-process  "netsh" -ArgumentList "ras set client name=$username state=disconnect" -Wait -WindowStyle Hidden  
        [System.Windows.Forms.MessageBox]::Show("Der VPN-Client '$username' wurde getrennt!")  
        &$refresh_sessions
    }
}

$handler_timer_tick = {
    &$refresh_sessions
}
$handler_form_closing = {
    $timer.stop()
    $timer.Dispose()
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 281
$System_Drawing_Size.Width = 471
$form1.ClientSize = $System_Drawing_Size
$form1.MinimumSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"  
$form1.Text = "VPN Sitzungen"  
$form1.add_Load($handler_form1_Load)
$form1.add_Closing($handler_form_closing)

$label3.Anchor = 9
$label3.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 437
$System_Drawing_Point.Y = 11
$label3.Location = $System_Drawing_Point
$label3.Name = "label3"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 18
$System_Drawing_Size.Width = 33
$label3.Size = $System_Drawing_Size
$label3.TabIndex = 11
$label3.Text = "Sek."  

$form1.Controls.Add($label3)

$lblRefresh.Anchor = 9
$lblRefresh.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 270
$System_Drawing_Point.Y = 11
$lblRefresh.Location = $System_Drawing_Point
$lblRefresh.Name = "lblRefresh"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 101
$lblRefresh.Size = $System_Drawing_Size
$lblRefresh.TabIndex = 10
$lblRefresh.Text = "Aktualisierung alle"  

$form1.Controls.Add($lblRefresh)

$txtRefresh.Anchor = 9
$txtRefresh.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 377
$System_Drawing_Point.Y = 9
$txtRefresh.Location = $System_Drawing_Point
$txtRefresh.Maximum = 3600
$txtRefresh.Name = "txtRefresh"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 54
$txtRefresh.Size = $System_Drawing_Size
$txtRefresh.TabIndex = 9
$txtRefresh.TextAlign = 2
$txtRefresh.Value = 5
$txtRefresh.add_ValueChanged($handler_txtRefresh_ValueChanged)

$form1.Controls.Add($txtRefresh)


$btn_disconnect_session.Anchor = 10
$btn_disconnect_session.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 339
$System_Drawing_Point.Y = 242
$btn_disconnect_session.Location = $System_Drawing_Point
$btn_disconnect_session.Name = "btn_disconnect_session"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 121
$btn_disconnect_session.Size = $System_Drawing_Size
$btn_disconnect_session.TabIndex = 4
$btn_disconnect_session.Text = "Auswahl trennen"  
$btn_disconnect_session.UseVisualStyleBackColor = $True
$btn_disconnect_session.add_Click($handler_btn_disconnect_session_Click)

$form1.Controls.Add($btn_disconnect_session)

$dg_sessions.AllowUserToAddRows = $False
$dg_sessions.AllowUserToDeleteRows = $False
$dg_sessions.AllowUserToResizeRows = $False
$dg_sessions.Anchor = 15
$dg_sessions.AutoSizeColumnsMode = 16
$dg_sessions.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 35
$dg_sessions.Location = $System_Drawing_Point
$dg_sessions.MultiSelect = $False
$dg_sessions.Name = "dg_sessions"  
$dg_sessions.ReadOnly = $True
$dg_sessions.RowHeadersVisible = $False
$dg_sessions.RowHeadersWidth = 20
$dg_sessions.SelectionMode = 1
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 202
$System_Drawing_Size.Width = 447
$dg_sessions.Size = $System_Drawing_Size
$dg_sessions.TabIndex = 0

$form1.Controls.Add($dg_sessions)

$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,1,3,1)  

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 9
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 199
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 1
$label1.Text = "Aktive VPN Sitzungen"  

$form1.Controls.Add($label1)

$timer.Enabled = $false
$timer.add_Tick($handler_timer_tick)
#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function

Hide-Powershell
GenerateForm
back-to-top[CODE] Variante mit "Get-RemoteAccessConnectionStatistics" (ab Server 2012 / Windows 8)
$showWindowAsync = Add-Type –memberDefinition @” 
[DllImport("user32.dll")]   
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 
“@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru

function Hide-PowerShell() { 
    [void]$showWindowAsync::ShowWindowAsync((Get-Process –id $pid).MainWindowHandle, 2) 
}

function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$label3 = New-Object System.Windows.Forms.Label
$lblRefresh = New-Object System.Windows.Forms.Label
$txtRefresh = New-Object System.Windows.Forms.NumericUpDown
$btn_disconnect_all_session = New-Object System.Windows.Forms.Button
$btn_disconnect_session = New-Object System.Windows.Forms.Button
$dg_sessions = New-Object System.Windows.Forms.DataGridView
$label1 = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$timer = New-Object System.Windows.Forms.Timer

#endregion Generated Form Objects

$refresh_sessions = {
    # get open network sessions
    $sessions = Get-RemoteAccessConnectionStatistics | select @{n="Username";e={$_.Username}},ClientExternalAddress,ClientIPv4Address,TunnelType,ConnectionStarttime,@{n='Connection-Duration';e={[timespan]::FromSeconds($_.ConnectionDuration).ToString('c')}}  
    $arrList = New-Object System.Collections.ArrayList
    if ($sessions.Length -gt 1){
        $arrList.AddRange($sessions)
    }else{
        $arrList.Add($sessions)
    }
    $dg_sessions.DataSource = $arrList

}


#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------

$handler_txtRefresh_ValueChanged= 
{
    $timer.Interval = $txtRefresh.Value * 1000
}

$handler_form1_Load= 
{
    &$refresh_sessions
    $timer.Interval = $txtRefresh.Value * 1000
    $timer.Start()
}

$handler_btn_disconnect_session_Click= 
{
  if ($dg_sessions.SelectedRows.Count -gt 0 ){
        $username = $dg_sessions.Rows[$dg_sessions.SelectedRows[0].Index].Cells['Username'].Value  
        Disconnect-VPNUser -UserName $username
        [System.Windows.Forms.MessageBox]::Show("Der VPN-Client '$username' wurde getrennt!")  
        .$refresh_sessions
  }
}

$handler_timer_tick = {
    &$refresh_sessions
}
$handler_form_closing = {
    $timer.stop()
    $timer.Dispose()
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 281
$System_Drawing_Size.Width = 471
$form1.ClientSize = $System_Drawing_Size
$form1.MinimumSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"  
$form1.Text = "VPN Sitzungen"  
$form1.add_Load($handler_form1_Load)
$form1.add_Closing($handler_form_closing)

$label3.Anchor = 9
$label3.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 437
$System_Drawing_Point.Y = 11
$label3.Location = $System_Drawing_Point
$label3.Name = "label3"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 18
$System_Drawing_Size.Width = 33
$label3.Size = $System_Drawing_Size
$label3.TabIndex = 11
$label3.Text = "Sek."  

$form1.Controls.Add($label3)

$lblRefresh.Anchor = 9
$lblRefresh.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 270
$System_Drawing_Point.Y = 11
$lblRefresh.Location = $System_Drawing_Point
$lblRefresh.Name = "lblRefresh"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 101
$lblRefresh.Size = $System_Drawing_Size
$lblRefresh.TabIndex = 10
$lblRefresh.Text = "Aktualisierung alle"  

$form1.Controls.Add($lblRefresh)

$txtRefresh.Anchor = 9
$txtRefresh.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 377
$System_Drawing_Point.Y = 9
$txtRefresh.Location = $System_Drawing_Point
$txtRefresh.Maximum = 3600
$txtRefresh.Name = "txtRefresh"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 54
$txtRefresh.Size = $System_Drawing_Size
$txtRefresh.TabIndex = 9
$txtRefresh.TextAlign = 2
$txtRefresh.Value = 5
$txtRefresh.add_ValueChanged($handler_txtRefresh_ValueChanged)

$form1.Controls.Add($txtRefresh)


$btn_disconnect_session.Anchor = 10
$btn_disconnect_session.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 339
$System_Drawing_Point.Y = 242
$btn_disconnect_session.Location = $System_Drawing_Point
$btn_disconnect_session.Name = "btn_disconnect_session"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 121
$btn_disconnect_session.Size = $System_Drawing_Size
$btn_disconnect_session.TabIndex = 4
$btn_disconnect_session.Text = "Auswahl trennen"  
$btn_disconnect_session.UseVisualStyleBackColor = $True
$btn_disconnect_session.add_Click($handler_btn_disconnect_session_Click)

$form1.Controls.Add($btn_disconnect_session)

$dg_sessions.AllowUserToAddRows = $False
$dg_sessions.AllowUserToDeleteRows = $False
$dg_sessions.AllowUserToResizeRows = $False
$dg_sessions.Anchor = 15
$dg_sessions.AutoSizeColumnsMode = 16
$dg_sessions.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 35
$dg_sessions.Location = $System_Drawing_Point
$dg_sessions.MultiSelect = $False
$dg_sessions.Name = "dg_sessions"  
$dg_sessions.ReadOnly = $True
$dg_sessions.RowHeadersVisible = $False
$dg_sessions.RowHeadersWidth = 20
$dg_sessions.SelectionMode = 1
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 202
$System_Drawing_Size.Width = 447
$dg_sessions.Size = $System_Drawing_Size
$dg_sessions.TabIndex = 0

$form1.Controls.Add($dg_sessions)

$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,1,3,1)  

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 9
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 199
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 1
$label1.Text = "Aktive VPN Sitzungen"  

$form1.Controls.Add($label1)

$timer.Enabled = $false
$timer.add_Tick($handler_timer_tick)
#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function

# Powershell Fenster verstecken
Hide-Powershell
#Call the Function
GenerateForm
Viel Spaß
Grüße Uwe
Virus1988
Virus1988 19.10.2014 um 17:20:40 Uhr
Goto Top
Wie geil ist das denn ? Vielen Dank genau das was ich meinte face-smile