I had to throw together a script that would create a folder and then share it, on multiple servers.  I was having trouble getting it to work on remote servers, but after changing to Invoke-Command it came together a bit cleaner.

Note: the SMBshare cmdlets are only available on Server 2012 and Windows 8.

 

$command = {
## Variables
$path = "c:\testfolder"
$share = "testShare"
## Create Folder
IF (!(test-path $path)){
write-host "Creating folder: " $path -ForegroundColor green
New-Item -Path $path -ItemType directory
} else {
write-host "The folder already exists: "$path -ForegroundColor Yellow
}

## Create Share
IF (!(Get-SmbShare -Name $share -ErrorAction SilentlyContinue)) {
write-host "Creating share: " $share -ForegroundColor green
New-SmbShare –Name $share –Path $path –Description ‘Test Shared Folder’ –FullAccess Administrator –ReadAccess Everyone
} else {
write-host "The share already exists: " $share -ForegroundColor Yellow
}
}
$servers = get-content "B:\Temp\servers.txt"
Invoke-Command -ComputerName $servers -scriptblock $command

 

Advertisements