更改配置文件夹的所有者

时间:2016-02-17 15:04:45

标签: powershell powershell-v2.0 powershell-v3.0

我有这个脚本

$dir = '\\knesmbmdc001\profiles'
$logFile = 'C:\user_done.txt'
Get-ChildItem -Directory $dir | % {
$user = $_.Name
$acl = Get-Acl –LiteralPath $_.FullName
$userIdentity = New-Object System.Security.Principal.NTAccount $user
$acl.SetOwner($userIdentity)
Set-Acl –LiteralPath $_.FullName -AclObject $acl
Add-Content $logFile $user
}

我收到此错误

Exception calling "SetOwner" with "1" argument(s): "Some or all identity references could not be translated."
Set-Acl : The security identifier is not allowed to be the owner of this object.

我正在尝试获取文件夹的名称(用户名)并将其设置为所有者。

1 个答案:

答案 0 :(得分:0)

该文件夹的名称不是有效的用户名。您应该在脚本中输出$user以查看哪个失败。我假设这是Windows域的漫游配置文件。我看到两个潜在的问题:

  1. 用户是否存在?从域中删除用户时,不会删除漫游配置文件,因此可能是已删除的用户
  2. Vista及更新版本中的配置文件最后可能会有一个版本号,以避免在不同的操作系统之间混合配置文件,例如。 username.v2。所以你需要从中提取名称。
  3. 如果(因为-eq 2),请尝试:

    $user = $_.Name.Split(".")[0]
    
相关问题