0

Im attempting to learn powerscript and was wondering what i'm doing wrong here >

Foreach($result in $results){
$userEntry = $result.GetDirectoryEntry()

$row = $table.NewRow()
 $row.Name = $userEntry.displayName
 $row.UserID = $userEntry.sAMAccountName
 $row.Department = $userEntry.department

 $table.Rows.Add($row)
}

What i want is for the userdata to output into the proper cells of my table, but what i'm getting is 'System.DirectoryServices.PropertyValueCollection" repeated in each cell.

when i use the same format but output with

Foreach($result in $results){
$userEntry = $result.GetDirectoryEntry()
Write-Output('' + "Name: " + $userEntry.displayName + "`r`n" + " " +"AccountInfo:"   +$userEntry.sAMAccountName + "`r`n" + " " + "Dept: " + $userEntry.department)

}

It's correct

what's the difference with table information?

Thanks everyone, i'm now looking to add this information to a worksheet, have it currently as:

#Excel worksheet
$objExcel = new-object -comobject excel.application 
$objExcel.Visible = $True 
$objWorkbook = $objExcel.Workbooks.Add() 
$objWorksheet = $objWorkbook.Worksheets.Item(1)

but now how would I get my foreach statement values to print onto the excel worksheet, or onto the workbook that is created with the statement??

4

2 回答 2

3

尝试使用 value 属性

$user.propertyName.value

于 2012-05-17T17:26:57.130 回答
2

我想说答案在于您的 $user.Foo 类型,以及 PowerShell 处理集合的方式。相比:

$user.Foo | Get-Member

, $user.Foo | Get-Member

因为 $user.Foo看起来像一个标量,但实际上是一个集合,所以 $row.Foo = $user.Foo 将显示类型而不是值。

你会怎么解决?两种选择:

$row.Foo = -join $user.Foo

或者

$row.Foo = $user.Foo[0]

HTH 巴特克

于 2012-05-17T17:44:37.097 回答