I want to run a powershell script when i close the lid on a laptop with windows 10, if there isn't an external monitor attached to te pc, it goes sleep. The script works well if i lauch it manually, but i cannot find inside task scheduler an activation event like 'closing the lid'.
This is the powershell script:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
if ( @(Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams | where-object { $_.Active }).Length -eq 1 )
{
#put laptop to sleep if there are no external monitor connected
$PowerState = [System.Windows.Forms.PowerState]::Suspend;
$Force = $true;
$DisableWake = $false;
[System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);
}