【问题标题】:How to return a Powershell Object Property Value to an excel Cell如何将 Powershell 对象属性值返回到 Excel 单元格
【发布时间】: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


    【解决方案1】:

    我对 Excel 互操作的工作不多,但就访问属性而言,您需要先将对象分配给一个变量,然后才能引用其属性:

    $aclObj = New-Object PSObject -Property @{
      Folder = $path.Replace("Microsoft.PowerShell.Core\FileSystem::","")
      Access = $_.FileSystemRights
      User = $_.IdentityReference
    } 
    

    那么,访问它们很简单:

    $col = 1
    $cells.item($row,$col) = $aclObj.User
    $col++
    $cells.item($row,$col) = $aclObj.Folder
    $col++
    $cells.item($row,$col) = $aclObj.Access
    $col++
    

    或者,你真的需要创建一个对象吗?如果这就是您要做的全部,为什么不直接将您想要的值分配给 Excel?

    $_.Access | % {
      $col = 1
      $cells.item($row,$col) = $_.IdentityReference
      $col++
      $cells.item($row,$col) = $path.Replace("Microsoft.PowerShell.Core\FileSystem::","")
      $col++
      $cells.item($row,$col) = $_.FileSystemRights
      $row++
    }
    

    【讨论】:

    • KevinD,感谢您的回复。我创建了 AclObj 变量来访问属性。但是,当我检查用户属性 Write-Host "User : $AclObj.User" 时,显示了 AclObj 的所有属性,然后当它转储到 excel 文件中时,所有属性都放置在单元格中。
    • 我明白了。我像你说的那样为对象创建了一个变量。但由于某种原因,我必须创建一个变量$User = $AclObj.User 才能将确切的属性放入单元格中。单元格代码是 $Cells.item($row,$col) = "$User" 我还必须将 User 变量放在引号中才能使其工作。
    • 之所以如此,是因为PowerShell是如何给字符串添加变量的。如果您不想要额外的变量,则必须使用 Write-Host "User : $($AclObj.User)"。看到这个(我在这里问过的第一个问题!)了解更多信息:stackoverflow.com/questions/13615676/…
    • 谢谢。这是一个非常有用的提示。
    • 为什么返回的权限与使用文件夹属性显示的安全选项卡时看到的权限不同?同步权限有什么作用?
    猜你喜欢
    • 2011-07-15
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多