AutoLogon.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#Define ASCII Characters
    $Equals = [Char]61
    $Space = [Char]32
    $SingleQuote = [Char]39
    $DoubleQuote = [Char]34
    $NewLine = "`n"

#Load WMI Classes
    $OperatingSystem = Get-WmiObject -Namespace "root\CIMv2" -Class "Win32_OperatingSystem" -Property * | Select *

#Define Functions
    #Set The Value Of A Registry Path
        Function Set-RegistryValue
            {
                [CmdletBinding(SupportsShouldProcess=$True)]
                Param
                    (
                        [Parameter(Mandatory=$True)]
                        [ValidateScript({$_ -like "HK*:*"})]
                        [String]$Path, 
                        
                        [Parameter(Mandatory=$True)]
                        [String]$Name,
                        
                        [Parameter(Mandatory=$True)]
                        $Value,

                        [Parameter(Mandatory=$True)]
                        [ValidateSet("Binary","DWord","ExpandString","MultiString","String","QWord")]
                        [String]$ValueType
                    )
                
                $PSDrive_Name = $Path.Split(":")[0]

                If ($PSDrive_Name -eq "HKCR") {$PSDrive_Root = "HKEY_CLASSES_ROOT"}
                If ($PSDrive_Name -eq "HKCU") {$PSDrive_Root = "HKEY_CURRENT_USER"}
                If ($PSDrive_Name -eq "HKLM") {$PSDrive_Root = "HKEY_LOCAL_MACHINE"}
                If ($PSDrive_Name -eq "HKU") {$PSDrive_Root = "HKEY_USERS"}
                If ($PSDrive_Name -eq "HKCC") {$PSDrive_Root = "HKEY_CURRENT_CONFIG"}
                
                If (!(Get-PSDrive -Name $PSDrive_Name -PSProvider Registry -ErrorAction SilentlyContinue)) {$PSDrive_Create = New-PSDrive -Name $PSDrive_Name -Root $PSDrive_Root -PSProvider Registry}
                
                If (!(Test-Path -Path $Path -ErrorAction SilentlyContinue))
                    {
                        New-Item -Path $Path -Force | Out-Null
                    }

                New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType $ValueType -Force | Out-Null

                If ($? -eq $True)
                    {
                        Write-Verbose -Message "Set-RegistryValue: `"$($Name)`" of type `"$($ValueType)`" with value of `"$($Value)`" in path `"$($Path)`" was successful"
                    }
                Else
                    {
                        Write-Error -Message "Set-RegistryValue: `"$($Name)`" of type `"$($ValueType)`" with value of `"$($Value)`" in path `"$($Path)`" was unsuccessful"
                    }
            }
<#
.Synopsis
This powershell Cmdlet enables AutoLogon the next time the device reboots. Can be configured to only require a logoff. Additionally, a command can executed upon login.
 
-Domain : Provide the domain of the user to be logged in. Default is the local workstation.
-Username : Provide the username that the system will use to login.
-Password : Provide the password for the username provided. Must be of type [System.Security.SecureString].
-LogonCount : Sets the number of times the system would reboot without asking for credentials. Default is 1.
-ForceAutoLogon : Force auto logon without a restart. A logoff would be sufficient.
-AsynchronousRunOnce : Allows the Windows interface to load while running the command specified in the command parameter.
-Command : Provide the command that will be executed after the device is restarted. Example : $($PSHome)\powershell.exe -ExecutionPolicy Bypass -NoLogo -WindowStyle Normal -File `"$($Env:Windir)\Temp\MyPowershellScript.ps1`"
 
.Description
Enables AutoLogon.
 
.Example
 
$SecurePassword = ConvertTo-SecureString -String "" -AsPlainText -Force
Enable-AutoLogon -Username "Administrator" -Password $SecurePassword
 
.Example
 
$SecurePassword = ConvertTo-SecureString -String "" -AsPlainText -Force
Enable-AutoLogon -Username "Administrator" -Password $SecurePassword -LogonCount "3"
 
.EXAMPLE
 
$SecurePassword = ConvertTo-SecureString -String "" -AsPlainText -Force
Enable-AutoLogon -Username "Administrator" -Password $SecurePassword -Command "$($PSHome)\powershell.exe -ExecutionPolicy Unrestricted -NoLogo -WindowStyle Maximized -File `"$($Env:Windir)\Temp\MyPowershellScript.ps1`""
 
.EXAMPLE
 
$SecurePassword = ConvertTo-SecureString -String "" -AsPlainText -Force
Enable-AutoLogon -Username "Administrator" -Password $SecurePassword -AsynchronousRunOnce -Command "$($PSHome)\powershell.exe -ExecutionPolicy Unrestricted -NoLogo -WindowStyle Maximized -File `"$($Env:Windir)\Temp\MyPowershellScript.ps1`""
#>


