2013-02-06 22 views
2

我正在使用powershell與sharepoint 07一起列出一些東西。我試圖讓一個(權力)用戶指定他們想要顯示哪些字段。
例如,我可以運行如下我的代碼:
\ psextractor -fields「類型|名稱|用戶說明
這樣做後,我會得到顯示上面列出的域目前我使用的文件的列表。選擇-對象標識符,我想知道,如果這是可能的,如果沒有,有沒有辦法做到這一點不使用創建對象cmdlet
我的代碼:?Powershell:如何使用select-object來獲取一組動態屬性?

#$Args 
if($Args[0] -eq "-fields" -and $Args.Count -ge 2){ 
    $flds = $Args[1].split("|") 
} 

#Later in code 
$web.Lists | foreach{ 
    $lib = $_ 
    if($lib.BaseType -eq [Microsoft.SharePoint.SPBaseType]::DocumentLibrary 
      -and $lib.BaseTemplate -eq [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary){ 
     $lib.Items | Select-Object DisplayName, 
        @{n=$flds[0];e={$_.Item($flds[0])}} , 
        @{n=$flds[1];e={$_.Item($flds[1])}} 
        #, etc, etc 

    } 

} 

編輯: 我用下面有一些調整Graimer的解決方案

SOLUTION:

param([object[]]$flds) 
[email protected]() #globally declared since some of this is done in functions later 

$mflds = $("Author","Created","Modified","Modified By") #mandatory fields 
$mflds | foreach{ 
    if($flds -notcontains $_){ 
     $flds += $_ 
    } 
} 
#had to use regular for loop because the $_ identifier was conflicting 
for ($i =0; $i -lt $flds.Count; $i++) { 
    $props += @{n=$flds[$i];e=([Scriptblock]::Create("`$_[`$flds[$i]]"))} 
} 
#other mandatory custom fields 
    #the create method could have been used here 
$props += @{n="FileName";e={"$($_.Item('Name'))"}} 
$props += @{n="Url";e={"$wburl/$($_.Url)"}} 

#Later in code 
$web.Lists | foreach{ 
    $lib = $_ 
    if($lib.BaseType -eq [Microsoft.SharePoint.SPBaseType]::DocumentLibrary 
      -and $lib.BaseTemplate -eq [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary){ 
     $lib.Items | Select-Object -property $props 

    } 

} 

回答

3

我建議採取在paramters作爲一個正常的string[](Array)參數,並用它來創建一個哈希表數組(自定義表達式Select-Object)。然後用Select-Object提供散列表。例如:

param (
    [String[]]$Fields 
) 

#Create property-array for Select-Object 
$props = @() 

#Add mandatory displayname property 
$props += @{n="DisplayName";e=([Scriptblock]::Create("`$_.DisplayName"))} 

#Add user-defined fields 
foreach ($field in $Fields) { 
    $props += @{n=$field;e=([Scriptblock]::Create("`$_.Item($field)"))} 
} 

#Later in code 
$web.Lists | foreach{ 
    $lib = $_ 
    if($lib.BaseType -eq [Microsoft.SharePoint.SPBaseType]::DocumentLibrary ` 
    -and $lib.BaseTemplate -eq [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary) 
    { 
     $lib.Items | Select-Object -Property $props 
    } 

} 
#Usage: .\psextractor -Fields "Type", "Name", "User", "Desc" 
#This will list all fields specified after '-Fields' 
+0

順便說一句,如果人們會低估我的答案,我將非常感激評論,說明我應該改進什麼。這是一個社區,我們應該 –

+0

我希望我沒有不小心投票給你,因爲你的帖子肯定有幫助,但正如我在其他答案中所述,你不能以這種方式獲得這些屬性,唯一的辦法是他們可以訪問是通過一個方法或使用散列,即:@ {$ _ [「my fields」]}或@ {$ _ get(「My Field」)}。任何建議,請分享。 – Realistic

+0

嘗試我的更新回答:) –

0

你可以嘗試這樣的:

​​

調用你的函數是這樣的:

myfunction.ps1 -fields Type,name,User,Description 
+0

這不起作用,因爲屬性存儲在被選中的對象內。這就是爲什麼我必須使用$ _。Item(「我需要的字段」)。同樣,他們可以像這樣訪問:$ itm = $ _; $ itm [「我需要的字段」]。我不確定SP如何做到這一點,但它是一個痛苦:( – Realistic