Audit Folders/Shares and Export to CSV file

# NAME: AuditPerms2CSV.ps1
#
# AUTHOR: OldDog
# DATE : 11/12/2014
#
# COMMENT: Audit Folders/Shares and Export to CSV file
# This saves the results in a directory called C:\powershell\results
# if you don't have that directory it will create one.
# It also deletes any existing copy of Result.csv so if you want to save them rename them.
# ==============================================================================================
$path = $args[0]

if (! $args ) {
$path = Read-Host “Please enter the full path name you wish to check, like this: \\FileServer\share\user”

}

If (Test-path “C:\Powershell\Results”) {
If (Test-path “C:\Powershell\Results\Result.csv”) {
del “C:\Powershell\Results\Result.csv” }
}
Else {
New-Item -Path “C:\Powershell\Results” -ItemType directory
}

Add-Content “C:\Powershell\Results\Result.csv” “Folder Path:, Users/Groups:,Permissions:,Permissions Inherited:”

####Change the path to the folder or share you want NTFS perms on####
$dirToAudit = Get-ChildItem -Path $path -recurse | Where {$_.psIsContainer -eq $true}

foreach ($dir in $dirToAudit)
{
$colACL = Get-Acl -Path $dir.FullName

foreach ($acl in $colACL)
{
If ($acl.Access | ? inheritanceflags -eq ‘none’){
$In = $false } Else {$In = $True }

foreach ($accessRight in $acl.Access)
{
$DirName = $dir.FullName
$ar = $($AccessRight.FileSystemRights)
$id = $($AccessRight.IdentityReference)
Add-Content “C:\Powershell\Results\Result.csv” “$dirName ,$id,””$ar””,$In” #Note the “” to escape the ‘,’ in the permissions
}
$In = $null
}

}

How can I use Windows Powershell to copy Selected CSV Files to one Excel WorkBook?


# Script name: Copy-CsvToExcel.ps1
# Created on: 3/21/2011
# Author: OldDog1
# Purpose: How can I use Windows Powershell to
# copy Selected CSV Files to one Excel WorkBook?
#-----------------------------------------------------
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
#-----------------------------------------------------
$xlPasteValues = -4163          # Values only, not formulas
$xlCellTypeLastCell = 11        # to find last used cell

$xl = new-object -comobject excel.application
$xl.Visible = $True
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Add()
$i = 1
# The Get_ChildItem returns the entire Path (G:\Scrips\myfile.csv)

$collection = Get-ChildItem G:\Scripts\* -include *.csv

$length = 4

foreach ($item in $collection) {
$wb1 = $xl.Workbooks.Open("$item")
$array = $item.ToString()
$delim = "\"
$SheetName = $array.split($delim) # here i split off the G:\scripts\
$s = $SheetName[2]
$sn = $s.split(".") # Here I split off the .csv
$nsn = $sn[0]
$ws1 = $wb1.worksheets | where {$_.name -eq $nsn}
Write-Host $item $nsn # here I make sure I did it right
$used = $ws1.usedRange
$used.Select()
$used.copy()
$wb.Activate()
$ws = $wb.Sheets.Add()
$ws2 = $wb.worksheets | where {$_.name -eq "sheet$i"}
[void]$ws2.Range("A1").PasteSpecial(-4163)
$ws2.name = $nsn # Here I name the new sheet
$i++
$wb1.Close() # Close the .CSV file and move on to the next one
}

# $xl.quit()

# $a = Release-Ref($ws)
# $a = Release-Ref($wb)
# $a = Release-Ref($xl)