2016-04-22 175 views
2

我試圖將發現here的腳本改爲與PowerShell CSOM等效。在屬性中使用散列表鍵

function setFieldVisibility(listTitle,fieldName,properties,success,failure) 
{ 
    var ctx = SP.ClientContext.get_current(); 
    var web = ctx.get_web(); 
    var list = web.get_lists().getByTitle(listTitle); 
    var field = list.get_fields().getByTitle(fieldName); 
    field.setShowInDisplayForm(properties.ShowInDisplayForm); 
    field.setShowInNewForm(properties.ShowInNewForm); 
    field.setShowInEditForm(properties.ShowInEditForm); 
    field.set_hidden(properties.Hidden); 
    field.update(); 
    ctx.executeQueryAsync(success,failure); 
} 

在推廣它的過程中,我試圖在哈希表中通過。我想迭代這個散列表來動態地構建要編輯的屬性,但是我遇到了一些麻煩。我的功能和用法如下:

Function Set-FieldProperties{ 

    param(
     [Parameter(Mandatory=$true)][string]$Url, 
     [Parameter(Mandatory=$true)][string]$ListTitle, 
     [Parameter(Mandatory=$true)][string]$FieldName, 
     [Parameter(Mandatory=$true)][hashtable]$Properties 
    ) 
    begin { 
     $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url) 
     $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password) 
    } 
    process{ 

     $web = $context.Web 
     $list = $web.Lists.GetByTitle($ListTitle) 
     $field = $list.Fields.GetByTitle($FieldName) 
     foreach($key in $Properties.Keys){ 
      $key 
      $Properties[$key] 
      #failing to make this part dynamic 
      $field.$key($Properties[$key]) 

     } 
     <#$field.ShowInDisplayForm($properties.ShowInDisplayForm) 
     $field.ShowInNewForm($properties.ShowInNewForm) 
     $field.ShowInEditForm($properties.ShowInEditForm) 
     $field.Hidden($properties.Hidden) 
     try{ 
      $field.update() 
      $context.executeQuery() 
      Write-Host -ForegroundColor Green "Field properties updated successfully" 
     } 
     catch{ 
      Write-Host -ForegroundColor Red $_.Exception.Message 
     } 
     #> 
    } 
    end{ 
    $context.Dispose() 
    } 

} 
Set-FieldPoperties -Url "https://tenant.sharepoint.com/teams/eric" -ListTitle "CalcColumns" -FieldName "Title" -Properties @{"SetShowInDisplayForm"=$true; "SetShowInEditForm"=$false} 

我的奮鬥是如何讓這部分建立所需的輸出:

foreach($key in $Properties.Keys){ 
       $key 
       $Properties[$key] 
       #failing to make this part dynamic 
       $field.$key($Properties[$key]) 

      } 

如何使用密鑰作爲一個屬性上設置在這種情況下$ field對象?那可能嗎?我不希望有一些巨大的if塊檢查所有可能的屬性,我希望它根據用戶在$ Properties散列表中傳遞的內容來構建這些屬性。

回答

1

您可以使用Invoke-Expression來創建一個動態命令,但我們需要轉義一些變量,以便它們在執行前不會展開。

@ avvi的解決方案的問題是$Properties[$key]將返回$true,但由於它是字符串中的子表達式,因此它將轉換爲True。當它被執行時,它將是一個不帶引號的字符串,它使得它可以調用函數/ cmdlet /程序名稱。這會引發錯誤,因爲名爲True的方法不存在等。

"`$field.$key($($Properties[$key]))" 
#Returns: 
$field.test(True) 

#ERROR!! 
At line:17 char:13 
+ $field.test(True) 
+    ~ 


"`$field.$key(`$Properties[`$key])" 
#Returns: 
$field.test($Properties[$key]) 

#Good. Will get value from Properties on execution => Object will not be converted to string. 
#This allow both $key and returned value from $Properties to be any type of object 

嘗試:

## START Sample data 
$field = New-Object psobject 
Add-Member -InputObject $field -MemberType ScriptMethod -Name "test" -Value { param($in) $in.Gettype() | Out-Host; "in = $in" } 

$Properties = @{ "test" = $true } 
## END Sample data 


foreach($key in $Properties.Keys){ 
    $key 
    $Properties[$key] 
    #failing to make this part dynamic 
    Invoke-Expression "`$field.$key(`$Properties[`$key])" 
} 

輸出:

#From $key and $Properties[$key] 
test 
True 

#From invoke-expression result 
IsPublic IsSerial Name BaseType   
-------- -------- ---- --------   
True  True  Boolean System.ValueType                     

in = True 
+0

就是這樣,謝謝。 –

1

你可以使用

Invoke-Expression 

這將動態評估的字符串。

因此,這將是這樣的:

iex "`$field.$key($($Properties[$key]))" 
  • 雙引號允許封閉變量的評價
  • 反引號逃脫$場參數,這樣它的輸出字面上
  • $鍵評估作爲$ key的值
  • $()將使所包含的表達式評估
+0

這是非常接近,除了布爾沒有過來爲$ true或$ false,因爲我想我需要的函數來執行。我需要什麼或如何適應這種情況? –