Get-SMBShare 将该输出保存到 CSV

Get-SMBShare save that output to a CSV

我想将输出导出为 CSV。但是我无法正确导出。

foreach ($share in (Get-SmbShare)) {
    $share | Get-SmbShareAccess | % {
        $_ |
            Add-Member -Name 'Path' -Value $share.Path -MemberType NoteProperty -PassThru |
            Select Name, Path, AccountName, AccessControlType, AccessRight
    }
}

foreach(){...} 循环的输出分配给一个变量,然后将其通过管道传递给 Export-Csv:

$shareAccess = foreach ($share in (Get-SmbShare)) {
    $share | Get-SmbShareAccess | % {
        $_ |
            Add-Member -Name 'Path' -Value $share.Path -MemberType NoteProperty -PassThru |
            Select Name, Path, AccountName, AccessControlType, AccessRight
    }
}

$shareAccess |Export-Csv .\path\to\output.csv -NoTypeInformation

我觉得是这样的

 Get-SmbShare | 
    ForEach-Object { 
        $share = $_
        return $share | Get-SmbShareAccess | 
            ForEach-Object {
                return [PSCustomObject]@{Share=$share; Access=$_}
            }
    } |
    ForEach-Object {
        return [PSCustomObject]@{
            Name = $_.Share.Name
            Path = $_.Share.Path
            AccessName = $_.Access.AccountName
            AccessType = $_.Access.AccessControlType
            AccessRight = $_.Access.AccessRight
        }
    } |
    Out-GridView