使用循环和阵列的PowerShell驱动器配置

时间 2019-02-21
阅读 41
点赞 0
收藏 0
连接tyler-durden

我创建了一个PowerShell脚本来进行一些操作系统驱动器配置,但它很长,我想缩短它很多。我可以使用PowerShell,但不能使用循环或数组。我肯定我可以加一些for循环来缩短它。查看示例可以帮助我。抱歉,代码太长了,但它的工作原理。

#PS Script to online drives, initialize, create volume, and format the volume.

# Online the 5 HDD's

set-disk -Number 1 -IsOffline $False
set-disk -Number 2 -IsOffline $False
set-disk -Number 3 -IsOffline $False
set-disk -Number 4 -IsOffline $False
set-disk -Number 5 -IsOffline $False

# Makes a read only drive writeable

set-disk -number 1 -isReadOnly $False
set-disk -number 2 -isReadOnly $False
set-disk -number 3 -isReadOnly $False
set-disk -number 4 -isReadOnly $False
set-disk -number 5 -isReadOnly $False

# Initializes the disks

Initialize-Disk -number 1 -PartitionStyle GPT
Initialize-Disk -number 2 -PartitionStyle GPT
Initialize-Disk -number 3 -PartitionStyle GPT
Initialize-Disk -number 4 -PartitionStyle GPT
Initialize-Disk -number 5 -PartitionStyle GPT

# Creates a new volume and assigns the specific drive letter

New-Partition -DiskNumber 1 -DriveLetter D -UseMaximumSize
New-Partition -DiskNumber 2 -DriveLetter E -UseMaximumSize
New-Partition -DiskNumber 3 -DriveLetter F -UseMaximumSize
New-Partition -DiskNumber 4 -DriveLetter G -UseMaximumSize
New-Partition -DiskNumber 5 -DriveLetter K -UseMaximumSize

# Formats the drive with 64KB, sets it as NTFS and Labels it. 

Format-Volume -DriveLetter D -FileSystem NTFS -NewFileSystemLabel SQL -AllocationUnitSize 65536 -Force -Confirm:$false
Format-Volume -DriveLetter E -FileSystem NTFS -NewFileSystemLabel Data -AllocationUnitSize 65536 -Force -Confirm:$false
Format-Volume -DriveLetter F -FileSystem NTFS -NewFileSystemLabel Logs -AllocationUnitSize 65536 -Force -Confirm:$false
Format-Volume -DriveLetter G -FileSystem NTFS -NewFileSystemLabel TembDB -AllocationUnitSize 65536 -Force -Confirm:$false
Format-Volume -DriveLetter K -FileSystem NTFS -NewFileSystemLabel Backups -AllocationUnitSize 65536 -Force -Confirm:$false
建议答案

谢谢你的回复。最后我写了这个,它起了作用:

$drives = @()
  $drives = @("1:D:SQL","2:E:Data","3:F:Logs","4:G:TempDB","5:K:Backup")
  foreach ( $drive in $drives) {
  $volume,$letter,$label = $drive.split(':')
  set-disk -Number $volume -IsOffline $False
  set-disk -number $volume -isReadOnly $False
  Initialize-Disk -number $volume -PartitionStyle GPT
  New-Partition -DiskNumber $volume -DriveLetter $letter -UseMaximumSize
  Format-Volume -DriveLetter $letter -FileSystem NTFS -NewFileSystemLabel "$label" -AllocationUnitSize 65536 -Force -Confirm:$false
}
👍 0