简体   繁体   English

Powershell - 多个 AD 命令,导出到多列 csv

[英]Powershell - Multiple AD Commands, exporting into multiple columns csv

My goal is to take multiple PS commands, export the results into individual columns.There are no relations between the data.我的目标是采取多个PS命令,将结果导出到单独的列中。数据之间没有关系。 Currently I run 15-20 individual PS scripts that each output one column of data into their own csv file.目前我运行 15-20 个单独的 PS 脚本,每个脚本将一列数据输出到自己的 csv 文件中。 I then take each of those 15-20 csv documents and copy/paste their individual columns into a new, single spreadsheet--into their own columns.然后,我将这 15-20 个 csv 文档中的每一个都复制/粘贴到一个新的单一电子表格中——复制/粘贴到他们自己的列中。 I am trying to make this process more efficient by running one script that generates one csv file that contains multiple columns with their respected results.我试图通过运行一个脚本来提高这个过程的效率,该脚本生成一个 csv 文件,该文件包含多列及其受尊重的结果。

Here is what my final output should look like:这是我的最终输出应该是这样的:

Here is what my current output looks like这是我当前的输出

Here is my current rendition of the code.这是我目前的代码版本。 I have tried many variations although this is the closest I've been to a working product.我尝试了许多变体,尽管这是我最接近工作产品的方法。

$ExportPath="\\NetworkLocation\test.csv"
$ADSG1="AD_Security_Group_Name"
$OUpath='OU=Servers,DC=sample,DC=sample,DC=org'

$TST_Script1=Get-ADGroupMember $ADSG1 | Select-object -ExpandProperty Name | Out-String
$TST_Script2=Get-ADComputer -Filter * -SearchBase $OUpath | Select-object -ExpandProperty Name | Out-String

$Object = [pscustomobject]@{
    TST_Script1 = $TST_Script1
    TST_Script2 = $TST_Script2
}
$Object | Export-Csv -Path $ExportPath -NoTypeInformation

You can do the following:您可以执行以下操作:

$TST_Script1=Get-ADGroupMember $ADSG1 | Select-object -ExpandProperty Name
$TST_Script2=Get-ADComputer -Filter * -SearchBase $OUpath | Select-object -ExpandProperty Name

0..(($TST_Script1.Count,$TST_Script2.Count | Sort -Desc)[0]-1) | Foreach-Object {
    [pscustomobject]@{
        TST_Script1 = $TST_Script1[$_]
        TST_Script2 = $TST_Script2[$_]
    }
} | ConvertTo-Csv -NoType

Export-Csv converts an input object's properties into column headers. Export-Csv将输入对象的属性转换为列标题。 The values of those properties are added as data rows aligning with the headers.这些属性的值添加为与标题对齐的数据行。 When piping in a collection, each object in the collection is converted.在集合中进行管道传输时,集合中的每个对象都会被转换。

The issue with your attempt is you are outputting one object with two properties.您尝试的问题是您正在输出一个具有两个属性的对象。 Each of those properties then contains an array of values.这些属性中的每一个都包含一个值数组。 That is different than outputting multiple objects with each having two properties and those properties contain singular values.这与输出多个对象不同,每个对象具有两个属性,并且这些属性包含奇异值。

Out-String muddies things even more by taking an array output and converting it to a single string preserving the formatted array output. Out-String通过获取数组输出并将其转换为保留格式化数组输出的单个字符串使事情更加混乱。


As an aside, doing it the way you want does not make much sense to me.顺便说一句,按照你想要的方式做对我来说没有多大意义。 Both $TST_Script1 and $TST_Script2 seem disjointed as in they don't depend on each other and could contain different object types. $TST_Script1$TST_Script2似乎是脱节的,因为它们不相互依赖并且可以包含不同的对象类型。 Typically, you would want your data rows to represent objects that share schema or object type and contain properties that describe each of those objects.通常,您希望数据行表示共享架构或对象类型的对象,并包含描述每个对象的属性。 Often, data from different objects or commands is used to create properties for a current object.通常,来自不同对象或命令的数据用于为当前对象创建属性。 But the purpose is to better describe the object you are outputting.但目的是更好地描述您正在输出的对象。 Your objective is to take two different, independent properties about two different objects and mash them together to appear as one object.您的目标是获取关于两个不同对象的两个不同的独立属性,并将它们混合在一起以显示为一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM