
@echo off
setlocal

echo.
if NOT EXIST X:\i386 goto Usage

set _log="c:\sysprep\whssysprep.log"
set _err="c:\sysprep\whssysprep.err"

if /I NOT "%~dp0"=="c:\sysprep\" (
 mkdir c:\sysprep
 echo Sysprep tools are in required to be located in the c:\sysprep location >> %_err%
 goto :err
)

cd c:\sysprep



echo.
echo Removing certs, system ID

sysutil.exe >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

echo.
echo Stopping WHS services

echo    WHSArchiver
net stop WHSArchiver >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    WMConnectCDS
net stop WMConnectCDS >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    DriveExtenderMigrator
net stop DriveExtenderMigrator >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    WHSSetup
net stop WHSSetup >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    WHSBackup
net stop WHSBackup >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    pdl
net stop pdl >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    QSM
net stop QSM >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    upnphost
net stop upnphost >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    WDS
net stop WSearch >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

echo.
echo Configuring WHS services to manual
REM except for transport and setup

echo    WHSArchiver
sc config WHSArchiver start= demand >> %_log% 2>> %_err%
echo    WHSBackup
sc config WHSBackup start= demand >> %_log% 2>> %_err%
echo    pdl
sc config pdl start= demand >> %_log% 2>> %_err%
echo    QSM
sc config QSM start= demand >> %_log% 2>> %_err%
echo    DriveExtenderMigrator
sc config DriveExtenderMigrator start= demand >> %_log% 2>> %_err%
echo    WMConnectCDS
sc config WMConnectCDS start= demand >> %_log% 2>> %_err%
echo    upnphost
sc config upnphost start= demand >> %_log% 2>> %_err%
echo    WDS
sc config WSearch start= demand >> %_log% 2>> %_err%

echo.
echo Deleting QSM registry

echo    Disks
reg delete "HKLM\Software\Microsoft\Windows Home Server\Storage Manager\Disks" /f >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    Folders
reg delete "HKLM\Software\Microsoft\Windows Home Server\Storage Manager\Folders" /f >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    Shares
reg delete "HKLM\Software\Microsoft\Windows Home Server\Storage Manager\Shares" /f >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
echo    Volumes
reg delete "HKLM\Software\Microsoft\Windows Home Server\Storage Manager\Volumes" /f >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

echo.
echo Copying shares content

mkdir c:\shares  >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
xcopy /yes d:\shares c:\shares  >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

echo.
echo Copying folders content

mkdir c:\folders  >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
xcopy /yes d:\folders c:\folders  >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

REM remove archiver, backup and WDS folders
rmdir /s /q c:\folders\{378AE094-F4CA-4D9B-BDA5-649D6F738276}  >> %_log% 2>> %_err%
rmdir /s /q c:\folders\{00008086-058D-4C89-AB57-A7F909A47AB4}  >> %_log% 2>> %_err%
rmdir /s /q c:\folders\{C3C1CD0A-A0FD-48D0-8608-1A9397389B71}  >> %_log% 2>> %_err%

echo.
echo Stop sharing

for /f "tokens=*" %%a in ('dir /b d:\shares') do ( 

echo    %%a
net share "%%a" /delete >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

)

echo.
echo Deleting primary volume

echo select volume D > tempfile
if ERRORLEVEL 1 goto :err
echo delete volume override >> tempfile
if ERRORLEVEL 1 goto :err
diskpart /s tempfile >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err
del tempfile >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

echo.
echo Cleaning up registry
REM no error checking, values may be missing

echo    UPNPID 
reg delete "HKLM\Software\Microsoft\Windows Home Server\Transport" /v UPNPID /f >> %_log% 2>> %_err%

echo    WHS console
reg delete "HKLM\Software\Microsoft\Windows Home Server\HomeServerConsole" /v FirstTime /f >> %_log% 2>> %_err%

echo    OOBE
reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v OOBE /f >> %_log% 2>> %_err%
reg delete "HKLM\Software\Microsoft\Windows Home Server\install" /v OobeInProgress /f >> %_log% 2>> %_err%

echo.
echo Setting OOBE registry

reg add "HKLM\Software\Microsoft\Windows Home Server\install" /v OOBE /t REG_DWORD /d 4 /f >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

echo.
echo Copying X:\i386 to %systemroot%\i386
mkdir %systemroot%\i386
xcopy /E X:\i386 %systemroot%\i386
if ERRORLEVEL 1 goto :err

echo.
echo Updating setup installation directories
reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Setup" /v SourcePath /t REG_SZ /d %systemroot% /f >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Setup" /v ServicePackSourcePath /t REG_SZ /d %systemroot% /f >> %_log% 2>> %_err%
if ERRORLEVEL 1 goto :err

echo.
echo Running sysprep

cd c:\sysprep
if ERRORLEVEL 1 goto :err
sysprep -reseal
if ERRORLEVEL 1 goto :err

endlocal

goto:eof

:err
echo.
echo ERROR %ERRORLEVEL%
echo.
type %_err%

endlocal

exit /b
goto:eof

:usage
echo.
echo Usage:
echo The Windows Home Server Installation DVD must be in the DVD Drive in order to run this script.
goto:eof