Search code examples
powershellwindows-server-2012-r2hyper-v

provide pool, virtual disc and shares


I am beginning to learn PowerShell on my own. In one of the books (Windows Server 2012 R2, implementation and maintaning) I got the task where I have to create script which after run, will create a storage pool, virtual disc, and new shares on new created vdisc.

Based on book instruction this is what I did already:

New-StoragePool -FriendlyName "Pool" -StorageSubSystemFriendlyName (Get-StorageSubSystem).FriendlyName -PhysicalDisk (Get-PhysicalDisk | where CanPool -eq True) -ProvisingTypeDefault Thin -ResiliencySettingNameDefault Mirror

New-VirtualDisk -FriendlyName "vDisk1" -StoragePoolFriendlyName "Pool" -Size 5TB

New-VirtualDisk -FriendlyName "vDisk2" -StoragePoolFriendlyName "Pool" -Size 10TB

New Partition -DiskNumber(Get-Disk | where BusType -eq Spaces).Number -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -Confirm:$false

Now I want to create shared folders for newly created virtual disk by New-Item Function. The problem is that I am not sure how to create a path for multiple disks and what's more the parameter -AssignDriveLetter from New Partition creates drive letter automatically. Because of that I do not know the drive letters. As a result I do not know how to set up variable Path in New-Item.


Solution

  • I suggest a general approach with this: Use variables as you configure your pools and disks, so you can reuse the them in your next steps and there will be references to specific objects that you have created:

    $NewPartitions = New-Partition -DiskNumber(Get-Disk | where BusType -eq Spaces).Number -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -Confirm:$false
    

    $NewPartitions variable will contain an array of your new partitions along with their drive letters, which you can iterate with foreach loop executing New-Item each time:

    $NewFolders = foreach ($DriveLetter in ($NewPartitions.DriveLetter))
     {
      New-Item -Type Directory -Path ($DriveLetter+":\share")
     }
    

    This will yield a folder called "share" on each of the drives, which you might want to then pass to New-SmbShare cmdlet using the same tactics. $NewFolders.FullName contains full paths of your newly created folders.