Powershell:如何使用select-object获取动态的属性集?

时间:2013-02-06 14:46:23

标签: powershell powershell-v2.0

我正在使用powershell与sharepoint 07一起列出一些东西。我试图让(电源)用户指定他们想要显示的字段。
例如,我可以按如下方式运行我的代码:
。\ psextractor -fields“类型|名称|用户说明
执行此操作后,我将获得一个显示上面列出的字段的文件列表。目前我正在使用Select-Object标识符,我想知道这是否可行。如果没有,有没有办法在不使用create-object cmdlet的情况下执行此操作?
我的代码:

#$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)
$props=@() #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

    }

 }

2 个答案:

答案 0 :(得分:3)

我建议将参数作为普通string[](数组)参数,并使用它来创建哈希表数组(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'

答案 1 :(得分:0)

您可以尝试这样:

param([object[]]$fields)

$fields += "DisplayName"

 $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 $fields
    }

 }

像这样调用你的函数:

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