Monthly Archives: July 2016

SMB shares Operational readiness

Sup’ PSHomies!

In migration mode! It’s been awhile since I’ve done post-configuration of File Servers. I needed to recreate a DFSn structure in our Datacenter. A great opportunity to try out the new dfsn en smb cmdlets! 😉

The SMB & DFSn cmdlet work with CIMSessions.  Before I can create the DFS structure I need to create the shares… Here’s an impression of what the csv file looks like:

Server	ShareName	Path	FullAccess	ChangeAccess	ReadAccess
AZR-FS-01	FS_ARC001$	E:\FS_ARC001	NT AUTHORITY\Authenticated Users
AZR-FS-01	FS_ARC002$	E:\FS_ARC002	NT AUTHORITY\Authenticated Users
AZR-FS-01	FS_ARC003$	E:\FS_ARC003	NT AUTHORITY\Authenticated Users
AZR-FS-01	FS_ARC004$	E:\FS_ARC004	NT AUTHORITY\Authenticated Users
AZR-FS-01	FS_ARC005$	E:\FS_ARC005	NT AUTHORITY\Authenticated Users
AZR-FS-01	FS_ARC006$	E:\FS_ARC006	NT AUTHORITY\Authenticated Users

I’ll be using the servername to retain the cimsession amongst other things. Instead of giving everyone FullAccess to the share, we’re giving ‘Authenticates users’ FullAccess. To complete the list I’ve added Change- and ReadAccess as well even though I won’t be using them.

<#
Author: I. Strachan
Version:
Version History:
Purpose: Create new SMB shares
#>
#region import saved credential and csv File
$Hash = Import-CliXml -Path "${env:\userprofile}\Hash.Cred"
$cred = $Hash.'admstrachan'
$exportDate = Get-Date -Format ddMMyyyy
$csvShares = Import-Csv .\sources\csv\DFSnShares.csv -Delimiter "`t" -Encoding UTF8
#endregion
#region Main
$csvShares |
Foreach-object{
#Get CIMSession
$CIMSession = Get-CimSession -Name $_.Server
#If it doesn't exist create one
if(!$CIMSession){
$CIMSession = New-CimSession -ComputerName $_.Server -Name $_.Server -Credential $cred
}
#Test if Share is available
$Share = Get-SmbShare -Name $_.ShareName -CimSession $CIMSession -ErrorAction SilentlyContinue
if($Share){
#Test if Share path is equal to CSV Share Path
if($_.Path -eq $share.Path){
[PSCustomObject]@{
Server = $_.Server
ShareName = $_.ShareName
Path = $_.Path
Exists = $true
PathCorrect = $true
}
}
else{
[PSCustomObject]@{
Server = $_.Server
ShareName = $_.ShareName
Path = $_.Path
Exists = $true
PathCorrect = $false
}
}
}
else{
#Create folder via UNCPath
$UNCPath = '\\{0}\{1}' -f $_.Server,($_.Path).Replace(':','$')
New-Item -Path $UNCPath -ItemType Directory -Force
New-SmbShare -CimSession $CIMSession -FullAccess $_.FullAccess -Name $_.ShareName -Path $_.Path
[PSCustomObject]@{
Server = $_.Server
ShareName = $_.ShareName
Path = $_.Path
Exists = 'Created'
PathCorrect = $true
}
}
} |
Export-Csv .\export\dfsn\DFSnShares-Created-$($exportDate).csv -Encoding UTF8 -Delimiter "`t" -NoTypeInformation
#endregion
#Remove all CimSessions
Get-CimSession | Remove-CimSession
view raw New-SMBShares.ps1 hosted with ❤ by GitHub

Here’s a quick rundown of the script. I’ve created quite a few checkpoints. First, see if the share exists then verify if the paths are identical. You never know if the share already exists and is pointing to another folder, I’ve learned that from past migration. If it doesn’t exist create it!

The reason for this setup is that I want to be able to re-run this script in the future. Without the check we’d end up with quite some errors… Incidentally, this is how I did my validation pre Pester… 😉

Ok now for the fun part!

<#
Author: I. Strachan
Version:
Version History:
Purpose: OVF for SMBShares
#>
[CmdletBinding()]
Param(
$csvFile = 'DFSnShares.csv'
)
#region Import CSV and saved Credentials
$csvDFSnShares = Import-Csv .\sources\csv\$csvFile -Encoding UTF8 -Delimiter "`t"
$savedCreds = Import-CliXml -Path "${env:\userprofile}\Hash.Cred"
$cred = $savedCreds.'admstrachan'
#endregion
#region
$csvDFSnShares |
ForEach-Object{
#If CIMSession doesn't exist create one
if(!$( Get-CimSession -Name $_.Server -ErrorAction SilentlyContinue)){
$CIMSession = New-CimSession -ComputerName $_.Server -Name $_.Server -Credential $cred
}
else{
#Get CIMSession
$CIMSession = Get-CimSession -Name $_.Server
}
#Test if Share is available
$Share = Get-SmbShare -Name $_.ShareName -CimSession $CIMSession -ErrorAction SilentlyContinue
Describe "SMBShare Operation validation share $($_.ShareName) on $($_.Server)" -Tags 'DFSnShares' {
Context "Verifying share properties on $($_.Server)" {
It "Share Name $($_.ShareName) exists" {
$_.ShareName | Should be $Share.Name
}
It "Share Path $($_.Path) exists"{
$_.Path | Should Be $Share.Path
}
It "$($_.FullAccess) has full Access" {
$FullAccess = Get-SmbShareAccess -CimSession $CIMSession -Name $share.Name | Where-Object{$_.AccessRight -eq 'Full'}
$FullAccess.AccountName.Contains($_.FullAccess) | Should be $true
}
}
}
}
#endregion
#region Remove CimSessions
Get-CimSession | Remove-CimSession
#endregion
view raw SMB.Shares.Test.ps1 hosted with ❤ by GitHub

This time I didn’t make use of snapshots and went straight to the source! Using the csv file I can verify that the shares have been create using the right path and that ‘Authenticated Users’ have Full access.

Screen Shot 2016-07-26 at 12.40.06

Looking at the creation script you might wonder why bother with an OV script (Operation Validation)? As chance would have it, one of the File Server’s  volume needed to be recreated. Which meant shares had to be recreated (and validated…) Now I wasn’t part of the recreation process, but I could validate that the shares were available as intended once the recreation took place! 😉 It’s all about having peace of mind… I just ran the test before posting this blog, everything is as it should be… 🙂

Hope it’s worth something to you

Ttyl,

Urv