SharePoint 2013: Como crear, actualizar y borrar property bags con PowerShell (II)!

Siguiendo con la serie de posts sobre como trabajar con Property Bags de SharePoint a nivel de PowerShell, en esta ocasión os dejo como crear, actualizar y borrar Property Bags a nivel de granja…como veréis, el mecanismo es similar sólo que en este caso jugamos con el miembro Properties de SPFarm en lugar de AllProperties. Como siempre, podéis descargaros el script de la galería de scripts de TechNet en el siguiente enlace: How to create, read, update and remove a farm level property bag

   1: ############################################################################################################################################

   2: # This script allows to work with SharePoint property bags at the Site Collection Level

   3: # Required Parameters: 

   4: #    ->$sOperationType: Operation type to be done with the property bag - Create - Update - Delete.

   5: #    ->$sPropertyBagKey: Key for the property bag to be added.

   6: #    ->$sPropertyBagValue: Value for the property bag addded.

   7: ############################################################################################################################################

   8:  

   9: If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 

  10: { Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }

  11:  

  12: $host.Runspace.ThreadOptions = "ReuseThread"

  13:  

  14: #

  15: #Definition of the function that allows to create, update and remove a property bag

  16: #

  17: function WorkWithFarmPropertyBags

  18: {

  19:     param ($sOperationType,$sPropertyBagKey,$sPropertyBagValue)

  20:     try

  21:     {

  22:         $spfFarm=Get-SPFarm        

  23:         switch ($sOperationType) 

  24:         { 

  25:         "Create" {

  26:             Write-Host "Adding Property Bag $sPropertyBagKey to the farm!!" -ForegroundColor Green                        

  27:             $spfFarm.Properties.Add($sPropertyBagKey,$sPropertyBagValue)           

  28:             $spfFarm.Update()            

  29:             $sPropertyBag=$spfFarm.Properties[$sPropertyBagKey]

  30:             Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green

  31:             } 

  32:         "Read" {

  33:             Write-Host "Reading Property Bag $sPropertyBagKey" -ForegroundColor Green                 

  34:             $sPropertyBag=$spfFarm.Properties[$sPropertyBagKey]

  35:             Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green

  36:             }

  37:         "Update" {

  38:             $sPropertyBag=$spfFarm.Properties[$sPropertyBagKey]

  39:             Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green

  40:             Write-Host "Updating Property Bag $sPropertyBagKey in the farm" -ForegroundColor Green            

  41:             $spfFarm.Properties[$sPropertyBagKey]="SPSiteColPBagUpdatedValue_2"    

  42:             $spfFarm.Update()                    

  43:             $sPropertyBag=$spfFarm.Properties[$sPropertyBagKey]

  44:             Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green

  45:             } 

  46:         "Delete" {

  47:             Write-Host "Removing Property Bag $sPropertyBagKey" -ForegroundColor Green                                    

  48:             $spfFarm.Properties.Remove($sPropertyBagKey)            

  49:             $spfFarm.Update()            

  50:             $sPropertyBag=$spfFarm.Properties[$sPropertyBagKey]

  51:             Write-Host "Property bag $sPropertyBagKey has the value $sPropertyBag" -ForegroundColor Green

  52:             }           

  53:         default {

  54:             Write-Host "Requested Operation not valid!!" -ForegroundColor DarkBlue                  

  55:             }

  56:         }        

  57:     }

  58:     catch [System.Exception]

  59:     {

  60:         write-host -f red $_.Exception.ToString()

  61:     }

  62: }

  63:  

  64: Start-SPAssignment –Global

  65: #Calling the function

  66: $sPropertyBagKey="<PropertyBagKey>"

  67: $sPropertyBagValue="<PropertyBagValue>"

  68: WorkWithFarmPropertyBags -sOperationType "Create" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue

  69: WorkWithFarmPropertyBags -sOperationType "Read" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue

  70: #WorkWithFarmPropertyBags -sOperationType "Update" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue

  71: #WorkWithFArmPropertyBags -sOperationType "Delete" -sPropertyBagKey $sPropertyBagKey -sPropertyBagValue $sPropertyBagValue

  72: Stop-SPAssignment –Global

  73:  

  74: Remove-PSSnapin Microsoft.SharePoint.PowerShell