向PSObject添加数组元素



前言:我没有受过任何正式的脚本编写训练。所以我相信你们中的许多人都会把头撞在桌子上,想知道我在做什么。

我正在努力收集关于我们当前团队网站目录和各自网站所有者的信息。我有一个PS脚本来收集网站列表,然后它循环通过每个网站来获得所有者列表。在我收集了那个所有者列表(存储在一个var中(之后,我循环遍历那个var以获得用户名,并将每个用户名作为一个新元素存储在数组中。稍后在向PSObject添加新成员时会使用该元素,这样我就可以将结果导出到CSV。以下是一些代码;

#Var's
$Count = 1
$TSO = New-Object PSObject
$SiteOwner = @("SiteOwner0","SiteOwner1","SiteOwner2","SiteOwner3","SiteOwner4","SiteOwner5","SiteOwner6","SiteOwner7") #Array to create column headers in the CSV

#STEP 1: Get a list of the sites
$Sites = get-team | sort-object DisplayName
#STEP 2: Get a list of the owners of the different sites
FOREACH ($i in $Sites){
$h = Get-TeamUser -GroupID $i.GroupID | Where {$_.role -eq "Owner"}
$Count += 1
Write-Host . -ForeGroundColor "Cyan" -NoNewLine
#Add the new columns to $TSO
$TSO | Add-Member -MemberType NoteProperty -Name "SiteName" -Value $i.DisplayName -Force
$TSO | Add-Member -MemberType NoteProperty -Name "GroupOwner" -Value $i.Description -Force
$TSO | Add-Member -MemberType NoteProperty -Name "Visibility" -Value $i.Visibility -Force
$TSO | Add-Member -MemberType NoteProperty -Name "Archived" -Value $i.Archived -Force
$TSO | Add-Member -MemberType NoteProperty -Name "SiteEmail" -Value $i.MailNickname -Force
#loop through each member to discover the assigned role
FOREACH ($o in $h){
Write-Host . -ForeGroundColor "Cyan" -NoNewLine
#Build the dynamics of the owners before passing to $TSO
$OHolder += $o.user
} #END NESTED FOREACH

#Handle BLANK elements in the $OHolder array
#The number 8 is the number of array elements we need a value for
$Comp = 0

While($Comp -lt 8){

If($OHolder[$Comp] -ne $null){
$TSO | Add-Member -MemberType NoteProperty -Name $SiteOwner[$Comp] -Value $OHolder[$Comp] -Force
}
ELSEIF(-not ($OHolder[$Comp])){
$OHolder[$Comp] = "NA"
$TSO | Add-Member -MemberType NoteProperty -Name $SiteOwner[$Comp] -Value $OHolder[$Comp] -Force
}

#Increment the $Comp
$Comp += 1

}#END WHILE
#Resetting the $Comp outside the loop
$Comp = 0
#************* END STEP 2 *************************

#Squirt out the info to a CSV
$TSO | Export-CSV -Path $OPathOut -NoTypeInformation -Append -Force

#Reset the $OHOLDER Array
$OHolder = @()
} #END FOREACH

我的问题是;当将站点所有者添加到站点所有者($TSO | Add-Member -MemberType NoteProperty -Name $SiteOwner[$Comp] -Value $OHolder[$Comp] -Force(的PSObject时,它将值的每个字符作为一个元素进行计数。

例如:$OHolder将具有两个元素,假定$OHolder[0]等于"0";doe@domain.com"并且假定CCD_ 4等于"0";john@domain.com&";。实际情况是数组的长度变为29(对于每个字符(,而不是2。

如何按预期将用户添加到PSObject?现在输出将有:

SiteOwner0 | SiteOwner1 | SiteOwner2
d          | o          | e
@          |d           |o

不要使用+=向数组中添加项。如果阵列变大,效率会很低。每次将当前数组内容读入内存后,都会创建一个新数组。您可以简单地输出foreach内部的每个项,并将数组变量分配给foreach输出。

$OHolder = @(FOREACH ($o in $h){
Write-Host . -ForeGroundColor "Cyan" -NoNewLine
#Build the dynamics of the owners before passing to $TSO
$o.user # Output 
})

在上面的简单示例中,循环可能甚至没有必要,因为在PowerShell v3+中$OHolder = ,$h.user应该足够了。

在使用+=运算符之前,您没有定义$OHolder变量。

在这种情况下,默认情况下powershell似乎定义了一个字符串变量。

$undefVar = $null
$undefVar += "test"
$undefVar += "some"
# content of $undefVar is now testsome

尝试在循环FOREACH ($o in $h){之前移动$OHolder = @()

最新更新