Hi I tried the following script
My log file contains:
$UserNotificationStateSource = @'
using System;
using System.Runtime.InteropServices;
namespace QueryUser
{
public class NotificationState
{
// http://msdn.microsoft.com/en-us/library/bb762533(v=vs.85).aspx
public enum UserNotificationState
{
ScreenSaverOrLockedOrFastUserSwitching=1, FullScreenOrPresentationMode=2, RunningDirect3dFullScreen=3, PresentationMode=4, AcceptsNotifications=5, QuietTime=6, WindowsStoreAppRunning=7
}
// Only for Vista or above
[DllImport("shell32.dll")]
static extern int SHQueryUserNotificationState(out UserNotificationState pquns);
public static UserNotificationState GetState()
{
UserNotificationState state;
int returnVal = SHQueryUserNotificationState(out state);
return state;
}
}
}
'@
If (-not ([System.Management.Automation.PSTypeName]'QueryUser.NotificationState').Type) {
Add-Type -TypeDefinition $UserNotificationStateSource -Language CSharp
}
$time = Get-date
$test = [QueryUser.NotificationState]::GetState()
$output = "$($test) - $($time)"
$output |Out-File -FilePath c:\test.log -Append
# Then use an if statement of some sort to determine what to do based on the result you get.
# ScreenSaverOrLockedOrFastUserSwitching, FullScreenOrPresentationMode, RunningDirect3dFullScreen, PresentationMode, AcceptsNotifications, QuietTime, WindowsStoreAppRunning
I schedule it in my Windows 7 task scheduler and run as SYSTEM every 5 minutes.My log file contains:
FullScreenOrPresentationMode - 01/16/2015 17:46:42
FullScreenOrPresentationMode - 01/16/2015 17:49:32
FullScreenOrPresentationMode - 01/16/2015 17:53:22
FullScreenOrPresentationMode - 01/16/2015 17:58:22
FullScreenOrPresentationMode - 01/16/2015 18:03:22
FullScreenOrPresentationMode - 01/16/2015 18:08:22
FullScreenOrPresentationMode - 01/16/2015 18:13:22
FullScreenOrPresentationMode - 01/16/2015 18:18:22
FullScreenOrPresentationMode - 01/16/2015 18:23:56
And it doesn't matter if I am logged on or not or if the screen is locked :-(