So I am running a Powershell script that will return the ALL the Users and their Access Permissions for all Shared Folders with in a Specified Folder. My problem is, I am trying to get Powershell to dump the info directly into an open excel file. So far the script runs outputs to a CSV file with the correct info, but when when I try to get it to dump into the excel it doesn't work. his is because I can not figure out how to set the cell value to the Object property value.
Here is the Code
#Get Folder Permissions
$xl = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
$targetWBpath = 'C:\<Path>\test_dump.xlsx'
$targetWbName = Split-Path $targetWbpath -Leaf
$targetWB = $xl.Workbooks.Item($targetWBName)
$targetWS = $targetWB.Sheets.Item('Test_1')
$cells = $targetWS.Cells
$row = 1
$col = 1
"User","Folder","Access" | foreach {
$cells.item($row,$col)=$_
$cells.item($row,$col).font.bold=$True
$col++
}
$row = 2
Get-childitem "<Path to Target Folder>" -recurse | where{$_.psiscontainer} |
Get-Acl | % {
$path = $_.Path
$_.Access | % {
New-Object PSObject -Property @{
Folder = $path.Replace("Microsoft.PowerShell.Core\FileSystem::","")
Access = $_.FileSystemRights
User = $_.IdentityReference
}
$col = 1
$cells.item($row,$col) = User <How do I get the Property Value Here for the User property>
$col++
$cells.item($row,$col) = Folder <How do I get the Property Value Here for the Folder property>
$col++
$cells.item($row,$col) = Access <How do I get the Property Value Here for the Access property>
$col++
$row++
}
}
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
It would be nice if I could figure out how to do a Foreach loop to retrieve the property values , but I couldn't figure that out either. FYI this is my first Powershell script so I apologize if this is a not so challenging question. Overall Goal is to call this from a VBA script and have the output placed in the Workbook the script is running from.
Thank you for your help, JD