Skip to content

Instantly share code, notes, and snippets.

@irwins
Last active July 26, 2016 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irwins/b3d608b257916f95d4f0f84265749a9f to your computer and use it in GitHub Desktop.
Save irwins/b3d608b257916f95d4f0f84265749a9f to your computer and use it in GitHub Desktop.
<#
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment