You are setting up a new template or VM to be used with SQL Server and to get the best performance you are adding multiple disks to the VM. When you first boot the VM the disks are all offline. First thing you do is change the default SAN policy in diskpart to set disks online as follows:
diskpart san policy=onlineall exit
This is great, by default all the connected disks will come online when the VM boots. But what about brining all of the attached disks online all at once so you can do things with them? Well, for that, you need a small script.
I stumbled across an article while Googling, on the Microsoft Technet Forum here. Unfortunately the script didn’t work with a lot of disks, as it had a bug in it. I corrected the bug, so I thought I’d post my modified script here. You can use this to mark all disks on a particular VM or any Windows server for that matter online. I’ve tested this with Windows 2012 R2 and Windows 2016.
#Check for offline disks on server. $offlinedisk = "list disk" | diskpart | where {$_ -match "offline"} #If offline disk(s) exist if($offlinedisk) { #for all offline disk(s) found on the server foreach($offdisk in $offlinedisk) { $offdiskS = $offdisk.Substring(2,7) #Creating command parameters for selecting disk, making disk online and setting off the read-only flag. $OnlineDisk = @" select $offdiskS attributes disk clear readonly online disk attributes disk clear readonly "@ #Sending parameters to diskpart $OnlineDisk | diskpart } }
An alternative suggestion proposed by Barrie in the comments is to use the native PowerShell commands as follows:
$offlinedisks = get-disk | where OperationalStatus -EQ offline foreach ($disk in $offlinedisks) {Set-Disk -Number $disk.Number -IsOffline $false Set-Disk -Number $disk.Number -IsReadOnly $false}
Final Word
The above script is really useful if you are going to add a lot of disks to a VM. You could extend it further to create partitions on all of the disks as well. I used the above to help set up a few SQL Server VM’s for some testing, but it could be used for any type of Windows VM. Don’t forget to turn off write cache by changing the Windows Disk Policy to Quick Remove as I mentioned in this article, before starting any performance testing.
This post first appeared on the Long White Virtual Clouds blog at longwhiteclouds.com. By Michael Webster +. Copyright © 2012 – 2017 – IT Solutions 2000 Ltd and Michael Webster +. All rights reserved. Not to be reproduced for commercial purposes without written permission.
You could also use native Powershell disk commands to avoid cmd tools
$offlinedisks = get-disk | where OperationalStatus -EQ offline
foreach ($disk in $offlinedisks)
{Set-Disk -Number $disk.Number -IsOffline $false
Set-Disk -Number $disk.Number -IsReadOnly $false}
Thanks Barrie, that's a great suggestion.
[…] via Windows Script for Marking All Disks Online — Long White Virtual Clouds […]