写一个脚本 满足如下条件 判断是否有多个硬盘; 如果只有一个物理硬盘则跳过; 如果有多个硬盘; 获取物理硬盘然后,排除系统盘,排除USB设备,排除虚拟磁盘; 删除获取硬盘的分区并创建分区;
时间: 2024-03-30 11:37:04 浏览: 40
好的,以下是一个满足您要求的Windows脚本。请注意,在脚本中,我使用了diskpart命令来获取硬盘信息并进行操作,所以需要以管理员身份运行该脚本。
```batch
@echo off
setlocal enabledelayedexpansion
rem 获取物理硬盘数量
set disk_count=0
for /f "skip=1" %%d in ('"wmic diskdrive get interfacetype"') do (
set /a disk_count+=1
)
rem 如果只有一个物理硬盘则跳过
if %disk_count% equ 1 (
echo "Only one physical disk found, skipping."
exit /b
)
rem 遍历物理硬盘
for /f "skip=1" %%d in ('"wmic diskdrive get deviceid, interfacetype"') do (
set "disk=%%d"
set "interface=%%e"
rem 排除非SATA接口的硬盘
if not "!interface!" == "IDE" (
rem 获取硬盘序号
set "disk_number=!disk:~-1!"
rem 排除系统盘
for /f "skip=1" %%p in ('"wmic diskdrive where (index=%disk_number%) get MediaType"') do (
set "media_type=%%p"
if "!media_type!" == "12" (
echo "Skipping system disk."
exit /b
)
)
rem 排除USB设备和虚拟磁盘
for /f "skip=1" %%p in ('"wmic diskdrive where (index=%disk_number%) get MediaType"') do (
set "media_type=%%p"
if "!media_type!" == "Removable Media" (
echo "Skipping USB device."
exit /b
)
)
for /f "skip=1" %%p in ('"wmic diskdrive where (index=%disk_number%) get MediaType"') do (
set "media_type=%%p"
if "!media_type!" == "Virtual hard disk" (
echo "Skipping virtual disk."
exit /b
)
)
rem 删除分区并创建分区
echo "Deleting partitions on disk !disk_number!."
echo "create partition primary" | diskpart /s
)
)
echo "All disks have been processed."
```
您可以将以上代码保存为一个.bat文件,然后以管理员身份运行该脚本。该脚本将遍历所有非系统盘、非USB设备和非虚拟磁盘的物理硬盘,并删除其分区并创建一个主分区。
阅读全文