Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # QuerySessionInformation.ps1
- # Written by Ryan Ries, Jan. 2013, with help from MSDN and Stackoverflow.
- $Code = @'
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- public class RDPInfo
- {
- [DllImport("wtsapi32.dll")]
- static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String pServerName);
- [DllImport("wtsapi32.dll")]
- static extern void WTSCloseServer(IntPtr hServer);
- [DllImport("wtsapi32.dll")]
- static extern Int32 WTSEnumerateSessions(
- IntPtr hServer,
- [MarshalAs(UnmanagedType.U4)] Int32 Reserved,
- [MarshalAs(UnmanagedType.U4)] Int32 Version,
- ref IntPtr ppSessionInfo,
- [MarshalAs(UnmanagedType.U4)] ref Int32 pCount);
- [DllImport("wtsapi32.dll")]
- static extern void WTSFreeMemory(IntPtr pMemory);
- [DllImport("Wtsapi32.dll")]
- static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
- [StructLayout(LayoutKind.Sequential)]
- private struct WTS_SESSION_INFO
- {
- public Int32 SessionID;
- [MarshalAs(UnmanagedType.LPStr)]
- public String pWinStationName;
- public WTS_CONNECTSTATE_CLASS State;
- }
- public enum WTS_INFO_CLASS
- {
- WTSInitialProgram,
- WTSApplicationName,
- WTSWorkingDirectory,
- WTSOEMId,
- WTSSessionId,
- WTSUserName,
- WTSWinStationName,
- WTSDomainName,
- WTSConnectState,
- WTSClientBuildNumber,
- WTSClientName,
- WTSClientDirectory,
- WTSClientProductId,
- WTSClientHardwareId,
- WTSClientAddress,
- WTSClientDisplay,
- WTSClientProtocolType
- }
- public enum WTS_CONNECTSTATE_CLASS
- {
- WTSActive,
- WTSConnected,
- WTSConnectQuery,
- WTSShadow,
- WTSDisconnected,
- WTSIdle,
- WTSListen,
- WTSReset,
- WTSDown,
- WTSInit
- }
- public static IntPtr OpenServer(String Name)
- {
- IntPtr server = WTSOpenServer(Name);
- return server;
- }
- public static void CloseServer(IntPtr ServerHandle)
- {
- WTSCloseServer(ServerHandle);
- }
- public static void CheckDisconnected(String ServerName)
- {
- IntPtr serverHandle = IntPtr.Zero;
- List<String> resultList = new List<string>();
- serverHandle = OpenServer(ServerName);
- try
- {
- IntPtr SessionInfoPtr = IntPtr.Zero;
- IntPtr connectState = IntPtr.Zero;
- Int32 sessionCount = 0;
- Int32 retVal = WTSEnumerateSessions(serverHandle, 0, 1, ref SessionInfoPtr, ref sessionCount);
- Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
- Int32 currentSession = (int)SessionInfoPtr;
- uint bytes = 0;
- bool result = false;
- if (retVal != 0)
- {
- for (int i = 0; i < sessionCount; i++)
- {
- WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)currentSession, typeof(WTS_SESSION_INFO));
- currentSession += dataSize;
- WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSConnectState, out connectState, out bytes);
- if(connectState == WTSDisconnected)
- {
- result = true;
- }
- WTSFreeMemory(connectState);
- }
- WTSFreeMemory(SessionInfoPtr);
- }
- }
- catch(Exception ex)
- {
- Console.WriteLine("Exception: " + ex.Message);
- }
- finally
- {
- CloseServer(serverHandle);
- }
- }
- }
- '@
- Add-Type $Code
- // THE SCRIPT
- $check = [RDPInfo]::CheckDisconnected
- if($check) {
- $processList = Get-Process -Name DWM
- foreach($pid in $processList.Id) {
- Stop-Process -Id $pid -Force
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement