Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Copy-SmbShareAccess()
- {
- [CmdletBinding(DefaultParameterSetName="BySmbShareName")]
- param
- (
- [Parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByInputObject", DontShow=$true)]
- [CimInstance] $InputObject,
- [Parameter(Mandatory=$true, Position = 0, ParameterSetName="BySmbShareName")]
- [string] $Name,
- [Parameter(Mandatory=$true, Position = 0, ParameterSetName="ByInputObject")]
- [Parameter(Mandatory=$true, Position = 1, ParameterSetName="BySmbShareName")]
- [ValidateScript({
- # Must be a CimInstance or string.
- $_ -is [CimInstance] -or $_ -is [string]
- })]
- [object] $Destination,
- [Parameter(Mandatory=$false)]
- [switch] $Overwrite,
- [Parameter(Mandatory=$false)]
- [switch] $Force,
- [Parameter(Mandatory=$false)]
- [switch] $PassThru
- )
- Begin
- {
- if ($PSBoundParameters.ContainsKey("Name"))
- {
- $InputObject = Get-SmbShare -Name $Name
- }
- if ($Destination -is [string])
- {
- $Destination = Get-SmbShare -Name $Destination
- }
- }
- Process
- {
- $entries = New-Object 'System.Collections.Generic.List[object]'
- if ($InputObject.CimSystemProperties.ClassName -eq "MSFT_SmbShare" -and $InputObject.CimSystemProperties.Namespace -eq "ROOT/Microsoft/Windows/SMB")
- {
- $InputObject | Get-SmbShareAccess | ForEach-Object {
- $entries.Add($_)
- }
- }
- elseif ($InputObject.CimSystemProperties.ClassName -eq "MSFT_SmbShareAccessControlEntry" -and $InputObject.CimSystemProperties.Namespace -eq "ROOT/Microsoft/Windows/SMB")
- {
- $entries.Add($_)
- }
- }
- End
- {
- if ($Overwrite)
- {
- $Destination | Get-SmbShareAccess | ForEach-Object {
- Revoke-SmbShareAccess -InputObject $Destination -AccountName $_.AccountName -ErrorAction Stop -Force:$Force | Out-Null
- }
- }
- foreach ($entry in $entries)
- {
- $accessArgs = @{
- InputObject = $Destination
- AccountName = $entry.AccountName
- #ScopeName = $entry.ScopeName
- Force = $Force.ToBool()
- }
- if ($entry.AccessControlType -eq "Allow")
- {
- Grant-SmbShareAccess @accessArgs -AccessRight $entry.AccessRight | Out-Null
- }
- else
- {
- Block-SmbShareAccess @accessArgs | Out-Null
- }
- }
- if ($PassThru)
- {
- $Destination | Get-SmbShareAccess
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement