输出到 csv 时的 Powershell 奇怪行为
Powershell odd behaviour when outputting to csv
我在将 foreach 循环输出到 csv 文件时遇到问题。
我的群组是这样设置的:
$Groups = "Group1", "Group2", "Group3"
我的代码是:
$results = ForEach ($Group in $Groups) {
$memberof = get-adgroup $Group | select -expandproperty distinguishedname
Write-Output $Group
Get-ADObject -Filter 'memberof -eq $memberof -and (ObjectClass -eq "user" -or ObjectClass -eq "contact")' -properties * | select name, Objectclass, mail
Write-Output ""
Write-Output ""
}
$results | Export-csv Contacts.csv -NoTypeInformation
问题似乎出在 Write-Output 行,但我不知道为什么。当我 运行 我的代码没有写入 csv 文件时,我得到了预期的结果,比如:
NameOfGroup1
name Objectclass mail
---- ----------- ----
User1 user User1@mail.com
User2 user User2@mail.com
#Spaces caused by write-output ""
NameOfGroup2
User1 user User1@mail.com
Contact1 contact Contact1@externalmail.com
然后,当我 运行 我的代码写入 csv 文件并注释掉 write-output $Group
时,我得到了类似的结果。
但是,如果我 运行 来自本页顶部的完整代码,包括 write-output $Group
,结果如下:
我已经弄清楚这些结果代表什么,但我不知道为什么它们打印成这样。
本质上数字是指组名的长度,所以前17位就是17个字符的组名,下面的行数等于该组内的联系人和用户数.每组末尾的 2 个零是 write-output ""
行的长度。
是什么导致了这种行为?
以下代码将密切输出您正在尝试的内容。
$results = ForEach ($Group in $Groups) {
$memberof = get-adgroup $Group | select -expandproperty distinguishedname
Get-ADUser -Filter "memberof -eq '$memberof' -and (ObjectClass -eq 'user' -or ObjectClass -eq 'contact')" -properties name,ObjectClass,Mail | Select-Object @{n='Group';e={$Group}},name, Objectclass, mail
[pscustomobject]"" | Select-Object Group,Name,ObjectClass,Mail
[pscustomobject]"" | Select-Object Group,Name,ObjectClass,Mail
}
$results | Export-csv Contacts.csv -NoTypeInformation
解释:
Export-Csv
将具有属性的对象或对象数组转换为 CSV 文件。您可以使用 ConvertTo-Csv
在控制台中看到相同的结果。属性被转换为列,属性 值被放置在它们关联的列下。当您输出 Write-Output $Group
中的字符串时,它的 属性 为 Length
。要解决此问题,您需要在 Select-Object
中添加 $Group
作为计算得出的 属性。如果你想在你的 CSV 中做空行,那么你应该输出另一个对象,所有 属性 值作为 ''
.
当您在 PowerShell 输出中混合对象时,您会看到意想不到的结果。您的 Get-ADObject
输出一个自定义对象。您的 Write-Output
行输出一个字符串。这两种对象类型不共享属性。所以你只能看到数组中第一个对象的属性,它是一个字符串。如果将所有 Write-Output
语句放在循环的末尾,您将在 CSV 中看到更多属性。请参阅下面的示例,仅通过反转处理对象的顺序,您会得到不同的结果。
$str = "string"
$obj = [pscustomobject]@{property1 = "value1"; property2 = "value2"}
$str,$obj | convertto-csv -notype
"Length"
"6"
$obj,$str | convertto-csv -notype
"property1","property2"
"value1","value2"
,
注意自定义对象 $obj
和字符串 $str
.
可用的属性
$obj | get-member -Type Properties
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
property1 NoteProperty string property1=value1
property2 NoteProperty string property2=value2
$str | get-member -Type Properties
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Length Property int Length {get;}
我在将 foreach 循环输出到 csv 文件时遇到问题。
我的群组是这样设置的:
$Groups = "Group1", "Group2", "Group3"
我的代码是:
$results = ForEach ($Group in $Groups) {
$memberof = get-adgroup $Group | select -expandproperty distinguishedname
Write-Output $Group
Get-ADObject -Filter 'memberof -eq $memberof -and (ObjectClass -eq "user" -or ObjectClass -eq "contact")' -properties * | select name, Objectclass, mail
Write-Output ""
Write-Output ""
}
$results | Export-csv Contacts.csv -NoTypeInformation
问题似乎出在 Write-Output 行,但我不知道为什么。当我 运行 我的代码没有写入 csv 文件时,我得到了预期的结果,比如:
NameOfGroup1
name Objectclass mail
---- ----------- ----
User1 user User1@mail.com
User2 user User2@mail.com
#Spaces caused by write-output ""
NameOfGroup2
User1 user User1@mail.com
Contact1 contact Contact1@externalmail.com
然后,当我 运行 我的代码写入 csv 文件并注释掉 write-output $Group
时,我得到了类似的结果。
但是,如果我 运行 来自本页顶部的完整代码,包括 write-output $Group
,结果如下:
我已经弄清楚这些结果代表什么,但我不知道为什么它们打印成这样。
本质上数字是指组名的长度,所以前17位就是17个字符的组名,下面的行数等于该组内的联系人和用户数.每组末尾的 2 个零是 write-output ""
行的长度。
是什么导致了这种行为?
以下代码将密切输出您正在尝试的内容。
$results = ForEach ($Group in $Groups) {
$memberof = get-adgroup $Group | select -expandproperty distinguishedname
Get-ADUser -Filter "memberof -eq '$memberof' -and (ObjectClass -eq 'user' -or ObjectClass -eq 'contact')" -properties name,ObjectClass,Mail | Select-Object @{n='Group';e={$Group}},name, Objectclass, mail
[pscustomobject]"" | Select-Object Group,Name,ObjectClass,Mail
[pscustomobject]"" | Select-Object Group,Name,ObjectClass,Mail
}
$results | Export-csv Contacts.csv -NoTypeInformation
解释:
Export-Csv
将具有属性的对象或对象数组转换为 CSV 文件。您可以使用 ConvertTo-Csv
在控制台中看到相同的结果。属性被转换为列,属性 值被放置在它们关联的列下。当您输出 Write-Output $Group
中的字符串时,它的 属性 为 Length
。要解决此问题,您需要在 Select-Object
中添加 $Group
作为计算得出的 属性。如果你想在你的 CSV 中做空行,那么你应该输出另一个对象,所有 属性 值作为 ''
.
当您在 PowerShell 输出中混合对象时,您会看到意想不到的结果。您的 Get-ADObject
输出一个自定义对象。您的 Write-Output
行输出一个字符串。这两种对象类型不共享属性。所以你只能看到数组中第一个对象的属性,它是一个字符串。如果将所有 Write-Output
语句放在循环的末尾,您将在 CSV 中看到更多属性。请参阅下面的示例,仅通过反转处理对象的顺序,您会得到不同的结果。
$str = "string"
$obj = [pscustomobject]@{property1 = "value1"; property2 = "value2"}
$str,$obj | convertto-csv -notype
"Length"
"6"
$obj,$str | convertto-csv -notype
"property1","property2"
"value1","value2"
,
注意自定义对象 $obj
和字符串 $str
.
$obj | get-member -Type Properties
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
property1 NoteProperty string property1=value1
property2 NoteProperty string property2=value2
$str | get-member -Type Properties
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Length Property int Length {get;}