【发布时间】:2014-03-21 20:49:32
【问题描述】:
所以我正在运行一个 Powershell 脚本,它将返回指定文件夹中所有共享文件夹的所有用户及其访问权限。我的问题是,我试图让 Powershell 将信息直接转储到打开的 excel 文件中。到目前为止,脚本运行输出到具有正确信息的 CSV 文件,但是当我尝试将其转储到 excel 中时,它不起作用。这是因为我不知道如何将单元格值设置为 Object 属性值。
这是代码
#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)
如果我能弄清楚如何执行 Foreach 循环来检索属性值,那就太好了,但我也想不通。仅供参考,这是我的第一个 Powershell 脚本,所以如果这是一个不那么具有挑战性的问题,我深表歉意。 总体目标是从 VBA 脚本调用它,并将输出放在运行脚本的工作簿中。
谢谢你的帮助,京东
【问题讨论】:
标签: excel vba powershell