-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Get_Focus_Assist_Status.ps1
74 lines (64 loc) · 1.93 KB
/
Get_Focus_Assist_Status.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Get Focus Assist Status via PowerShell
# https://redplait.blogspot.com/2018/07/wnf-ids-from-perfntcdll-adk-version.html
# https://github.com/DCourtel/Windows_10_Focus_Assist/blob/master/FocusAssistLibrary/FocusAssistLib.cs
$Focus = @{
Namespace = "WinAPI"
Name = "Focus"
Language = "CSharp"
MemberDefinition = @"
[DllImport("NtDll.dll", SetLastError = true)]
private static extern uint NtQueryWnfStateData(IntPtr pStateName, IntPtr pTypeId, IntPtr pExplicitScope, out uint nChangeStamp, out IntPtr pBuffer, ref uint nBufferSize);
[StructLayout(LayoutKind.Sequential)]
public struct WNF_TYPE_ID
{
public Guid TypeId;
}
[StructLayout(LayoutKind.Sequential)]
public struct WNF_STATE_NAME
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public uint[] Data;
public WNF_STATE_NAME(uint Data1, uint Data2) : this()
{
uint[] newData = new uint[2];
newData[0] = Data1;
newData[1] = Data2;
Data = newData;
}
}
public enum FocusAssistState
{
NOT_SUPPORTED = -2,
FAILED = -1,
OFF = 0,
PRIORITY_ONLY = 1,
ALARMS_ONLY = 2
};
// Returns the state of Focus Assist if available on this computer
public static FocusAssistState GetFocusAssistState()
{
try
{
WNF_STATE_NAME WNF_SHEL_QUIETHOURS_ACTIVE_PROFILE_CHANGED = new WNF_STATE_NAME(0xA3BF1C75, 0xD83063E);
uint nBufferSize = (uint)Marshal.SizeOf(typeof(IntPtr));
IntPtr pStateName = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WNF_STATE_NAME)));
Marshal.StructureToPtr(WNF_SHEL_QUIETHOURS_ACTIVE_PROFILE_CHANGED, pStateName, false);
uint nChangeStamp = 0;
IntPtr pBuffer = IntPtr.Zero;
bool success = NtQueryWnfStateData(pStateName, IntPtr.Zero, IntPtr.Zero, out nChangeStamp, out pBuffer, ref nBufferSize) == 0;
Marshal.FreeHGlobal(pStateName);
if (success)
{
return (FocusAssistState)pBuffer;
}
}
catch { }
return FocusAssistState.FAILED;
}
"@
}
if (-not ("WinAPI.Focus" -as [type]))
{
Add-Type @Focus
}
[WinAPI.Focus]::GetFocusAssistState()