Wednesday, August 31, 2011

Muxing AccessControl and FileInfo objects

Most of us know the members (partially printed at bottom) of System.Security.AccessControl and System.IO.FileInfo. And most of us know they both share the PS* NoteProperty items:
  • PSChildName                     NoteProperty   System.String PSChildName=test.txt
  • PSDrive                         NoteProperty   System.Management.Automation.PSDriveInfo PSDrive=C
  • PSParentPath                    NoteProperty   System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\
  • PSPath                          NoteProperty   System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\test.txt
  • PSProvider                      NoteProperty  



These 'NoteProperty' may be what makes co-operation between 'get-childitem' (alias 'ls' or 'gci') and 'get-acl'  straightforward:
[gci * | get-acl]




However, this produces a System.Security.AccessControl object only. What if you need to see both FileInfo properties and AccessControl properties in the same object?  For this we will have to do something more clever. Calculated Properties help us mux AccessControl and FileInfo objects into one, giving us an output of all files with their AccessControl information sorted by LastAccessTime. This is easier to do because AccessControl and FileInfo objects share the 'PSChildName' NoteProperty which acts here as a type of primary key:



function Check-RecentAccess {
[CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline=$true)]
           [int]$days=1
    )
$StartTime = (get-date) - (new-timespan -days $days)
$List=gci * | where {!$_.psiscontainer}
$Query= foreach ($i in $List) { gci $i |Select FullName,*Time, @{Label="Access";Expression={get-acl $_.PSChildName | % {$_.AccessToString}}}, @{Label="Owner";Expression={get-acl $_.PSChildName| % {$_.Owner}}}}
$Query | Select LastAccessTime,CreationTime,FullName,Owner,Access| where {$_.LastAccessTime -gt $StartTime} |Sort -descending LastAccessTime
}










I have uploaded:


Check-RecentAccess 
Check-RecentAccessRecurse


TypeName: System.Security.AccessControl.FileSecurity


Name                            MemberType     Definition
----                            ----------     ----------
Access                          CodeProperty   System.Security.AccessControl.AuthorizationRuleCollection Access{get=GetAccess;}
Group                           CodeProperty   System.String Group{get=GetGroup;}
Owner                           CodeProperty   System.String Owner{get=GetOwner;}
Path                            CodeProperty   System.String Path{get=GetPath;}
Sddl                            CodeProperty   System.String Sddl{get=GetSddl;}
....


TypeName: System.IO.FileInfo


Name                      MemberType     Definition
----                      ----------     ----------
Mode                      CodeProperty   System.String Mode{get=Mode;}
AppendText                Method         System.IO.StreamWriter AppendText()
CopyTo                    Method         System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(string destFileName, bool overwrite)
Create                    Method         System.IO.FileStream Create()
CreateObjRef              Method         System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
...

No comments:

Post a Comment