我试图通过获取用户的 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!
发布评论
评论(3)
如前所述,仅使用
格式 - *
cmdlet产生 for-display output output ,从不输出 data 必须以稍后的方式进行处理。什么格式表
输出是代表格式指令的对象,它是其属性,最终在您的CSV文件中 - 参见 this Answer 有关更多信息。在中,要在CSV输出中包括集合(数组),您必须使用自选择分离器。否则,简单地调用收集对象本身上的 的,该产生了集合的类型名称,并且没有有关其元素的信息。<<<<<<<<<<<< /p>
因此,使用以下内容,以下内容使用
','','
作为分隔符字符串来表示单个列中的组名称和描述:注意:
[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. WhatFormat-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: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 aNew-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 theGroupsDescriptions
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.问题在于,您先通过管道传输到
Format-Table
,然后再通过管道传输到Export-Csv
。仅使用Format-Table
在屏幕上显示内容。解决方法就是删除它。The problem is that you pipe to
Format-Table
before you pipe toExport-Csv
. Only useFormat-Table
for displaying things on screen. The fix is to just remove that.多亏了这篇文章在这里和Mkrement0。我能够找出此问题的格式化部分。
现在,我拥有剩余的代码,可以完全按预期导出。
-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.
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.