0

I am imaging a set of machines using Windows PE and a flash drive. I have created several scripts to automate the process, but I can't get it to start up on boot. I have placed it in the C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp folder so that no matter who logs in it will run. The image has been prepped to log in as administrator when it boots right after the image installs completely.

I tried putting it in the C:\Users\administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder and it doesn't run either on startup. I can click on them manually, and they will run, but they don't automatically.

Here are the scripts below:

The script below is the main one that will launch and start off the others when the registry value has been met.

@echo off

title Computer Setup

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

echo Computer Setup Command
echo Version 3.0
echo Date Created 7-7-17
echo Author: Harley Frank
echo ====================

ECHO Checking Registry for AutoRun Value

setlocal ENABLEEXTENSIONS
set KEY_NAME="HKLM\Software\Microsoft\Windows NT\CurrentVersion\Run"
set VALUE_NAME=AutoRun

FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set ValueName=%%A
    set ValueType=%%B
    set ValueValue=%%C
)

if defined ValueName (
    @echo Value Name = %ValueName%
    @echo Value Type = %ValueType%
    @echo Value Value = %ValueValue%
    set regValue=%ValueValue%
) else (
    @echo %KEY_NAME%\%VALUE_NAME% not found.
    @echo Creating registry value now to begin the setup. This will take only but a moment.
    ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Run" /v AutoRun /t REG_SZ /d 1
)

Echo Now running the setup scripts. Please wait.


if %regValue% EQU 1 (

echo Initial Restart Script
echo Version 2
echo Date Created 7-7-17
echo Author: Harley Frank
echo ====================

echo Restarting the Machine for the First Time.

ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 1
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d administrator
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d omitted
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoLogonCount /t REG_DWORD /d 1

ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Run" /v AutoRun /t REG_SZ /d 2

shutdown /r /c "Restarting the computer to apply changes." /t 60 /f

) else if %regValue% EQU 2 (

call "C:\Source Files\Scripts\rename.bat"

ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Run" /v AutoRun /t REG_SZ /d 3

exit

) else if %regValue% EQU 3 (

call "C:\Source Files\Scripts\domain.bat"

ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Run" /v AutoRun /t REG_SZ /d 4

exit
) else if %regValue% EQU 4 (
echo running silent software install

"C:\Source Files\SCCM2012Client\ccmsetup.exe"

ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Run" /v AutoRun /t REG_SZ /d 5

msg "%username%" The machine setup should be completed now. SCCM should have successfully launched and is running in the background.
) else if %regValue% EQU 5 (
    rem del "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\run.bat"
    echo DONE
    exit
)

Here is computer rename script:

@ECHO OFF

title Computer Rename

SETLOCAL ENABLEDELAYEDEXPANSION

ECHO Setting Machine to Login as Administrator on next reboot.

ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 1
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d administrator
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d omitted
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoLogonCount /t REG_DWORD /d 1

echo Rename Script
echo Version 3.0
echo Date Created 7-11-17
echo Author: Harley Frank
echo ====================

SET count=1
SET /p campus= Please enter the abbreviation name of the campus you are at for this computer to be properly named: 

FOR /F "tokens=* USEBACKQ" %%F IN (`wmic bios get serialnumber`) DO ( 
SET serialnumber!count!=%%F
SET /a count=!count!+1
)

SET pcname=%campus%-%serialnumber2%

::
SET pcname=%pcname:~0,15%
SET pcname=%pcname: =%
::

ECHO Automated Rename in Progress. Renaming to:
ECHO %pcname%

WMIC computersystem where name="%computername%" call rename name="%pcname%"

shutdown /r /c "Restarting to apply computer name changes." /t 60 /f

Here is the domain batch file and PowerShell file:

@ECHO OFF

tile Domain Setup Script

echo Domain Setup Script
echo Version 4.0
echo Date Created 6-29-17
echo Author: Harley Frank
echo ====================

ECHO Prepping the machine to sign into the default domain account on restart.

ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 1
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultDomainName /t REG_SZ /d omitted
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d adddomain
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d omitted
ECHO y | reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoLogonCount /t REG_DWORD /d 2

ECHO Adding the machine to the domain. Will restart when complete.

CALL PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Source Files\Scripts\domain.ps1""' -Verb RunAs}"

ECHO Restarting machine now.

shutdown /r /c "Restarting to add the machine to the domain." /t 60 /f

Write-Output 'Domain Setup Script'
Write-Output 'Version 4.0'
Write-Output 'Date Created 6-29-17'
Write-Output 'Author: Harley Frank'
Write-Output '===================='

Write-Output 'Adding machine to the domain using adddomain user.'

$domain = "omitted"
$password = "omitted" | ConvertTo-SecureString -asPlainText -Force
$username = "$domain\adddomain" 
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential

Write-Output 'Restarting the computer to apply changes.'

The last thing it does is to log into the general domain account and install SCCM.

I tested the scripts separately, and they work without issue, they just won't autostart. Is there a way I could get this to work on start up?


UPDATE:

I restarted the machine several times. At least 3 and then the script decided to run. I placed the script into the C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp folder.

  • "The last thing it does is to log into the general domain account and install SCCM." This does not answer your question, but one can't help but wonder why you're not using SCCM or MDT to do these installations. – Patrick Seymour Jul 12 '17 at 18:15
  • Long story short, the Microsoft PXE server has crashed beyond repair. Instead of rebuilding it as it would cost too much, they would like to switch to something like ghost. However, through a ton of research I found that its easier through USB and PE, but still need SCCM to be installed. I was trying to do it efficiently for when we re-image mass machines at a time. – Harley Frank Jul 12 '17 at 22:25
  • Fair enough, and I won't beat this to death since it's not answering your question, but you can build a deployment with MDT (Microsoft Deployment Toolkit), and put that deployment on a USB drive. I only mention that because you wouldn't need to do all of this custom scripting yourself. – Patrick Seymour Jul 13 '17 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.