Skip to content

Instantly share code, notes, and snippets.

@hpmmatuska
Last active March 17, 2020 16:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpmmatuska/f80ae1678d0c8820a9a3 to your computer and use it in GitHub Desktop.
Save hpmmatuska/f80ae1678d0c8820a9a3 to your computer and use it in GitHub Desktop.
Get active remote connection thru RRAS and RDS Gateway for the specified servers.
function mms-RAFarm {
<#
.Synopsis
Get active remote connection thru RRAS and RDS Gateway for the specified servers.
.Description
This command will query the Win32_TSGatewayConnection class using RPC and root/Microsoft/Windows/RemoteAccess using WinRM
and write summary object to the pipeline.
.Parameter Computername
The name of the computer(s) to query. This parameter has an alias of CN. The Input can be piped.
.Example
PS C:\> mms-RAFarm server1,server2
UserName Server ConnectedFrom RemoteAccessType ConnectedResource IdleTime ConnectionDuration RecievedKB SentKB
-------- ------ ------------- ---------------- ----------------- -------- ------------------ ---------- ------
Domain\user1 server1 88.234.211.153 Vpn 10.10.91.194 04:12:14 3110 20423
Domain\user2 server2 120.210.33.201 RDP 10.10.90.121 00:00:00 03:37:20 6162 16504
Domain\user2 server2 120.210.33.201 Vpn 10.10.91.229 01:16:10 88 5
Formatted results for multiple computers. You can also pipe computer names into this command.
.Notes
Last Updated: February 05, 2015
Version : 1.1
Changelog:
v1.1 - Replaced [PSObject] with [PSCustomObject] to achieve $object column order
.Link
#>
Param(
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[Alias("cn","name")]
[String[]]$ComputerName
)
Write-Verbose "Starting Script"
foreach ($computer in $ComputerName){
Try { #RDP
Write-Verbose "Connecting over RPC to $computer"
[object[]]$wmi_onPC = Get-WmiObject -class "Win32_TSGatewayConnection" -namespace "root\cimv2\TerminalServices" -ComputerName $computer
} # Try $wmi_OnPC RDP
Catch {write-warning "Cannot query RDP GW on the host: $computer"}
if ($wmi_onPC){
Write-Verbose "Building object from WMI received from $computer"
foreach ($wmi in $wmi_onPC){
[PSCustomObject]@{
UserName = $wmi.UserName
Server = $wmi.PSComputerName
ConnectedFrom = $wmi.ClientAddress
RemoteAccessType = $wmi.ProtocolName
IdleTime = (New-TimeSpan -seconds ($wmi.IdleTime).Substring(0,14))
ConnectionDuration = (New-TimeSpan -seconds ($wmi.ConnectionDuration).Substring(0,14))
ConnectedResource = $wmi.ConnectedResource
RecievedKB = $wmi.NumberOfKilobytesReceived
SentKB = $wmi.NumberOfKilobytesSent
} #end $obj
} # foreach $wmi
Write-Verbose "Result builded from wmi from $computer"
}#end IF $wmi for RDP
else {Write-Verbose "There is no connected RDP GW client on $computer"}
Try { #VPN
Write-Verbose "Connecting over WMRemote to $computer"
[object[]]$wmi_onPC = Get-RemoteAccessConnectionStatistics -ComputerName $computer
} # Try $wmi_OnPC VPN
Catch {write-warning "Cannot query VPN on the host: $computer"}
if (($wmi_onPC |measure).count -gt 0) {
Write-Verbose "Building object from query results from $computer"
foreach ($wmi in $wmi_onPC){
[PSCustomObject]@{
UserName = $wmi.UserName
Server = $computer
ConnectedFrom = $wmi.ClientExternalAddress
RemoteAccessType = $wmi.ConnectionType
IdleTime = ""#(New-TimeSpan -seconds ($wmi.IdleTime).Substring(0,14))
ConnectionDuration = (New-TimeSpan -seconds ($wmi.ConnectionDuration))
ConnectedResource = $wmi.ClientIPAddress
RecievedKB = ([Math]::Round($wmi.TotalBytesIn/1KB))
SentKB = ([Math]::Round($wmi.TotalBytesOut/1KB))
} #end $obj
} # foreach $wmi
Write-Verbose "Result builded from query from $computer"
}# else IF $wmi count >1
else {Write-Verbose "There is no connected VPN client on $computer"}
}#foreach $computer
Write-Verbose "Function ends."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment