尝试输出自定义 powershell 对象,在其中我可以对齐包含 Category:Description 的两个不同变量的每一行

发布于 2025-01-18 12:07:53 字数 1724 浏览 0 评论 0 原文

我试图通过获取用户的 AD 组及其描述来进行网络访问控制审核,然后按照以下示例所示的方式输出它们:


[用户]

@[1]组:@[1]组描述 @[2]...
@[3]...


以下是我目前所拥有的。

$UserGroups = @{

User = Read-Host -Prompt "What user do You want to look up Access for?"
Groups = (Get-ADUser $User -Properties MemberOf).MemberOf
GroupsDescriptions = (Get-ADUser $User -Properties MemberOf).MemberOf | % {(Get-ADGroup $_ -Properties *).description}
}

$Object = New-Object psobject -Property $UserGroups

$Object | format-table | Export-Csv c:\tmp\test.csv

虽然输出很奇怪。我不明白。以下是 Get-Content C:tmp\test.csv 的结果

#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatStartData “ClassId2e4f51ef21dd47e99d3c952918aff9cd”,“pageHeaderEntry”,“pageFooterEntry”,“autosizeInfo”,“shapeInfo”,“groupingEntry” "033ecb2bc07a4d43b5ef94ed5a35d280",,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo", “9e210fe47d09416682b841769c78b8a3”,,,,, “27c87ef9bbda4f709f6b4002fa4af63c”,,,,, “4ec4f0187cb04f4cb6973460dfe252df”,,,,, “cf522b78d86c486691226b40aa69e95c”,,,,,

我尝试使用 Out-file 输出到 .txt 文件,但我总是在最后用 ... 截断每个属性。在将数据传输到导出行之前格式化数据时,我使用了 -Autosize 和 -Expand 。

任何建议或建议都会非常有帮助。

我稍后会看的事情 遍历 PowerShell 对象中的每一行并提取变量 Powershell 和ActiveDirectory - 尝试输出组中的用户及其成员身份 在尝试输出时输出文件裁剪我的文本一个表 谢谢!

I'm trying to do an network access control audit by grabbing a user's AD groups, their descriptions and then output them in a way shown by this example:


[User]

@[1]Groups : @[1]GroupDescription
@[2]...
@[3]...


Below is what I have at the moment.

$UserGroups = @{

User = Read-Host -Prompt "What user do You want to look up Access for?"
Groups = (Get-ADUser $User -Properties MemberOf).MemberOf
GroupsDescriptions = (Get-ADUser $User -Properties MemberOf).MemberOf | % {(Get-ADGroup $_ -Properties *).description}
}

$Object = New-Object psobject -Property $UserGroups

$Object | format-table | Export-Csv c:\tmp\test.csv

Though the output is very strange. I don't understand it. Below is a result of Get-Content C:tmp\test.csv

#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
"ClassId2e4f51ef21dd47e99d3c952918aff9cd","pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"
"033ecb2bc07a4d43b5ef94ed5a35d280",,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo",
"9e210fe47d09416682b841769c78b8a3",,,,,
"27c87ef9bbda4f709f6b4002fa4af63c",,,,,
"4ec4f0187cb04f4cb6973460dfe252df",,,,,
"cf522b78d86c486691226b40aa69e95c",,,,,

I have tried outputting to a .txt file using Out-file, but I always get each property cut off with a ... at the end. I've used the -Autosize and -Expand when formatting the data before piping it to the export line.

Any Suggestions or advice would be extremely helpful.

Things I'll be Looking at later
Go through each line in PowerShell object and extract variables
Powershell & ActiveDirectory - trying to output users in a group and their membership
Out-file crops my text when trying to output a table
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聽兲甴掵 2025-01-25 12:07:53

因此,使用以下内容,以下内容使用','','作为分隔符字符串来表示单个列中的组名称和描述:

$UserGroups = [pscustomobject] @{
  User = ($user = Read-Host -Prompt "What user do You want to look up Access for?")
  Groups = ($groups = (Get-ADUser $User -Properties MemberOf).MemberOf) -join ', '
  GroupsDescriptions = (
      $groups | ForEach-Object { (Get-ADGroup $_ -Properties *).Description }
    ) -join ', '
}

$UserGroups | Export-Csv c:\tmp\test.csv

注意:

  • [pscustomObobject] @{ ...} 直接用于 构建一个自定义对象,这是句法糖,因为PowerShell V3比 new-object call。

  • 为了使用 read-host 在对象定义的以后属性中调用结果,您必须在AUX中缓存。变量 $ user (请注意,将分配包装在(...)中的分配中。

    将其值传递。

  • 变量 $ user (请注意,将分配包含在(...)中的 /code>在aux中缓存。
  • 但是,AS zett42 指出,使 $ user = ... $ groups = ... 分配单独的语句,并将其放在对象构造之前。

  • As stated, only ever use Format-* cmdlets to produce for-display output, never for outputting data that must be processed programmatically later. What Format-Table outputs are objects representing formatting instructions, and it is their properties that ended up in your CSV file - see this answer for more information.

  • In order to include collections (arrays) in CSV output, you must convert them to a single string, using a self-chosen separator. Otherwise, Export-Csv simply calls .ToString() on the collection object itself, which yields the collection's type name, and no information about its elements.

Therefore, use something like the following, which uses ', ' as the separator string to represent the group names and descriptions in a single column each:

$UserGroups = [pscustomobject] @{
  User = ($user = Read-Host -Prompt "What user do You want to look up Access for?")
  Groups = ($groups = (Get-ADUser $User -Properties MemberOf).MemberOf) -join ', '
  GroupsDescriptions = (
      $groups | ForEach-Object { (Get-ADGroup $_ -Properties *).Description }
    ) -join ', '
}

$UserGroups | Export-Csv c:\tmp\test.csv

Note:

  • [pscustomobject] @{ ... } is used to directly construct a custom object, which is syntactic sugar available since PowerShell v3 that is simpler and more efficient than a New-Object call.

  • In order to use the result from your Read-Host call in later properties of your object definition, you must cache it in aux. variable $user (note that enclosing the assignment in (...) passes its value through.

  • Similarly, the result of the Get-ADUser call is cached in aux. variable $groups, so that it doesn't have to be repeated in the GroupsDescriptions value.

  • However, as zett42 points out, it may be cleaner to make the $user = ... and $groups = ... assignments separate statements and place them before the object construction.

嗳卜坏 2025-01-25 12:07:53

问题在于,您先通过管道传输到 Format-Table,然后再通过管道传输到 Export-Csv。仅使用 Format-Table 在屏幕上显示内容。解决方法就是删除它。

$Object | Export-Csv c:\tmp\test.csv

The problem is that you pipe to Format-Table before you pipe to Export-Csv. Only use Format-Table for displaying things on screen. The fix is to just remove that.

$Object | Export-Csv c:\tmp\test.csv
如果没有 2025-01-25 12:07:53

多亏了这篇文章在这里和Mkrement0。我能够找出此问题的格式化部分。

现在,我拥有剩余的代码,可以完全按预期导出。

$user= Read-Host -Prompt "What user bonehead?"
$object =  Get-ADPrincipalGroupMembership $user

$Table = $object | ForEach-Object {
    [pscustomobject] @{
        Groups = $_.Name
        GroupDesc = (Get-ADGroup $_ -Properties *).Description
        GroupOwner = (Get-ADGroup $_ -Properties *).Info
        }
    }
    

$Table | Export-csv -NoTypeInformation c:\tmp\test.csv

-notypeinformation有助于消除.CSV文件上的标题和通过foreach -Object cmdlet的管道组信息,这有助于确保每个对象在Excel中具有自己的行。

Thanks to this post Here and mklement0. I was able to figure out the formatting portion of this problem.

Now I have the remaining code that exports it exactly as intended.

$user= Read-Host -Prompt "What user bonehead?"
$object =  Get-ADPrincipalGroupMembership $user

$Table = $object | ForEach-Object {
    [pscustomobject] @{
        Groups = $_.Name
        GroupDesc = (Get-ADGroup $_ -Properties *).Description
        GroupOwner = (Get-ADGroup $_ -Properties *).Info
        }
    }
    

$Table | Export-csv -NoTypeInformation c:\tmp\test.csv

The -NoTypeInformation helps eliminate the header on the .csv files and the piped Group info through the ForEach-Object cmdlet helped insure every object had it's own row in excel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文