Function Enable-AutoLogon
    {
        [CmdletBinding()]
            Param
                (
                    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=0)]
                    [Alias("DMN")]
                    [String]$Domain = "$($Env:Computername)",
                    
                    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=1)]
                    [Alias("UN")]
                    [String]$Username = "$($Env:Username)",

                    [Parameter(Mandatory=$True,ValueFromPipeline=$True,Position=2)]
                    [Alias("PW")]
                    [ValidateNotNullOrEmpty()]
                    [System.Security.SecureString]$Password,

                    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=3)]
                    [AllowEmptyString()]
                    [Alias("LC")]
                    [UInt32]$LogonCount = "1",

                    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=4)]
                    [Alias("FAL")]
                    [Switch]$ForceAutoLogon,
                    
                    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=5)]
                    [Alias("ASRO")]
                    [Switch]$AsynchronousRunOnce,
                    
                    [Parameter(Mandatory=$False,ValueFromPipeline=$True,Position=6)]
                    [AllowEmptyString()]
                    [Alias("Script")]
                    [String]$Command = "$($PSHome)\powershell.exe -ExecutionPolicy Unrestricted -NoLogo -NoExit -WindowStyle Maximized"          
                )

            Begin
                {
                    $RegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
                    $RegistryRunOncePath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
                    $RegistryAsynchronousRunOncePath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"
                }
    
            Process
                {
                    Try
                        {
                            [String]$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
                            Set-RegistryValue -Path $RegistryPath -Name "AutoAdminLogon" -Value "1" -ValueType String -Verbose
                            Set-RegistryValue -Path $RegistryPath -Name "DefaultUsername" -Value $Username -ValueType String -Verbose
                            Set-RegistryValue -Path $RegistryPath -Name "DefaultPassword" -Value $UnsecurePassword -ValueType String
                            Set-RegistryValue -Path $RegistryPath -Name "DefaultDomainName" -Value $Domain -ValueType String -Verbose
            
                            If ($PSBoundParameters.ContainsKey("LogonCount"))
                                {
                                    Set-RegistryValue -Path $RegistryPath -Name "AutoLogonCount" -Value $LogonCount -ValueType Dword -Verbose
                                }
                            Else
                                {
                                    Set-RegistryValue -Path $RegistryPath -Name "AutoLogonCount" -Value $LogonCount -ValueType Dword -Verbose
                                }
            
                            If ($PSBoundParameters.ContainsKey("Command"))
                                {
                                    Set-RegistryValue -Path $RegistryRunOncePath -Name "(Default)" -Value $Command -ValueType String -Verbose
                                }
                            Else
                                {
                                    Set-RegistryValue -Path $RegistryRunOncePath -Name "(Default)" -Value "" -ValueType String -Verbose
                                }
                        
                            If ($ForceAutoLogon.IsPresent)
                                {
                                    Set-RegistryValue -Path $RegistryPath -Name "ForceAutoLogon" -Value "1" -ValueType String -Verbose
                                }
                            Else
                                {
                                    Set-RegistryValue -Path $RegistryPath -Name "ForceAutoLogon" -Value "0" -ValueType String -Verbose
                                }
                            
                            If ($AsynchronousRunOnce.IsPresent)
                                {
                                    Set-RegistryValue -Path $RegistryAsynchronousRunOncePath -Name "AsyncRunOnce" -Value "1" -ValueType Dword -Verbose
                                }
                            Else
                                {
                                    Set-RegistryValue -Path $RegistryAsynchronousRunOncePath -Name "AsyncRunOnce" -Value "0" -ValueType Dword -Verbose
                                }    
                        }
                    Catch
                        {
                            Write-Output -InputObject $Error
                        }
                }
    }

<#
.Synopsis
This powershell Cmdlet disables AutoLogon completely.
 
.Description
Disables AutoLogon.
 
.Example
Disable-AutoLogon
#>


Function Disable-AutoLogon
    {
        Begin
            {
                $RegistryPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
                $RegistryRunOncePath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
                $RegistryAsynchronousRunOncePath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"
            }
    
        Process
            {
                Try
                    {
                        Set-RegistryValue -Path $RegistryPath -Name "AutoAdminLogon" -Value "0" -ValueType String -Verbose
                        Set-RegistryValue -Path $RegistryPath -Name "DefaultUsername" -Value "" -ValueType String -Verbose
                        Set-RegistryValue -Path $RegistryPath -Name "DefaultPassword" -Value "" -ValueType String -Verbose
                        Set-RegistryValue -Path $RegistryPath -Name "DefaultDomainName" -Value "" -ValueType String -Verbose
                        Set-RegistryValue -Path $RegistryPath -Name "AutoLogonCount" -Value "" -ValueType Dword -Verbose
                        Set-RegistryValue -Path $RegistryPath -Name "ForceAutoLogon" -Value "0" -ValueType String -Verbose
                        Set-RegistryValue -Path $RegistryRunOncePath -Name "(Default)" -Value "" -ValueType String -Verbose        
                        If ([Version]($OperatingSystem.Version) -lt [Version]"10.0")
                            {
                                Set-RegistryValue -Path $RegistryAsynchronousRunOncePath -Name "AsyncRunOnce" -Value "0" -ValueType Dword -Verbose
                            }
                        ElseIf ([Version]($OperatingSystem.Version) -ge [Version]"10.0")
                            {
                                Set-RegistryValue -Path $RegistryAsynchronousRunOncePath -Name "AsyncRunOnce" -Value "1" -ValueType Dword -Verbose
                            }
                    }
                Catch
                    {
                        Write-Output -InputObject $Error
                    }
            }
    }

#Export Module Functions
    Export-ModuleMember -Function "Enable-Autologon", "Disable-AutoLogon"