New-FileShare
Windows: Creates a new SMB file share and configures permissions
#Requires -Version 5.1
#Requires -Modules SmbShare
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true)]
[string]$Name,
[Parameter(Mandatory = $true)]
[string]$Path,
[string]$ComputerName = $env:COMPUTERNAME,
[string]$Description,
[string[]]$FullAccess,
[string[]]$ChangeAccess,
[string[]]$ReadAccess,
[pscredential]$Credential
)
Process
{
try
{
$session = $null
$cimParams = @{
'ErrorAction' = 'Stop'
}
if ($ComputerName -ne $env:COMPUTERNAME)
{
$sessionParams = @{
'ComputerName' = $ComputerName
}
if ($null -ne $Credential)
{
$sessionParams.Add('Credential', $Credential)
}
$session = New-CimSession @sessionParams
$cimParams.Add('CimSession', $session)
}
$newShareParams = @{
'Name' = $Name
'Path' = $Path
'Description' = $Description
'ErrorAction' = 'Stop'
}
if ($null -ne $FullAccess) { $newShareParams.Add('FullAccess', $FullAccess) }
if ($null -ne $ChangeAccess) { $newShareParams.Add('ChangeAccess', $ChangeAccess) }
if ($null -ne $ReadAccess) { $newShareParams.Add('ReadAccess', $ReadAccess) }
New-SmbShare @newShareParams @cimParams | Out-Null
$result = Get-SmbShare -Name $Name @cimParams | Select-Object Name, Path, Description
Write-Output $result
}
catch
{
throw
}
finally
{
if ($null -ne $session)
{
Remove-CimSession $session
}
}
}Specifies the name of the new share.
Specifies the local path of the folder to be shared. The folder must exist on the target system.
Specifies the name of the target computer. Defaults to the local computer.
Specifies an optional description for the share.
Specifies accounts (e.g., "DOMAIN\User") to be granted Full Control permissions.
Specifies accounts to be granted Change (Modify) permissions.
Specifies accounts to be granted Read-only permissions.
Specifies a PSCredential object for remote connection.