Advertisement
Guest User

Kill all disconnected DWMs

a guest
Oct 28th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. # QuerySessionInformation.ps1
  2. # Written by Ryan Ries, Jan. 2013, with help from MSDN and Stackoverflow.
  3.  
  4. $Code = @'
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Runtime.InteropServices;
  9. public class RDPInfo
  10. {
  11.    [DllImport("wtsapi32.dll")]
  12.    static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String pServerName);
  13.  
  14.    [DllImport("wtsapi32.dll")]
  15.    static extern void WTSCloseServer(IntPtr hServer);
  16.  
  17.    [DllImport("wtsapi32.dll")]
  18.    static extern Int32 WTSEnumerateSessions(
  19.        IntPtr hServer,
  20.        [MarshalAs(UnmanagedType.U4)] Int32 Reserved,
  21.        [MarshalAs(UnmanagedType.U4)] Int32 Version,
  22.        ref IntPtr ppSessionInfo,
  23.        [MarshalAs(UnmanagedType.U4)] ref Int32 pCount);
  24.  
  25.    [DllImport("wtsapi32.dll")]
  26.    static extern void WTSFreeMemory(IntPtr pMemory);
  27.  
  28.    [DllImport("Wtsapi32.dll")]
  29.    static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
  30.  
  31.    [StructLayout(LayoutKind.Sequential)]
  32.    private struct WTS_SESSION_INFO
  33.    {
  34.        public Int32 SessionID;
  35.        [MarshalAs(UnmanagedType.LPStr)]
  36.        public String pWinStationName;
  37.        public WTS_CONNECTSTATE_CLASS State;
  38.    }
  39.  
  40.    public enum WTS_INFO_CLASS
  41.    {
  42.        WTSInitialProgram,
  43.        WTSApplicationName,
  44.        WTSWorkingDirectory,
  45.        WTSOEMId,
  46.        WTSSessionId,
  47.        WTSUserName,
  48.        WTSWinStationName,
  49.        WTSDomainName,
  50.        WTSConnectState,
  51.        WTSClientBuildNumber,
  52.        WTSClientName,
  53.        WTSClientDirectory,
  54.        WTSClientProductId,
  55.        WTSClientHardwareId,
  56.        WTSClientAddress,
  57.        WTSClientDisplay,
  58.        WTSClientProtocolType
  59.    }
  60.  
  61.    public enum WTS_CONNECTSTATE_CLASS
  62.    {
  63.        WTSActive,
  64.        WTSConnected,
  65.        WTSConnectQuery,
  66.        WTSShadow,
  67.        WTSDisconnected,
  68.        WTSIdle,
  69.        WTSListen,
  70.        WTSReset,
  71.        WTSDown,
  72.        WTSInit
  73.    }
  74.  
  75.    public static IntPtr OpenServer(String Name)
  76.    {
  77.        IntPtr server = WTSOpenServer(Name);
  78.        return server;
  79.    }
  80.  
  81.    public static void CloseServer(IntPtr ServerHandle)
  82.    {
  83.        WTSCloseServer(ServerHandle);
  84.    }
  85.  
  86.    public static void CheckDisconnected(String ServerName)
  87.    {
  88.        IntPtr serverHandle = IntPtr.Zero;
  89.        List<String> resultList = new List<string>();
  90.        serverHandle = OpenServer(ServerName);
  91.  
  92.        try
  93.        {
  94.            IntPtr SessionInfoPtr = IntPtr.Zero;
  95.            IntPtr connectState = IntPtr.Zero;
  96.            Int32 sessionCount = 0;
  97.            Int32 retVal = WTSEnumerateSessions(serverHandle, 0, 1, ref SessionInfoPtr, ref sessionCount);
  98.            Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
  99.            Int32 currentSession = (int)SessionInfoPtr;
  100.            uint bytes = 0;
  101.             bool result = false;
  102.            if (retVal != 0)
  103.            {
  104.                for (int i = 0; i < sessionCount; i++)
  105.                {
  106.                    WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)currentSession, typeof(WTS_SESSION_INFO));
  107.                    currentSession += dataSize;
  108.  
  109.                    WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSConnectState, out connectState, out bytes);
  110.                     if(connectState == WTSDisconnected)
  111.                     {
  112.                         result = true;
  113.                     }
  114.                    WTSFreeMemory(connectState);
  115.                }
  116.                WTSFreeMemory(SessionInfoPtr);
  117.            }
  118.        }
  119.        catch(Exception ex)
  120.        {
  121.            Console.WriteLine("Exception: " + ex.Message);
  122.        }
  123.        finally
  124.        {
  125.            CloseServer(serverHandle);
  126.        }
  127.    }
  128. }
  129. '@
  130.  
  131. Add-Type $Code
  132.  
  133. // THE SCRIPT
  134. $check = [RDPInfo]::CheckDisconnected
  135. if($check) {
  136.     $processList = Get-Process -Name DWM
  137.     foreach($pid in $processList.Id) {
  138.         Stop-Process -Id $pid -Force
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement