【问题标题】:How to set share permissions on a remote share with Powershell?如何使用 Powershell 在远程共享上设置共享权限?
【发布时间】:2014-10-29 02:46:33
【问题描述】:

我需要通过 Powershell 4 脚本设置远程共享的共享权限。我查看了this page,特别是命令Grant-SmbShareAccess,但是该cmdlet 设置了本地共享的权限,我很想看到-ComputerName 参数,但是,唉,没有。

我想做类似的事情:Grant-SmbShareAccess -ComputerName MYREMOTESERVER -Name <share_name> -AccountName <domain_account> -AccessRight Full

关于如何做到这一点的任何想法?我的远程服务器可以是 Windows Server 或 NetApp vFiler。

编辑

我在 cmets 中针对 NetApp vFiler 尝试了 Matt 对 Invoke-Command 的建议并得到了这个错误:

Connecting to remote server MYREMOTESERVER failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".

在 Windows 资源管理器中更改共享的安全性可以正常工作。

【问题讨论】:

  • 也许Invoke-Command 是您正在寻找的。更多信息here
  • @Matt 如果我只处理远程 Windows 服务器,我会查看它,但如果远程共享托管在 NetApp vFiler 上,我认为它不会起作用,因为它取决于远程处理。
  • 您是否尝试将服务器名称与共享名称一起传递?比如:Grant-SmbShareAccess -Name \\MYREMOTESERVER\SHARENAME -AccountName USERACCOUNT -AccessRight Full
  • @CitizenRon 感谢您的建议,但这也不起作用,因为该命令正在寻找本地共享。反正我试过了:Grant-SmbShareAccess : No MSFT_SMBShare objects found with property 'Name' equal to '\\MYREMOTESERVER\SHARENAME'. Verify the value of the property and retry.

标签: powershell windows-server windows-server-2012-r2 powershell-4.0


【解决方案1】:

Grant-SmbShareAccess 是一个CDXML 命令,这意味着它使用 CIM。正如您已经注意到的,它只能在至少运行 PSv3 的 Windows 系统上运行(在这种情况下,使用的 WMI 类仅存在于 Windows 8 和 Server 2012 或更高版本上)。

可能有其他方法可以针对非 Windows 服务器执行此操作,但我会尝试 PowerShell Access Control Module

$SharePath = "\\ServerName\ShareName"
$Principal = <account name here>

# Add permission to $Principal (-Force will suppress the prompt)
Get-SecurityDescriptor -Path $SharePath -ObjectType LMShare |
    Add-AccessControlEntry -Principal $Principal -LogicalShareRights FullControl -Apply #-Force

# Verify:
Get-SecurityDescriptor -Path $SharePath -ObjectType LMShare |
    Get-AccessControlEntry

老实说,我不知道这是否可行,因为我只针对 Windows 服务器进行了测试,而且我不经常处理共享权限。试试看,如果有效,我会从答案中删除这部分。

【讨论】:

  • 谢谢,有什么办法不用那个模块?我正在尝试使用 Windows Server 上的默认工具集来执行此操作。
  • 也许,但我不知道。模块有效吗?如果是这样,我可以修改答案以包含一个更短的 PowerShell 脚本,该脚本只包含 P/Invoke 签名和使其工作所需的 PS/.NET 代码(不需要模块)。
【解决方案2】:

对于 Windows Server SMB 共享,使用 -CimSession 参数。

对于非 Windows SMB 共享,我不希望 Windows SMB 管理 cmdlet 与它们一起使用。

【讨论】:

    【解决方案3】:

    Netapp Powershell 工具包将对此有所帮助。安装后,您可以将模块导入脚本,并管理您的共享。这是一个连接到文件管理器的粗略示例,提示输入共享名,然后使用一些默认权限配置该共享:

    # Import the Netapp Powershell Module
    import-module dataontap
    
    # Connect to the filer
    connect-nacontroller *filername*
    
    # Get the sharename
    $sharename = Read-Host -Prompt 'Enter the share you want to configure'
    
    # Configure the CIFS Permissions
    Set-NaCifsShareAcl $sharename "Authenticated users" -AccessRights "Change"
    Set-NaCifsShareAcl $sharename filername\administrators -AccessRights "Full Control"
    Set-NaCifsShareAcl $sharename domain\somegroup -AccessRights "Full Control"
    Remove-NaCifsShareAcl $sharename everyone
    

    【讨论】:

      猜你喜欢
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2018-02-15
      • 2012-07-05
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多