Home > Powershell Scripts, Windows Server, Windows Server Automation > Modify Disk layout in Windows using powershell – updated version

Modify Disk layout in Windows using powershell – updated version

At work one of my colleagues is fond of the phrase: Assume – makes and ass of you and me

Now this has, again, become very appropriate for me to use:

In my first post of the disk layout script, found here, I wrote with assumption and confidence: “but should also work on Windows Server 2003, if it does not then I have to redo the Altiris job”…

So I have redone the Altiris job powershell script:

#first we find and initialize physical disks with no partitions
$drives = gwmi Win32_diskdrive
$scriptdisk = $Null
$script = $Null
$OSversion = (((Get-WmiObject Win32_operatingsystem).version).ToString()).Split('.')[0]

foreach ($disk in $drives){
if ($disk.Partitions -eq "0"){
    $drivenumber = $disk.DeviceID -replace '[\\\\\.\\physicaldrive]',''
    
    If ($OSversion -eq "5"){
$script = @"
select disk $drivenumber
online noerr
create partition primary noerr
"@
    }
    ElseIf ($OSversion -eq "6"){
$script = @"
select disk $drivenumber
online disk noerr
attributes disk clear readonly noerr
create partition primary noerr
format quick
"@
}
    }
    $drivenumber = $Null
    $scriptdisk += $script + "`n"
    }
$scriptdisk | diskpart

#then we will move the CDRom drive to x:
(gwmi Win32_cdromdrive).drive | %{$a = mountvol $_ /l;mountvol $_ /d;$a = $a.Trim();mountvol x: $a}

#then we will assign letters and labels to physical drives
# thanks to powershell.com for this bit
#(http://powershell.com/cs/blogs/tips/archive/2009/01/15/enumerating-drive-letters.aspx)
$volumes = gwmi Win32_volume | where {$_.BootVolume -ne $True -and $_.SystemVolume -ne $True}
$letters = 68..89 | ForEach-Object { ([char]$_)+":" }
$freeletters = $letters | Where-Object { 
  (New-Object System.IO.DriveInfo($_)).DriveType -eq 'NoRootDirectory'
}
foreach ($volume in $volumes){
    if ($volume.DriveLetter -eq $Null){
        If ($OSVersion -eq "5"){
          mountvol $freeletters[0] $volume.DeviceID
          format $freeletters[0] /FS:NTFS /q /y
        }
        Else {
            mountvol $freeletters[0] $volume.DeviceID    
        }    
    }
$freeletters = $letters | Where-Object { 
    (New-Object System.IO.DriveInfo($_)).DriveType -eq 'NoRootDirectory'
}
} 

As it turns out, diskpart v5.2 does not support either the online disk command nor the format quick command, which is why I have had to add OS filtering (got to love the one-liner 🙂 and the format command.

I have also had to remove the $_.DriveType -eq "3", as an unformated primary partition under Windows Server 2003 R2 does not have a drivetype defined. But in order to salvage some honor, I’ll call that optimization, as it is actually not required, because that part of the script only assigns drive letters to volumes without one, and both the floppy and CDRom have one already.

/theadminguy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: