使用Powershell更改共享权限

时间:2015-11-09 18:01:08

标签: windows powershell permissions share

我正在尝试在运行2008 R2或2012的一堆Windows服务器上修改共享驱动器的共享权限。

我编写了一个脚本,你可以在这里找到:

Import-Module ActiveDirectory

$list = Get-ADComputer -Filter 'SamAccountName -like "*FP*"' | Select -Exp Name



foreach ($Computer in $list)


{ 
Grant-SmbShareAccess -Name User -CimSession Server -AccountName "username" -AccessRight Full -confirm:$false
$acl = (Get-Item \\$Computer\d$\User ).GetAccessControl('Access')
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule ("Corp\uc4serv","FullControl","ContainerInherit, ObjectInherit","None","Allow")
$acl.AddAccessRule($rule)
Set-Acl \\$Computer\d$\User $acl
Write-Host -ForegroundColor DarkGreen "Permissions granted on $Computer"
}
Write-Host -ForegroundColor DarkBlue "Command Has Completed" 

但它在2008服务器上不起作用,可能是因为它们无法运行Get-SmbShareAccess cmdlet。

我正在尝试做的与这篇文章非常相似:How to set share permissions on a remote share with Powershell?但特别是在Windows服务器上。

我还在网站上发现了这段代码(http://windowsitpro.com/powershell/managing-file-shares-windows-powershell):

$acl = Get-Acl `
  \\servername\c$\Users\username\sharetest
$permission = "corp\uc4serv","FullControl","Allow"
$accessRule = New-Object `
  System.Security.AccessControl.FileSystemAccessRule `
  $permission
$acl.SetAccessRule($accessRule)
$acl |
  Set-Acl \\servername\c$\Users\username\sharetest

但这只是在共享上设置安全性而不是共享权限。

我还研究过使用Net Share命令但是为了更改共享权限,它必须完全删除并重新创建共享驱动器。

1 个答案:

答案 0 :(得分:0)

您可以使用“网络共享”。使用Invoke-Command在每个远程服务器上运行它。

来源 - https://social.technet.microsoft.com/Forums/windowsserver/en-US/3edcabac-f1a8-4c4a-850c-8ba4697930a2/using-net-share-within-powershell

实施例

def GetValueFromFile(env, source):
    result = ""
    with open(str(source), "r") as f:
        content = f.read()
        # process read content, e.g.
        result = str(content.count("foo"))
    return env.Value(result)