2012-03-01 13 views
0

オブジェクトをネイティブで返す方法はありますか?しばらくそれを見てきましたが、明確で簡単な例はありませんでした!Powershell Return Object非コマンドレット

[編集]固定!ちょうど私自身でそれを解決しました、正しいコードは以下です!

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 
+0

私自身の質問にちょうど答えました。 関数に返されたいものをエコー(書き込み)する必要があります。これは変数$ obj1に保存されるため、必要な処理を行うことができます。 –

答えて

0

あなたのスクリプトで変更する場合は、この:

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

か、単に

return $results 

はその後

$obj1 = search-member 
$obj1.gettype() 

'SearchResult'このobject[]を与えます'username'にSmith *のようなワイルドカードが含まれ、2つ以下のエントリ( 'smith'や 'smithson'など)に存在する場合'ユーザ名' はユニーク$obj1されている場合は、

$obj1[0].gettype()'SearchResult'

あるタイプである 'SearchResult'。

関連する問題