PowerShell不断抛出异常

时间:2014-04-07 19:11:56

标签: exception powershell error-handling active-directory

$RootPath = "W:\"
$OutFile = "I:\Permissions.csv"
$Header = "Folder Path,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags"
#Del $OutFile
Add-Content -Value $Header -Path $OutFile 

$Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true}

foreach ($Folder in $Folders)
{
    $ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access  }
    Foreach ($ACL in $ACLs)
    {

        $objSID = New-Object System.Security.Principal.SecurityIdentifier($ACL.IdentityReference.Value) 
        #$objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
        $objUser = $objSID.Translate([System.Security.Principal.SecurityIdentifier])
        $objUser.Value

        $DName = ([adsi]"LDAP://<SID=$($ACL.IdentityReference.value)>").distinguishedName

        $s = $DName
        $s -replace "(CN=)(.*?),.*",'$2'

        $dname.split("=")[1].split(",")[0]

        #Show User
        Write-Host “`r`nThe user mapped to SID $($objSID) is $($objUser.value)`r`n” -f “Red”

        $OutInfo = $Folder.Fullname + "," + $dname.split("=")[1].split(",")[0]  + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
        Add-Content -Value $OutInfo -Path $OutFile
    }
}

我收到以下错误消息。

You cannot call a method on a null-valued expression.
At I:\PermissionExporter.ps1:31 char:57
+         $OutInfo = $Folder.Fullname + "," + $dname.split <<<< ("=")[1].split(",")[0]  + "," 
+ $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.P
ropagationFlags
    + CategoryInfo          : InvalidOperation: (split:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

有没有人知道这里有什么想法?

1 个答案:

答案 0 :(得分:0)

$ DNName没有做你期望的事情。添加断点并验证:

$ dname.split(&#34; =&#34;)[1] .split(&#34;&#34;)[0]

工作正常。