简体   繁体   中英

PowerShell keeps throwing an exception

$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
    }
}

I get the following error messages.

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

Does anyone have any idea whats going on here?

$DNName isn't doing what you expect. Add a breakpoint and verify that this:

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

is working correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM