Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Powershell对象属性值返回excel单元格_Excel_Vba_Powershell - Fatal编程技术网

如何将Powershell对象属性值返回excel单元格

如何将Powershell对象属性值返回excel单元格,excel,vba,powershell,Excel,Vba,Powershell,因此,我正在运行一个Powershell脚本,该脚本将返回指定文件夹中的所有用户及其对所有共享文件夹的访问权限。我的问题是,我试图让Powershell将信息直接转储到打开的excel文件中。到目前为止,该脚本使用正确的信息将输出运行到CSV文件,但当我试图将其转储到excel中时,它不起作用。这是因为我不知道如何将单元格值设置为对象属性值 这是密码 #Get Folder Permissions $xl = [Runtime.Interopservices.Marshal]::GetActi

因此,我正在运行一个Powershell脚本,该脚本将返回指定文件夹中的所有用户及其对所有共享文件夹的访问权限。我的问题是,我试图让Powershell将信息直接转储到打开的excel文件中。到目前为止,该脚本使用正确的信息将输出运行到CSV文件,但当我试图将其转储到excel中时,它不起作用。这是因为我不知道如何将单元格值设置为对象属性值

这是密码

#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)
#获取文件夹权限
$xl=[Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application'))
$targetWebPath='C:\\test\u dump.xlsx'
$targetWbName=拆分路径$targetWbpath-叶
$targetWB=$xl.Workbook.Item($targetWBName)
$targetWS=$targetWB.Sheets.Item('Test_1'))
$cells=$targetWS.cells
$row=1
$col=1
“用户”、“文件夹”、“访问”| foreach{
$cells.item($row,$col)=$_
$cells.item($row,$col).font.bold=$True
$col++
}
$row=2
获取childitem“”-递归|其中{$\ psicContainer}|
获取Acl |%{
$path=$\路径
$|.访问|%{
新对象PSObject-属性@{
文件夹=$path.Replace(“Microsoft.PowerShell.Core\FileSystem::”,“”)
Access=$\文件系统权限
用户=$\标识引用
} 
$col=1
$cells.item($row,$col)=用户
$col++
$cells.item($row,$col)=文件夹
$col++
$cells.item($row,$col)=访问
$col++
美元行++
}
}
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
如果我能弄清楚如何执行Foreach循环来检索属性值,那就太好了,但我也弄不清楚。仅供参考,这是我的第一个Powershell脚本,如果这不是一个很有挑战性的问题,我深表歉意。 总体目标是从VBA脚本调用它,并将输出放在运行脚本的工作簿中


感谢您的帮助,JD

我对Excel interop的使用不多,但在访问属性时,您需要先将对象分配给变量,然后才能引用其属性:

$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”
我还必须将用户变量放入引号中才能使其工作。原因是PowerShell如何将变量添加到字符串中。如果不需要额外变量,则必须使用Write Host“User:$($AclObj.User)”。看到这个(我在这里问的第一个问题!)了解更多信息:谢谢。这是一个需要记住的非常有用的提示。为什么返回的权限与使用“文件夹属性”显示的“安全”选项卡时看到的权限不同?“同步”权限的作用是什么?