1

I have 2 scripts for imaging new PCs. 1 for if the PC has a CD rom and one for if it does not:

With CD Rom:

    select disk 0
    clean
    create partition primary
    select volume 1
    assign letter="C"
    format quick fs=ntfs
    active
    exit

Without CD Rom:

select disk 0
clean
create partition primary
select volume 0
assign letter="C"
format quick fs=ntfs
active
exit

I'm looking for a way to combine these into one script that will basically decide if volume 0 is a hard drive or cd rom, then execute one or the other scripts above based on a YES hdd or YES cd-rom.

I'm not sure if this is better done with Powershell or if an If statement can be working into a Diskpart Script or if all of this can be done with a simple CMD batch file.

Jay
  • 33
  • 5
  • You might be interested in the `wmic` command; for instance: `wmic VOLUME GET DriveLetter,DriveType` – aschipfl Mar 29 '16 at 18:20
  • I'd recommend PowerShell. `diskpart` accepts input from STDIN, so you can define the `diskpart` script as a multiline string or a string array and pipe it into the command. Use `Get-WmiObject` for checking whether a CD/DVD drive is present or not. – Ansgar Wiechers Mar 29 '16 at 19:00

2 Answers2

1

I don't know what a "Diskpart Script" looks like, but you can choose a filename for output by modifying the scriptname variable at the top of this script which will look for "CD-ROM" in your disk info (wmic command like @aschipfl mentioned) and output one or the other blocks of commands that you referenced above. It could also run that command for you at the end.

@ECHO OFF
set scriptname=yourscript.scr

REM Look for "CD-ROM" in disk information
wmic logicaldisk |findstr "CD-ROM" 1>nul
if ERRORLEVEL 1 (
REM If we did not find "CD-ROM", set volnum to 0
echo "no cd rom"
set volnum=0
) else (
REM If we found "CD-ROM", set volnum to 1
echo "has a cd rom"
set volnum=1
)

REM Write the script
 >"%scriptname%" echo.select disk 0
>>"%scriptname%" echo.clean
>>"%scriptname%" echo.create partition primary
>>"%scriptname%" echo.select volume %volnum%
>>"%scriptname%" echo.assign letter="C"
>>"%scriptname%" echo.format quick fs=ntfs
>>"%scriptname%" echo.active
>>"%scriptname%" echo.exit
REM run it by removing the 'REM' from the next line:
REM "%scriptname%"
LinuxDisciple
  • 2,289
  • 16
  • 19
1

create partition automatically selects created partition and corresponded volume. So that, select volume is not necessary at all. Just remove it.

user4003407
  • 21,204
  • 4
  • 50
  • 60