2012-10-18 21 views
0

我试图让文件夹信息和安全信息对我们的服务器上的所有文件夹。 但我在这里不熟悉Powershell。介意帮助新手吗?多个命令CSV如何管道文件[Powershell的]

怎么办,我得到安全ACL管道输送到文本文件? 只有文件夹名称,大小,子文件夹数量的成员对象?

# Step 1 Get Folder Path 
function Select-Folder($message='Select a folder', $path = 0) { 
    $object = New-Object -comObject Shell.Application 

    $folder = $object.BrowseForFolder(0, $message, 0, $path) 
    if ($folder -ne $null) { 
     $folder.self.Path 
    } 
} 


#Step 2:Search For Directories 
$dirToAudit = Get-ChildItem -Path (Select-Folder 'Select some folder!') -recurse | Where {$_.psIsContainer -eq $true} 

foreach ($dir in $dirToAudit) 
{ 

#Step 3: Output: [Folder Path, Name, Security Owner, Size, Folder Count] 
#Pipe To CSV Text File 
    Get-Acl -Path $dir.FullName | Select-Object PSPath, Path,Owner | export-csv C:\temp\SecurityData.csv 
    #I also want the Folder path, Size and SubFolder Count 
} 


#Step 4: Open in Excel 
invoke-item -path C:\temp\SecurityData.csv 

这里的一些网站,我发现有用的主题:http://blogs.msdn.com/b/powershell/archive/2007/03/07/why-can-t-i-pipe-format-table-to-export-csv-and-get-something-useful.aspx

http://www.maxtblog.com/2010/09/to-use-psobject-add-member-or-not/

回答

1

这个任务是特别不容易。首先,您需要创建一个包含所需属性的自定义对象。这些属性将通过不同的命令添加例如:

$objs = Get-ChildItem . -r | 
      Where {$_.PSIsContainer} | 
      Foreach {new-object psobject -prop @{Path=$_.FullName;Name=$_.Name;FolderCount=$_.GetDirectories().Length}} 
$objs = $objs | Foreach { Add-Member NoteProperty Owner ((Get-Acl $_.Path).Owner) -Inp $_ -PassThru} 

$objs | Export-Csv C:\temp\data.csv 

获取文件夹大小需要一些额外的工作来计算。

+0

原来是PSobject一个哈希表的数据类型呢? –

+0

再次感谢您的帮助! –

+1

PSObject可用于创建自定义对象,即可将属性和方法添加到对象的位置。您也可以将其视为创建记录或结构的一种方式。 –