Last active
April 12, 2024 06:05
-
-
Save awakecoding/199b55fc3f7539cd72f160844a7c1423 to your computer and use it in GitHub Desktop.
Invoke-VMAutologon.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-VMAutologon | |
{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true,Position=0)] | |
[string] $VMName, | |
[Parameter(Mandatory=$true)] | |
[string] $UserName, | |
[string] $DomainName = ".\", | |
[Parameter(Mandatory=$true)] | |
[string] $Password | |
) | |
if ([string]::IsNullOrEmpty($Password)) { | |
$Credential = Get-Credential -UserName $UserName | |
if ($PSEdition -eq 'Desktop') { | |
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString) | |
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr) | |
} else { | |
$Password = ConvertFrom-SecureString -SecureString $SecureString -AsPlainText | |
} | |
} else { | |
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force | |
$Credential = New-Object System.Management.Automation.PSCredential @($UserName, $SecurePassword) | |
} | |
$VMSession = New-PSSession -VMName $VMName -Credential $Credential | |
Invoke-Command -ScriptBlock { Param($UserName, $DomainName, $Password) | |
$WinlogonRegPath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" | |
New-ItemProperty -Path $WinlogonRegPath -Name AutoAdminLogon -Value 1 -PropertyType DWORD -Force | Out-Null | |
New-ItemProperty -Path $WinlogonRegPath -Name ForceAutoLogon -Value 0 -PropertyType DWORD -Force | Out-Null | |
New-ItemProperty -Path $WinlogonRegPath -Name DefaultUserName -Value $Username -PropertyType String -Force | Out-Null | |
New-ItemProperty -Path $WinlogonRegPath -Name DefaultPassword -Value $Password -PropertyType String -Force | Out-Null | |
# Force Winlogon.exe to restart and check for autologon without a reboot | |
Remove-Item $(Join-Path $WinlogonRegPath "AutoLogonChecked") -Force | |
Get-Process "Winlogon" | Stop-Process -Force | |
} -Session $VMSession -ArgumentList @($UserName, $DomainName, $Password) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment