Powershell返回对象非cmdlet

时间:2012-03-01 10:32:27

标签: powershell

如何以原生方式返回对象?一直在看它,没有明确,简单的例子!

[编辑]固定!只是我自己解决了,正确的代码如下!

function search-member()
{
$objOU_1 = New-Object System.DirectoryServices.DirectoryEntry("LDAP://dc=somedomain")

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher

$objSearcher.SearchRoot = $objOU_1
$objSearcher.PageSize = 1000
$strFilter = "(&(objectCategory=User)(sAMAccountName=username))"
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$results = $objSearcher.FindAll()

# return $results # instead of this

$results # you first "write" what you want returned
return   # then return
}

$obj1 = search-member-2003 # if you take away the "$obj = " it will spit out the write which u have in the function directly.

$obj1 # here is the boject

1 个答案:

答案 0 :(得分:0)

如果您更改了脚本:

foreach ($result in $results)
{     
    $result        
}

或只是

return $results

然后

$obj1 = search-member
$obj1.gettype()
如果'username'包含像Smith *这样的通配符并且存在于少于两个条目(如'smith'和'smithson'),那么

将给object[] 'SearchResult'这个$obj1[0].gettype()。然后

'SearchResult'$obj1

如果“用户名”是唯一的SearchResult,则类型为“{{1}}”。