如果特定用户在DFS文件夹上读取或写入访问权限(以验证我的DFS),我需要尝试。
我是域名管理员,我在$ Credentials中拥有此用户的凭据 但是Test-Path,Get-Item,New-Item不接受凭证
答案 0 :(得分:2)
使用Invoke-Command
在其他用户的上下文中运行命令:
$computer = 'localhost'
$cred = Get-Credential
Invoke-Command -Computer $computer -Credential $cred -ScriptBlock {
Param($path)
if (Test-Path -LiteralPath $path) {
New-Item -ItemType File "$path\your.txt"
}
} -ArgumentList 'C:\some\folder'
你也可以使用Start-Job
,但这会在后台运行scriptblock,因此它意味着更多的管理开销。
如果其他所有方法都失败,则另一个选项可能是runas.exe
,但可能需要一些creative quoting:
& runas /user:DOM\username "powershell -Command \`"&{ New-Item 'C:\some\folder\your.txt' }`"\"
如果您只需要检查给定文件夹的权限而不执行操作,则可以使用Get-Acl
:
Get-Acl 'C:\some\folder' |
select -Expand Access |
? { $_.IdentityReference -like '*\username' }
答案 1 :(得分:1)
而是递归地检查acl,我使用Start-Process
的真实写访问权限和特定凭据(start-process
是别名runas
)。
该过程使用$(whoami)内容编写新文件
在结束进程后检查文件是否存在及其内容。
function test-Write ($folder)
$WinCredential = Get-Credential -UserName "Domain\User" -Message "Login"
Start-Process -WindowStyle Hidden -Wait -Credential $WinCredential -FilePath "powershell.exe" -ArgumentList "whoami | out-file '$folder\test.txt'"
if ((get-content "$folder\test.txt") -like "Domain\User") {
return 'OK'
}
return 'Erreur NTFS Access'
}