20200630--Powershell实现单、双存储分区
用powershell简单实现检查磁盘类型,分区。适用于单机械硬盘,单固态硬盘,一个机械硬盘加一个固态硬盘,双固态硬盘。
代码如下
1 #get all disks info, except udisk 2 $Disks = Get-PhysicalDisk | Where-Object {$_.BusType -notlike "USB" -and $_.BusType -notlike "*Virtual*"} 3 $SSD_Results = Get-PhysicalDisk | Where-Object {$_.BusType -notlike "USB" -and $_.BusType -notlike "*Virtual*" -and $_.MediaType -like "SSD"} 4 $SSD_Count = Get-PhysicalDisk | Where-Object {$_.BusType -notlike "USB" -and $_.BusType -notlike "*Virtual*" -and $_.MediaType -like "SSD"}| Measure-Object 5 $HDD_Count = Get-PhysicalDisk | Where-Object {$_.BusType -notlike "USB" -and $_.BusType -notlike "*Virtual*" -and $_.MediaType -like "HDD"}| Measure-Object 6 7 $SSD0_HDD1 = " /S SSD0_HDD1.scp" 8 $SSD1_HDD0 = " /S SSD1_HDD0.scp" 9 $SSD = " /S SSD.scp" 10 $HDD = " /S HDD.scp" 11 12 switch ( $SSD_Count.Count ) 13 { 14 "0" 15 { # 0个SSD 1个HDD 16 Start-Process -FilePath DISKPART.exe -ArgumentList $HDD -NoNewWindow -wait -PassThru 17 echo there-is-no-ssd-but-only-one-hdd 18 } 19 "1" 20 { 21 if ( $HDD_Count.Count -eq "1") # 1个SSD 1个HDD 22 { 23 if ($Disks[0].DeviceID -eq "1" ) #结果显示顺序与实际Number顺序相反. 24 { 25 if ($Disks[0].MediaType -like "SSD" ) #第0条记录在diskpart中是disk 1 26 { 27 #第2个硬盘是SSD 28 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD1_HDD0 -NoNewWindow -wait -PassThru 29 echo ssd-is-disk-1-and-hdd-is-disk-0 30 } 31 else #第1条记录在diskpart中是disk 0 32 { 33 #第1个硬盘是SSD 34 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD0_HDD1 -NoNewWindow -wait -PassThru 35 echo ssd-is-disk-0-and-hdd-is-disk-1 36 } 37 } 38 else #结果显示顺序与实际Number顺序一致. 39 { 40 if ($Disks[0].MediaType -like "SSD" ) 41 { 42 #第1个硬盘是SSD 43 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD0_HDD1 -NoNewWindow -wait -PassThru 44 echo ssd-is-disk-0-and-hdd-is-disk-1 45 } 46 else 47 { 48 #第2个硬盘是SSD 49 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD1_HDD0 -NoNewWindow -wait -PassThru 50 echo ssd-is-disk-1-and-hdd-is-disk-0 51 } 52 } 53 } 54 else 55 { 56 # 1个SSD 57 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD -NoNewWindow -wait -PassThru 58 echo there-is-one-ssd-but-no-hdd 59 } 60 } 61 "2" 62 { 63 if ($Disks[0].DeviceID -eq "1" ) #结果显示顺序与实际Number顺序相反. 64 { 65 if ( $SSD_Results[0].Size -gt $SSD_Results[1].Size ) # disk 1容量大于 disk 0 , OS安装在disk 0 66 { 67 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD0_HDD1 -NoNewWindow -wait -PassThru 68 echo ssd-0-is-smaller-than-ssd-1 69 } 70 else 71 { 72 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD1_HDD0 -NoNewWindow -wait -PassThru 73 echo ssd-1-is-smaller-than-ssd-0 74 } 75 } 76 ELSE #结果显示顺序与实际Number顺序一致 77 { 78 if ( $SSD_Results[0].Size -gt $SSD_Results[1].Size ) # disk 0容量大于disk 1, OS安装在disk 1. 79 { 80 81 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD1_HDD0 -NoNewWindow -wait -PassThru 82 echo ssd-1-is-smaller-than-ssd-0 83 } 84 else 85 { 86 Start-Process -FilePath DISKPART.exe -ArgumentList $SSD0_HDD1 -NoNewWindow -wait -PassThru 87 echo ssd-0-is-smaller-than-ssd-1 88 } 89 } 90 91 } 92 }
浙公网安备 33010602011771号