Перейти к содержимому

Как сделать задание в планировщики для выключения windows 10 по простою (бездействие)

Задача выключать пк когда бездействие пк сопоставляет 2 часа.

В windows 10 почему то стандартный планировщики не работает по условию простоя пк. Тестировал по разному но пк выключается по истечению 5 минут простоя. Написал скрипт на powershell и завернул все в vbs что бы не было видно окна.

Скрипт выключения пк при простои больше 2 часов

#
# This background job automatically locks your Workstation after a specified amount of
# time. It will come in handy if you cannot access the screensaver settings due to policy
# restriction but want to lock your screen after a idle timeout. Or you could just
# press [Win]+[L] everytime you leave your desk ;) .
#
# start with
# 	powershell.exe -windowstyle hidden -executionpolicy Unrestricted P:\ATH\TO\logoff.ps1
#
# `-windowstyle hidden` will make your PowerShell disappear/run in background
# `-executionpolicy Unrestricted` will enable this PowerShell process to allow non-signed scripts
#

# This is the only setting: How long before locking?
# Alternative Options:
# * -Seconds 10 ( = 10 Seconds)
# * -Minutes 10 ( = 10 Minutes)
# * -Hours 10 ( = 10 Hours)
#
$idle_timeout = New-TimeSpan -Hours 2

# DO NOT CHANGE ANYTHING BELOW THIS LINE
####################################################################################################################################################################

# This snippet is from http://stackoverflow.com/a/15846912
Add-Type @'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PInvoke.Win32 {
    public static class UserInput {
        [DllImport("user32.dll", SetLastError=false)]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
        [StructLayout(LayoutKind.Sequential)]
        private struct LASTINPUTINFO {
            public uint cbSize;
            public int dwTime;
        }
        public static DateTime LastInput {
            get {
                DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);
                DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);
                return lastInput;
            }
        }
        public static TimeSpan IdleTime {
            get {
                return DateTime.UtcNow.Subtract(LastInput);
            }
        }
        public static int LastInputTicks {
            get {
                LASTINPUTINFO lii = new LASTINPUTINFO();
                lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));
                GetLastInputInfo(ref lii);
                return lii.dwTime;
            }
        }
    }
}
'@
#End snippet
# Helper: Is currently locked?
$idle_time = [PInvoke.Win32.UserInput]::IdleTime;
if (($idle_time -gt $idle_timeout) )
{stop-computer}

Скрипт vbs что бы не было видно запуска окна когда запускается задание в планировщики. Запускает powershell скрипт из той же папки.

Set objShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set F = FSO.GetFile(Wscript.ScriptFullName)
path = FSO.GetParentFolderName(F)
objShell.Run(CHR(34) & "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "" -ExecutionPolicy Bypass & ""'"  & path & "\poweroff.ps1'" & CHR(34)), 0, True

Создал задание в планировщики.

Как сделать задание в планировщики для выключения windows 10 по простою (бездействие)

Как сделать задание в планировщики для выключения windows 10 по простою (бездействие)

Полезные ссылки

https://stackoverflow.com/questions/15845508/get-idle-time-of-machine/15846912#15846912

https://social.technet.microsoft.com/Forums/lync/en-US/59a943b3-bbef-4204-a556-5f4f561ff7de/help-with-powershell-shutdown-script?forum=winserverpowershell

https://gist.github.com/wendelb/1c364bb1a36ca5916ca4

http://www.cyberforum.ru/powershell/thread2477120.html

Similar Posts:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *