PowerShell – the .Net Factor

PowerShell is a command shell and a scripting language. It easily outclasses both VBScript and the DOS-derived command shell available in Windows; but one of the areas that takes a bit of getting used to is the availability of the .Net Framework. This leads to some interesting combinations when trying to solve particular problems.

VNC is a great piece of software. Some of the work I do involves computers where VNC is is used as a remote support tool. In the era of UAC using VNC has its challenges, mainly due to the fact that UAC-elevated programs run in a different session to the session of the current user. User-initiated connections from VNC in application mode to a listening viewer can only interact with processes from that session. UAC prompts don’t appear in the VNC viewer, so remote support staff can’t work in an elevated context. The answer to this is to run VNC as a system service, which has access to all sessions. For various non-technical reasons it’s not acceptable to leave the VNC service running all of the time – the user needs to be seen to request the remote connection. The users don’t have permission to start the VNC service; they can however initiate a connection from an application-mode VNC server to a listening VNC viewer on a support person’s computer.

Finally, here is where PowerShell and .Net come in to the picture. Using .Net we can identify which computer is connected, kill off any VNC server processes using WMI, stop any running service instances, start our own server instance and start a VNC viewer connection to it.

The script to do this is below:

VNC Helper
Detect incoming VNC connection port 5500
Kill instance on remote box
start service

Function Get-ActiveTCPConnections {            
try {            
    $TCPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()            
    $Connections = $TCPProperties.GetActiveTcpConnections()            
    foreach($Connection in $Connections) {            
        if($Connection.LocalEndPoint.AddressFamily -eq "InterNetwork" ) { $IPType = "IPv4" } else { $IPType = "IPv6" }            
        $OutputObj = New-Object -TypeName PSobject            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "LocalAddress" -Value $Connection.LocalEndPoint.Address            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "LocalPort" -Value $Connection.LocalEndPoint.Port            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "RemoteAddress" -Value $Connection.RemoteEndPoint.Address            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "RemotePort" -Value $Connection.RemoteEndPoint.Port            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "State" -Value $Connection.State            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "IPV4Or6" -Value $IPType            
} catch {            
    Write-Error "Failed to get active connections. $_"            

$connections  = Get-ActiveTCPConnections
$vncconnection = $connections | Where-Object { $_.localport -eq '5500' }
$remoteip = $vncconnection.RemoteAddress
$remotename = [System.Net.Dns]::GetHostEntry($remoteip).HostName
$remotename = $remotename.ToUpper()
Get-service -ComputerName $remotename *vnc* | Stop-Service
(Get-WmiObject Win32_Process -ComputerName $remotename | ?{ $_.ProcessName -match "vncserver" })[0].Terminate()
Start-Sleep 5
Get-service -ComputerName $remotename *vnc* | Start-Service
&'C:\Program Files\RealVNC\VNC Viewer\vncviewer.exe' $remotename

Get familiar with the .Net framework if you want to get the best out of PowerShell.

This entry was posted in IT, PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s