Further work on win 81 x86 updater. Add Set-AutoAdminLogon function.

jowjDev
Micah R Ledbetter 8 years ago
parent 02d3de7c9d
commit c33977f20e

@ -22,7 +22,7 @@ catch {}
Invoke-ScriptblockAndCatch -scriptBlock {
Write-EventLogWrapper "PostInstall for packer build '$packerBuildName' of type '$packerBuilderType'"
Install-SevenZip
Disable-AutoAdminLogon
Set-AutoAdminLogon -Disable
Enable-RDP
Install-Chocolatey

@ -7,7 +7,7 @@ It downloads the Windows trial ISO that corresponds to the machine that runs it
#>
[cmdletbinding()] param(
$WorkingDirectory
$VagrantSharePath = "C:\Vagrant"
)
import-module $PSScriptRoot\wintriallab-postinstall.psm1
@ -34,25 +34,30 @@ function Get-AdkPath {
$possibleAdkPaths = @("${env:ProgramFiles(x86)}\Windows Kits\8.1","${env:ProgramFiles}\Windows Kits\8.1")
$possibleAdkPaths |% { if (test-path $_) { $adkPath = $_ } }
if (-not $adkPath) { throw "Could not find the Windows Automated Installation Kit" }
write-verbose "Found the WAIK at '$adkPath'"
Write-EventLogWrapper "Found the WAIK at '$adkPath'"
$arch = Get-OSArchitecture
switch ($arch) {
$ArchitectureId.i386 {
$formatted = $pathFormatString -f $adkPath,$arch
if (test-path $formatted) { return $formatted }
}
$ArchitectureId.amd64 {
foreach ($goddammit in @("amd64","x64")) {
$formatted = $pathFormatString -f $adkPath,$goddammit
if (test-path $formatted) { return $formatted }
}
}
default {
throw "Could not determine path for format string '$pathFormatString' for host architecture of '$arch'"
$resolvedPath = $null
if ($arch -match $ArchitectureId.i386) {
$formatted = $pathFormatString -f $adkPath,$arch
if (test-path $formatted) { $resolvedPath = $formatted }
}
elseif ($arch -match $ArchitectureId.amd64) {
foreach ($goddammit in @("amd64","x64")) {
$formatted = $pathFormatString -f $adkPath,$goddammit
if (test-path $formatted) { $resolvedPath = $formatted }
}
}
throw "Could not resolve format string '$pathFormatString' to an existing path"
if ($resolvedPath) {
Write-EventLogWrapper "Resolved ADK path to '$resolvedPath'"
return $resolvedPath
}
else {
$message = "Could not resolve format string '$pathFormatString' to an existing path"
Write-EventLogWrapper $message
throw $message
}
}
<#
@ -103,7 +108,7 @@ function New-WindowsInstallMedia {
$etfsBoot = resolve-path "$existingInstallMediaDir\boot\etfsboot.com" | select -expand Path
$oscdimgCall = '& "{0}" -m -n -b"{1}" "{2}" "{3}"' -f @($oscdImgPath, $etfsBoot, $installMediaTemp, $outputIsoPath)
write-verbose "Calling OSCDIMG: '$oscdimgCall"
Write-EventLogWrapper "Calling OSCDIMG: '$oscdimgCall'"
Invoke-ExpressionAndCheck $oscdimgCall -verbose:$verbose
dismount-diskimage -imagepath $sourceIsoPath
@ -123,7 +128,8 @@ function Apply-WindowsUpdatesToTrialIso {
[Parameter(Mandatory=$True, ParameterSetName="CorrespondingVersion")] [switch] $CorrespondingVersion,
[Parameter(Mandatory=$True, ParameterSetName="SpecifiedVersion")] $TrialIsoInfo,
[Parameter(Mandatory=$True, ParameterSetName="SpecifiedVersion")] $Architecture,
[Parameter(Mandatory=$True, ParameterSetName="SpecifiedVersion")] $WindowsUpdateCacheDir
[Parameter(Mandatory=$True, ParameterSetName="SpecifiedVersion")] $WindowsUpdateCacheDir,
$updatedIsoPath = $null
)
if ($PsCmdlet.ParameterSetName -match "$CorrespondingVersion") {
@ -138,7 +144,9 @@ function Apply-WindowsUpdatesToTrialIso {
$filenameVersionStamp = "$($hostWinVer.Major)-$($hostWinVer.Minor)-$Architecture"
$dateStamp = Get-Date -Format get-date -format yyyy-MM-dd
$pristineIsoPath = "$WorkingDirectory\Windows-$filenameVersionStamp-Pristine.iso"
$updatedIsoPath = "$WorkingDirectory\Windows-$filenameVersionStamp-Updated-$dateStamp.iso"
if (-not $updatedIsoPath) {
$updatedIsoPath = "$WorkingDirectory\Windows-$filenameVersionStamp-Updated-$dateStamp.iso"
}
$installWimFilePath = "$WorkingDirectory\install.wim"
$installWimMountPath = "$WorkingDirectory\mnt"
$nwimTemp = "$WorkingDirectory\NewWindowsInstallMediaTemp"
@ -156,13 +164,16 @@ function Apply-WindowsUpdatesToTrialIso {
Add-WindowsPackage -PackagePath $WindowsUpdateCacheDir -Path $wimMountSubdir
}
catch {
write-verbose "Caught error(s) when installing packages:`n`n$_`n"
Write-EventLogWrapper "Caught error(s) when installing packages:`n`n$_`n"
}
Dismount-WindowsImage -Path $wimMountSubdir -Save
}
New-WindowsInstallMedia -SourceIsoPath $pristineIsoPath -InstallMediaTemp $nwimTemp -InstallWimPath $installWimFilePath -OutputIsoPath $updatedIsoPath
Get-Item $updatedIsoPath
}
throw "Need to install oscdimg.exe and the DISM Powershell module somehow...?"
Apply-WindowsUpdatesToTrialIso -CorrespondingVersion
$iso = Apply-WindowsUpdatesToTrialIso -CorrespondingVersion
mv $iso "$VagrantSharePath\"
Stop-Computer

@ -133,8 +133,8 @@ TODO: this sucks but I can't think of anything better to do
#>
function Get-WindowsTrialISO {
[cmdletbinding()] param(
$WindowsVersion = [Environment]::OSVersion.Version.
$WindowsArchitecture = Get-OSArchitecture
$WindowsVersion = ([Environment]::OSVersion.Version),
$WindowsArchitecture = (Get-OSArchitecture)
)
if ($WindowsVersion.Major -eq 6 -and $WindowsVersion.Minor -eq 3) {
return $URLs.WindowsIsoDownload.w81.$WindowsArchitecture
@ -165,6 +165,7 @@ function Write-EventLogWrapper {
$messagePlus = "$message`r`n`r`nScript: $($script:ScriptPath)`r`nUser: ${env:USERDOMAIN}\${env:USERNAME}"
if ($messagePlus.length -gt 32766) { $messagePlus = $messagePlus.SubString(0,32766) } # Because Write-EventLog will die otherwise
Write-Host -foreground magenta "====Writing to $EvengLogName event log===="
Write-Host -foreground darkgray (get-date -Format "yyyy-MM-dd HH:mm:ss") # The event log tracks the date, but writing to host never shows it
write-host -foreground darkgray "$messagePlus`r`n"
Write-EventLog -LogName $eventLogName -Source $EventLogSource -EventID $eventId -EntryType $entryType -Message $MessagePlus
}
@ -378,9 +379,29 @@ function Install-VBoxAdditions {
}
}
function Disable-AutoAdminLogon {
Write-EventLogWrapper "Disabling auto admin logon"
set-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -Value 0
function Set-AutoAdminLogon {
[CmdletBinding(DefaultParameterSetName="Enable")] param(
[Parameter(Mandatory=$true,ParameterSetName="Enable")] [String] $Username,
[Parameter(Mandatory=$true,ParameterSetName="Enable")] [String] $Password,
[Parameter(Mandatory=$true,ParameterSetName="Disable")] [Switch] $Disable
)
if ($PsCmdlet.ParameterSetName -Match "Disable") {
Write-EventLogWrapper "Disabling auto admin logon"
$AutoAdminLogon = 0
$Username = ""
$Password = ""
}
elseif ($PsCmdlet.ParameterSetName -Match "Enable") {
Write-EventLogWrapper "Enabling auto admin logon for user '$Username'"
$AutoAdminLogon = 1
}
else {
throw "Invalid parameter set name"
}
$winLogonKey = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty -Path $winLogonKey -Name "AutoAdminLogon" -Value $AutoAdminLogon
Set-ItemProperty -Path $winLogonKey -Name "DefaultUserName" -Value $Username
Set-ItemProperty -Path $winLogonKey -Name "DefaultPassword" -Value $Password
}
function Enable-RDP {

@ -37,8 +37,9 @@ Vagrant.configure("2") do |config|
end
config.vm.provision "file", source: "#{commonScriptsDir}/wintriallab-postinstall.psm1", destination: "marionettist/wintriallab-postinstall.psm1"
config.vm.provision "file", source: "#{commonScriptsDir}/isoupdater-postinstall.ps1", destination: "marionettist/isoupdater-postinstall.ps1"
config.vm.provision "file", source: "#{commonScriptsDir}/trial-iso-updater.ps1", destination: "marionettist/trial-iso-updater.ps1"
config.vm.provision "file", source: "#{commonScriptsDir}/win-updates.ps1", destination: "marionettist/win-updates.ps1"
config.vm.provision "file", source: "isoupdater-postinstall.ps1", destination: "marionettist/isoupdater-postinstall.ps1"
config.vm.provision "shell", inline: "C:/marionettist/win-updates.ps1 -PostUpdateExpression C:/marionettist/isoupdater-postinstall.ps1"
config.vm.provision "shell", inline: 'powershell.exe -File "${ENV:UserProfile}/marionettist/isoupdater-postinstall.ps1"'
end

@ -0,0 +1,23 @@
<#
.synopsis
Postinstall script for my iso updater Vagrant boxes
.notes
- Enable current user autologon
- Install the Windows ADK
- Add the win-updates.ps1 task to run 1 minute from now. That task should call trial-iso-updater.ps1 when finished.
- Exit
TODO: can I pull the Vagrant username/password from the environment somehow? Sucks to have it hardcoded
#>
import-module $PSScriptRoot\wintriallab-postinstall.psm1
Install-Chocolatey
choco install --yes --force windows-adk
$restartCommandString = '& "{0}\win-updates.ps1" -MaxCycles 5 -PostUpdateExpression "{0}\trial-iso-updater.ps1"' -f $PSScriptRoot
Set-RestartScheduledTask -RestartCommand [ScriptBlock]::Create($restartCommandString)
Set-AutoAdminLogon -Username "vagrant" -Password "V@grant123"
# This returns immediately, which means Vagrant's provisioner will hopefully not interpret the restart as a failure?
shutdown.exe /r /f /t 10 /d u:0:0 /c "Reboot to run win-updates.ps1"
Loading…
Cancel
Save