我设法获取文件的身份参考,如下所示。
$arr=@((Get-Acl data.txt).Access|Select-Object -ExpandProperty IdentityReference)
for($i=0; $i -lt $arr.length ; $i++){
$test=($arr[$i]).Value ->The value $test needs to be checked if it Group Or User.
}
是否可以识别IdentityReference是一个组还是一个简单的用户。
提前致谢!
答案 0 :(得分:1)
如果您的计算机上安装了Framework .NET 3.5,并且您使用的是Active-Directory,则可以使用此解决方案
$file = 'data.txt'
# Loading the assembly
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
# Retreiving a principal context for the administrator
$domainContext = New-Object DirectoryServices.AccountManagement.PrincipalContext([DirectoryServices.AccountManagement.ContextType]::Domain, "VMESS01" , "jpb", "password")
try {
$irefs=@((Get-Acl $file).Access|Select-Object -ExpandProperty IdentityReference)
ForEach ($iref in $irefs)
{
$name = $iref.value.split("\\");
$groupPrincipal = [DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($domainContext, [DirectoryServices.AccountManagement.IdentityType]::Name, $name[1])
if ($groupPrincipal -ne $null)
{
Write-Host "$iref is a group"
}
else
{
Write-Host "$iref is not a group"
}
}
}
catch
{
$_
}
finally {
$domainContext.Dispose()
}
如果你有其他先决条件,请告诉我们?
编辑:
以下是使用ADSI的解决方案,您可以在PowerShell V1.0中使用它。我没有解决NT Authority \ SYSTEM a.k.a LocalSystem的案例。
Function IsGroup ($dn, $samAccountName)
{
$rc= "NA"
$Rech = new-object System.DirectoryServices.DirectorySearcher($dn)
$dumy = $Rech.filter = "((samAccountName=$samAccountName))"
$dumy = $Rech.SearchScope = "subtree"
$dumy = $Rech.PropertiesToLoad.Add("objectClass");
$dumy = $Rech.PropertiesToLoad.Add("objectCategory");
$Object = $Rech.findone()
if ($object -ne $null)
{
if ($object.Properties["objectCategory"] -like "CN=Group,*")
{
$rc = $true
}
else
{
$rc = $false
}
}
return $rc
}
Clear-Host
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://localhost:389/DC=SILOGIX-ESS01,DC=local","silogix@SILOGIX-ESS01.local","root.123;321.toor")
$arrs=@((Get-Acl 'data.txt').Access|Select-Object -ExpandProperty IdentityReference)
foreach ($arr in $arrs)
{
$arr
$netbiosName,$samAccountName = $arr -split '\\'
write-host "$arr est un group : $(IsGroup $dn $samAccountName)"
}
答案 1 :(得分:0)
我在计算机上尝试了此代码段,其中列出了每个身份参考的组和用户:
$arr=@((Get-Acl 'data.txt').Access|Select-Object -ExpandProperty IdentityReference)
for($i=0; $i -lt $arr.length ; $i++) {
#$test=($arr[$i]).Value ->The value $test needs to be checked if it Group Or User.
$name = $arr[$i].ToString().Split('\')[1];
$obj = Get-ADObject -Filter {SamAccountName -eq $name}
if ($obj) {
$obj.ObjectClass
}
}
希望这对您有用。