Retrieve information of network share and put results in Excel

Post date: Oct 16, 2013 7:10:43 AM

This script looks at a networkshare, retrieves directorynames and show which Groups/Users have access to that directory.

cls

# Ask for credentials

$cred = Get-Credential

# Connect PS drive

New-PSDrive -Credential $cred -PSProvider Filesystem -Root \\netwerk-share\Data -Name "Netwerkdrive"

#Create a new Excel object using COM

$Excel = New-Object -ComObject Excel.Application

$Excel.visible = $True

$Excel = $Excel.Workbooks.Add()

$Sheet = $Excel.Worksheets.Item(1)

$intcol = 4

$intRow = 1

$path = "\\netwerk-share\data"

$maindirectories = gci $path | ?{ $_.PSIsContainer }| select name

$Sheet.Cells.Item($intRow,1) = "Directorystructure FileShare"

$Sheet.Cells.Item($intRow,2) = "\\netwerk-share\Data"

$intRow ++

foreach ($maindirectory in $maindirectories){

$Sheet.Cells.Item($intRow,1) = $maindirectory.name

$Sheet.Cells.Item($intRow,1).Borders.Linestyle = 1

$Sheet.Cells.Item($intRow,1).Font.Bold=$True

$subpath1 = $path + "\" + $maindirectory.Name



$access1 = Get-Acl $subpath1

$rights = $access1.Access

$intcol = 4

foreach ($item in $rights){

[string]$veld = $item.IdentityReference

$veld = $veld.Replace("AD\","")

$Sheet.Cells.Item($intRow,$intcol) = $veld

$Sheet.Cells.Item($intRow,$intcol).Borders.Linestyle = 1

$intcol++

}





$intRow++

$subdirectories1 = gci $subpath1 | ?{ $_.PSIsContainer }| select name

foreach ($subdirectory1 in $subdirectories1){

$Sheet.Cells.Item($intRow,2) = $subdirectory1.name

$Sheet.Cells.Item($intRow,2).Borders.Linestyle = 1

$subpath2 = $subpath1 + "\" + $subdirectory1.Name

$access1 = Get-Acl $subpath2

$rights = $access1.Access

$intcol = 4

foreach ($item in $rights){

[string]$veld = $item.IdentityReference

$veld = $veld.Replace("AD\","")

$Sheet.Cells.Item($intRow,$intcol) = $veld

$Sheet.Cells.Item($intRow,$intcol).Borders.Linestyle = 1

$intcol++

}

$subdirectories2 = gci $subpath2 | ?{ $_.PSIsContainer }| select name

$intRow++

foreach ($subdirectory2 in $subdirectories2){

if ($subdirectories2) {

$Sheet.Cells.Item($intRow,3) = $subdirectory2.name

$Sheet.Cells.Item($intRow,3).Borders.Linestyle = 1

$Sheet.Cells.Item($intRow,3).Font.Italic=$True

$subpath3 = $subpath2 + "\" + $subdirectory2.Name

$access1 = Get-Acl $subpath3

$rights = $access1.Access

$intcol = 4

foreach ($item in $rights){

[string]$veld = $item.IdentityReference

$veld = $veld.Replace("AD\","")

$Sheet.Cells.Item($intRow,$intcol) = $veld

$Sheet.Cells.Item($intRow,$intcol).Borders.Linestyle = 1

$intcol++

}



$intRow++

}

}

}

$intRow++

}

Remove-PSDrive -Name "Netwerkdrive"

$Sheet.UsedRange.EntireColumn.AutoFit()