首页 > 其他 > 详细

Grant-Permission.ps1

时间:2014-02-01 13:00:50      阅读:521      评论:0      收藏:0      [点我收藏+]

Grant-Permission.ps1

Download the EXE version of SetACL 3.0.6 for 32-bit and 64-bit Windows. Put setacl.exe at the same location with the script.

bubuko.com,布布扣
function Grant-Permission
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter()]
        [string]$ComputerName = $env:computername,
        
        [Parameter(Mandatory=$true)]
        [string]$Path,
        
        [Parameter(Mandatory=$true)]
        [ValidateSet(file,reg,srv,prn,shr,wmi)]
        [string]$Type,
        
        [Parameter(Mandatory=$true)]
        [string]$Name,
        
        [Parameter(Mandatory=$true)]
        [string]$Permission,
        
        [Parameter()]
        [switch]$PassThru
    )
    
    Write-Verbose "Granting ‘$Name‘ $Permission permission on ‘$ComputerName‘..."
    
    if(!(Test-Connection $ComputerName -Count 1 -Quiet))
    {
        Write-Error "Unable to connect ‘$ComputerName‘. The network path not found."
        return
    }
    try
    {
        if(!$PSScriptRoot) { $PSScriptRoot = Split-Path (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Path }
        Set-Alias setacl "$PSScriptRoot\setacl.exe"
        if($ComputerName -ne $env:computername) { $fullPath = "\\$ComputerName\$Path" } else { $fullPath = $Path }
        $result = Invoke-Expression "setacl -on `"$fullPath`" -ot $Type -actn ace -ace `"n:$Name;p:$Permission`""
        if($result -match "error") {
            Write-Error ($result -join "`n")
            return
        }
        Write-Verbose ($result -join "`n")
        Write-Verbose "‘$Name‘ has been granted $Permission permission on ‘$ComputerName‘."
        
        if($Passthru)
        {
            $pso = New-Object PSObject -Property @{
                 ComputerName = $ComputerName.ToUpper()
                 Path = $Path
                 Type = $Type
                 Name = $Name
                 Permission = $Permission
            }
            $pso.PSTypeNames.Clear()
            $pso.PSTypeNames.Add(MKServerBuilder.ACL)
            $pso
        }
    }
    catch
    {
        $_
    }
}
bubuko.com,布布扣

Grant-Permission.ps1

原文:http://www.cnblogs.com/edward2013/p/3536847.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!