本地权限和本地用户/组的远程管理

时间:2018-10-26 19:28:18

标签: .net powershell permissions file-permissions remote-access

我正在尝试使用该远程计算机的本地用户和组来修改远程计算机的文件权限。

$IDR = New-Object System.Security.Principal.NTAccount( 'RemoteServer\testaccount' )

$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
    $IDR,
    'FullControl',
    'ContainerInherit, ObjectInherit',
    'None',
    'Allow'
)

$Item = (Get-Item \\RemoteServer\share\folder\folder\file)
$ACL = $Item.GetAccessControl()
$ACL.AddAccessRule($AccessRule)
$Item.SetAccessControl($ACL)

输出:带有“ 1”参数的调用“ AddAccessRule”的异常:“某些或所有身份引用无法翻译。”

我知道这意味着找不到我要搜索的“计算机\测试帐户”。我可以使用Builtin *(例如,Builtin \ Administrators)用户和组修改远程文件权限,但不能使用创建的帐户来修改。

testaccount在RemoteServer上确实存在。

\\ RemoteServer \ testaccount不起作用

建筑物\测试帐户不起作用

本地\测试帐户不起作用

测试帐户无效

我可以通过大致相同的方式从本地文件权限中远程删除本地帐户。

是否从我的本地工作站将RemoteServer的非内置本地用户和组添加到RemoteServer的本地文件权限中?

OR

如何解决上述错误?

解决方法编辑:

我不想这样做,但是我能够使用Win32_Process启动PowerShell会话,并使用所有数据来应用权限。有用。但是,这并不理想,因为它无法在不能接受WMI的系统上运行。我仍然想知道如果没有这种笨拙的解决方法,是否有可能解决我的原始问题。

1 个答案:

答案 0 :(得分:0)

您的代码似乎有些错误。

首先,您有错字

\\Computer\C$\user\username\desktop\testfile

但是应该是

\\Computer\C$\users\username\desktop\testfile

(“用户” =复数)。

接下来,您将使用Computer\testaccount获取Security.Principal.NTAccount,但是稍后您将使用username获取文件项。

这应该可以:

# create the Full Control access rule for the user
$AccessRule = (New-Object System.Security.AccessControl.FileSystemAccessRule(
                [System.Security.Principal.NTAccount]"$computerName\$userSamAccountName",
                "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow"))

# get the item you want to set the FullControl accessrule on.
# you need permissions for this of course..
$Item = (Get-Item "\\$computerName\C$\user\$userSamAccountName\desktop\testfile")
$ACL = (Get-Acl -Path $Item.FullName)
$ACL.AddAccessRule($AccessRule)
Set-Acl -Path $Item.FullName $ACL