0

I run sysprep with an answer file, which runs a batch script in the specialize configuration pass. The batch script gets the target HDD index, and runs diskpart against that HDD.

All files are located in C:\temp:

C:\temp\unattend.xml  
C:\temp\testdisk.bat  
C:\temp\1.txt  
C:\temp\2.txt

I run sysprep using an unattend answer like this:

sysprep /generalize /oobe /reboot unattend:c:\temp\unattend.xml

The unattend.xml looks lihe this:

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="specialize">
        <component name="Microsoft-Windows-Deployment" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <RunAsynchronous>
                <RunAsynchronousCommand wcm:action="add">
                    <Order>1</Order>
                    <Path>C:\Temp\testdisk.bat</Path>
                    <Description>test</Description>
                </RunAsynchronousCommand>
            </RunAsynchronous>
        </component>
    </settings>
    <cpi:offlineImage cpi:source="wim:E:\installwin10pro.wim#Windows 10 Pro" xmlns:cpi="urn:schemas-microsoft-com:cpi" />
</unattend>

The testdisk.bat looks like this:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

md c:\temp\testdir1

set "params=%*"

cd /d "%~dp0" && ( if exist "c:\temp\getadmin.vbs" del "c:\temp\getadmin.vbs" ) && fsutil dirty query C: 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/c cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> c:\temp\getadmin.vbs" && "c:\temp\getadmin.vbs" && exit /B )

Set "Index="
For /F "Tokens=2 Delims==" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
 DiskDrive Where "Model Like 'ST%%' And Size > 300000000000" Get Index
 /Value 2^>NUL') Do For %%H In (%%G) Do Set "Index=%%H"
If Not Defined Index GoTo :EOF

Echo Your Disk Index is %Index%

md c:\temp\testdir2

echo select disk %index% > 1.txt

type 2.txt >> 1.txt

diskpart /s c:\temp\1.txt

Pause

C:\temp\1.txt is empty for holding the index of the HDD.

C:\temp\2.txt looks like below:

clean
convert gpt
create partition primary size = 1000
format quick fs=ntfs
assign
create partition primary size = 1000
format quick fs=ntfs
assign

Finally, when getting into the OS, I checked, the batch script created testdir1 and testdir2, contents of 2.txt is attached to the end of 1.txt which looks like below:

select disk 1
clean
convert gpt
create partition primary size = 1000
format quick fs=ntfs
assign
create partition primary size = 1000
format quick fs=ntfs
assign

But diskpart does not appear to have been run.

I tried to run diskpart /s 1.txt after the installation, and it works well, but why did it not work in the unattend setup process?

Could someone point out where the problem is?

Compo
  • 36,585
  • 5
  • 27
  • 39
Jacky
  • 3
  • 3
  • Line 14 of your answer file has a typo, `e:/` should be ```E:\```. I would also change `cmd /c C:\Temp\testdisk.bat` to `C:\Temp\testdisk.bat` – Compo Apr 06 '23 at 08:42
  • @Compo the 'e:/' is auto generated by system image manager, anyway the testdisk.bat seems to be work fine. as i know, getadmin.vbs elevate the permission of testdisk.bat, but diskpart command doesn't appear to be running in that elevated mode, even if i delete "cmd /c" – Jacky Apr 06 '23 at 09:28
  • There is still no reason not to correct them! I would also change all instances of `%temp%` to `C:\temp`, and `%systemdrive%` to `C:`. – Compo Apr 06 '23 at 09:52
  • @Compo i have fixed them as you suggested – Jacky Apr 06 '23 at 10:05
  • @Compo i need the diskpart run in sysprep, if it works, i will run sysprep /shutdown instead of /reboot and capture the image. when i deploy the image to another pc, it will boot and partition the hdd automatically – Jacky Apr 06 '23 at 10:33
  • @Compo accroding to [this post](https://msfn.org/board/topic/158334-run-diskpart-before-install/) , i try to change the to, it actually worked. But I don't know the specific difference between these two commands – Jacky Apr 06 '23 at 11:28

0 Answers0