I’ve been using Powershell more and more lately, most of the time the motivation has been to do repeatable tasks much more quickly and efficiently but there has also been other cases where Powershell has been the only way to accomplish something. Most often this is due to lacking GUI or Windows issues that have prevented me from using the GUI in the first place. This post describes the latter scenario and the Powershell workaround.
I was building a database cluster for one of our customers and part of the HA/DR/Reporting solution for that environment is to set up a Log Shipping to create a copy of databases to a secondary server. Log shipping requires a shared folder through which the transaction log backups are copied and we usually create this into the same cluster group than the SQL Server. I started my usual routine, going to Failover Cluster Manager GUI, picking the role I wanted to add the shared folder into and then I clicked “Add File Share”. A window popped open, like the one below.
It remained there for a while and then disappeared, I waited for a moment and then tried it again and the exactly same thing happened. Few reboots and some more attempts later it was obvious that doing this through GUI would not work. It did something though, as I saw this kind of error at the GUI.
Couple Internet searches later I found some articles and posts saying that this is something that could happen in Windows and creating shared folders with Powershell should still work. None of these posts actually gave any advice on how to do that exactly, but finding it out wasn’t really that difficult. I knew I was looking for a way to create a share, so I fired up Powershell console and ran the following command.
Get-Command -Noun *share*
If you ever need to figure out what cmdlet to use, Get-Command is your friend. If you only can remember single cmdlet, make it this one In my case I got the following listing.
New-SmbShare looked promising so I checked the cmdlet help, the syntax was rather simple (it does have few more parameters than I’m using, but this was enough for me).
New-SmbShare -Name ShareName -Path X:\SomeFolder\ -FullAccess DOMAIN\UserAcc
In my case, when the disk was already in a correct role I didn’t even need to specify –ScopeName, it automatically added the File Server to correct role. To verify that the newly created share existed I used the following command:
Get-SmbShare -Name SharedFolder
The next thing I did was to verify that the permissions were set ok, this can be done with the following command:
Get-SmbShareAccess -Name ShareName -ScopeName ServerName
The –ScopeName parameter with SmbShare cmdlets is used to specify the server you’re managing. If the name of the shared folder is unique in the cluster, like in the example above, then you don’t need it.
If you need to add other permissions to shared folder it can be accomplished with the following command:
Grant-SmbShareAccess -Name ShareName -ScopeName ServerName -AccountName DOMAIN\UserAcc -AccessRight Full/Change/Read
So once again Powershell saved the day, also after I figured out the proper cmdlets, creating the actual shared folder and setting permissions was much quicker than doing it through GUI in the first place! And if you need to do it multiple times, like in my example where I had four different cluster groups, it’s even better.
Update on 3.5.2017
When you operate with Windows Failover Cluster resources, such as clustered drives in this case, make sure that you are doing it on the node where they are online. If you try to create the share with the above commands on the node where they are offline, it will not work.