ProductivityTools.PSSetLockDisplayTimeout.psm1

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<#
    My Function
#>

function Set-LockDisplayTimeout {
    [cmdletbinding()]
    param(
        [int]$Minutes,
        [switch]$Battery,
        [switch]$PluggedIn
    )
    Write-Verbose "Hello from Set-LockDisplayTimeout"
    
    SetEASPolicy  $Minutes
    SetScreenSaverTimeOut  $Minutes
    #DispableScreenSaver

    AddOptionToPowerOptions
    SetLockDisplayTimeout $Minutes $Battery $PluggedIn

    Write-Verbose "Bye, Bye!"
}

function AddOptionToPowerOptions()
{
    Write-Verbose "Adding options to power options"
    $command='powercfg -attributes SUB_VIDEO 8EC4B3A5-6868-48c2-BE75-4F3044BE88A7 -ATTRIB_HIDE'
    #$command='powercfg -attributes SUB_VIDEO 8EC4B3A5-6868-48c2-BE75-4F3044BE88A7 +ATTRIB_HIDE'
    Write-Verbose "Command which will be executed $command"
    Invoke-Expression -Command $command
    Write-Verbose "Options to power options should be added"
}

function SetLockDisplayTimeout()
{

    param(
        [int]$Minutes,
        [switch]$Battery,
        [switch]$PluggedIn
    )

    Write-Verbose "Set LockDisplayTimeout start"

    $seconds=$Minutes*60;
    $batterycommand="powercfg /SETDCVALUEINDEX SCHEME_CURRENT SUB_VIDEO VIDEOCONLOCK " +$seconds
    $pluggedInCommand="powercfg /SETACVALUEINDEX SCHEME_CURRENT SUB_VIDEO VIDEOCONLOCK "+ $seconds

    if ($Battery.IsPresent)
    {
        Write-Verbose "Battery switch is present"
        Write-Verbose "Invoke expression $batterycommand"
        Invoke-Expression -Command  $batterycommand;
    }

    if ($PluggedIn.IsPresent)
    {
        Write-Verbose "PluggedIn switch is present"
        Write-Verbose "Invoke expression $pluggedInCommand"
        Invoke-Expression -Command $pluggedInCommand;
    }

    if($Battery.IsPresent -eq $false -and $PluggedIn.IsPresent -eq $false)
    {
        Write-Verbose "Battery switch is not present"
        Write-Verbose "Invoke expression $batterycommand"
        Write-Verbose "PluggedIn switch is present"
        Write-Verbose "Invoke expression $pluggedInCommand"

        Invoke-Expression -Command $batterycommand;
        Invoke-Expression -Command $pluggedInCommand;
    }

    Write-Verbose "Set LockDisplayTimeout end"
}

function SetEASPolicy
{

    param(
        [int]$Minutes
    )
    
    $registryPath = "HKLM:\SYSTEM\CurrentControlSet\Control\EAS\Policies"
    $Name = "7"
    $value = $Minutes*60
    if(Test-Path $registryPath)
    {
        Set-ItemProperty -Path $registryPath -Name $name -Value $value 
    }
    else
    {
        New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType DWORD 
    }
}

function GetEASPolicyValue()
{
    $registryPath = "HKLM:\SYSTEM\CurrentControlSet\Control\EAS\Policies"
    $Name = "7"
    Get-ItemProperty -Path $registryPath -Name $name 
}


function DispableScreenSaver
{
    $registryPath = "HKCU:\Software\Policies\Microsoft\Windows\Control Panel\Desktop"
    $Name = "ScreenSaveActive"
    $value = "0"
    if(Test-Path $registryPath)
    {
        Set-ItemProperty -Path $registryPath -Name $name -Value $value 
    }
    else
    {
        New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType REG_SZ 
    }
}

function CreateNode($path)
{
    if(Test-Path $path)
    {
        Write-Host "Node $registryPath exists"
    }
    else
    {
        $parent=Split-Path -Path $path
        CreateNode $parent
        
        $element=Split-Path -Path $path -Leaf
        New-Item -Path $path -Value $element
    }
}


function SetScreenSaverTimeOut
{

    param(
        [int]$Minutes
    )
    
    
    $registryPath = "HKCU:\Software\Policies\Microsoft\Windows\Control Panel\Desktop"
    CreateNode $registryPath
    
    $Name = "ScreenSaveTimeOut"
    $value = $Minutes*60
    if(Test-Path $registryPath)
    {
        Set-ItemProperty -Path $registryPath -Name $name -Value $value 
    }
    else
    {
        New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType REG_SZ 
    }
}

Export-ModuleMember Set-LockDisplayTimeout