从Powershell到逗号分隔字符串的结果

从Powershell到逗号分隔字符串的结果,powershell,Powershell,我运行下面的脚本,如何将输出放入逗号分隔的字符串中 Get-ADGroup -filter * -properties GroupCategory | ft objectguid, samaccountname 电流输出 objectguid samaccountname ---------- ---

我运行下面的脚本,如何将输出放入逗号分隔的字符串中

Get-ADGroup -filter * -properties GroupCategory | ft objectguid, samaccountname
电流输出

objectguid                                                  samaccountname
----------                                                  --------------
f5b71a40-9405-4874-b9b0-b35e45346f63                        WinRMRemoteWMIUsers__
95a99aa1-a771-4f86-bdce-7a6a3f00057a                        Administrators
6a6ad877-180c-462b-9672-bfba20200cda                        Users
038b03cf-1171-4546-b0af-a7654823e289                        Guests
ed3995ae-6f77-492f-bcb5-bc197fa24996                        Print Operators
b1f8301a-78e1-47ab-9e51-8e55525acadf                        Backup Operators
3d23c4c0-71d9-4e08-a8ee-a9692a6ed61f                        Replicator
d86e5619-5893-41b7-b3a8-eda4c2725562                        Remote Desktop Users
473592e4-71c8-458f-b7fc-b474809933ff                        Network Configuration Operators
c433a049-f9e5-404a-9940-5cf1f8ce4d82                        Performance Monitor Users
1ce2daf3-5d97-4493-9ec1-c84a49326113                        Performance Log Users
24b06a4c-f32b-4081-a002-6f1851f98256                        Distributed COM Users
期望输出

f5b71a40-9405-4874-b9b0-b35e45346f63,WinRMRemoteWMIUsers__,95a99aa1-a771-4f86-bdce-7a6a3f00057a,Administrators,6a6ad877-180c-462b-9672-bfba20200cda,Users,038b03cf-1171-4546-b0af-a7654823e289,Guests,ed3995ae-6f77-492f-bcb5-bc197fa24996,Print Operators,b1f8301a-78e1-47ab-9e51-8e55525acadf,Backup Operators,3d23c4c0-71d9-4e08-a8ee-a9692a6ed61f,Replicator,d86e5619-5893-41b7-b3a8-eda4c2725562,Remote Desktop Users,473592e4-71c8-458f-b7fc-b474809933ff,Network Configuration Operators,c433a049-f9e5-404a-9940-5cf1f8ce4d82,Performance Monitor Users,1ce2daf3-5d97-4493-9ec1-c84a49326113,Performance Log Users,24b06a4c-f32b-4081-a002-6f1851f98256,Distributed COM Users

我将它传递到一个字符串中,并使用JavaScript远程解析数据

这相当简单,包括两个步骤:首先用逗号分隔GUID和帐户名:

$items = Get-ADGroup -filter * -properties GroupCategory |
  select objectguid,samaccountname
$combined = $items |
  ForEach-Object { $_.objectguid + ',' + $_.samaccountname }
$result = $separated -join ','
然后用逗号将所有这些字符串连接成一个字符串:

$result = $combined -join ','
我们也可以采用不同的方法:首先将对象分解为两个字符串:

$separated = $items | % { $_.objectguid; $_.samaccountname }
然后用逗号将所有这些连接起来:

$items = Get-ADGroup -filter * -properties GroupCategory |
  select objectguid,samaccountname
$combined = $items |
  ForEach-Object { $_.objectguid + ',' + $_.samaccountname }
$result = $separated -join ','
或者利用
$OFS
变量,该变量指定用什么分隔符数组转换为字符串。同样的开始,我们需要一个数组,其中包含所有应该用逗号连接的内容,然后设置
$OFS
变量并将数组转换为字符串:

$OFS = ','
$result = [string] $separated