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

Siguiendo con la serie de scripts sobre como crear, actualizar y borrar property bags con PowerShell, en esta ocasión os dejo un script que permite crear y actualizar property bags en distintos ámbitos (Granja y Colección de Sitios) leyendo toda la información (Ámbito, Clave para la property bag y valor) de un archivo .csv. Antes de empezar os dejo las referencias a los posts previos de esta serie:

Para que el script os funcione sin problemas, tenéis que crear un CSV con la siguiente estructura:

image

Y el script en sí, que podéis descargaros de , es el siguiente: How to create and update property bags from a CSV file

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

   2: # This Script allows to create & update property bags at different scopes

   3: # Required parameters

   4: #    ->$sInputFile: CSV file that stores the property bags to be created / updated

   5: #    ->$sSiteCollectionUrl: Site Collection where Property Bags are created / updated.

   6: ############################################################################################################################################

   7:  

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

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

  10:  

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

  12:  

  13: #

  14: #Function that reads the CSV file containing the Property Bags

  15: #

  16: function ReadPropertyBagsFromFile

  17: {

  18:     param ($sInputFile,$sSiteCollectionUrl) 

  19:     try

  20:     {

  21:         $bFileExists = (Test-Path $sInputFile -PathType Leaf) 

  22:         if ($bFileExists) { 

  23:             "Loading $sInputFile for processing..." 

  24:             $tblData = Import-CSV $sInputFile            

  25:         } else { 

  26:             Write-Host "File $sInputFile not found. Stopping loading process !" -ForegroundColor Red

  27:             exit 

  28:         }

  29:         Write-Host "Creating/Updating Property Bags ..." -ForegroundColor Green    

  30:         foreach ($fila in $tblData) 

  31:         {  

  32:             #$fila       

  33:             $sPBScope=$fila.PropertyBagScope.ToString()            

  34:             $sPBKey=$fila.PropertyBagKey.ToString() 

  35:             $sPBValue=$fila.PropertyBagValue.ToString()

  36:         #Calling the function that creates/updates the property bags

  37:             CreateUpdatePropertyBag -sPropertyBagScope  $sPBScope -sPropertyBagKey $sPBKey -sPropertyBagValue $sPBValue

  38:         }   

  39:     }

  40:     catch [System.Exception]

  41:     {

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

  43:     }

  44: }

  45:  

  46: #

  47: #Function that creates/updates each property bag

  48: #

  49: function CreateUpdatePropertyBag

  50: {

  51:     param ($sPropertyBagScope,$sPropertyBagKey,$sPropertyBagValue)

  52:     try

  53:     {              

  54:         switch ($sPropertyBagScope) 

  55:         { 

  56:         "Farm" {

  57:             $spfFarm=Get-SPFarm

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

  59:             if($sPropertyBag -eq "")

  60:             {  

  61:                 Write-Host "Adding Property Bag $sPropertyBagKey ato the farm!!" -ForegroundColor Green

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

  63:             }else{

  64:                 Write-Host "Updating Property Bag $sPropertyBagKey in the granja" -ForegroundColor Green

  65:                 $spfFarm.Properties[$sPropertyBagKey]=$sPropertyBagValue

  66:             }

  67:             $spfFarm.Update()            

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

  69:             Write-Host "Property bag $sPropertyBagKey has a value $sPropertyBag" -ForegroundColor Green

  70:             } 

  71:         "WebApplication" {

  72:             #Code for Web Application Property bags here

  73:             }

  74:         "SiteCollection" {

  75:             $spSite=Get-SPSite -Identity $sSiteCollection

  76:             $spwWeb=$spSite.OpenWeb()

  77:             $sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]

  78:             if($sPropertyBag -eq "")

  79:             {  

  80:                 Write-Host "Adding Property Bag $sPropertyBagKey to $sSiteCollection !!" -ForegroundColor Green

  81:                 $spwWeb.AllProperties.Add($sPropertyBagKey,$sPropertyBagValue) 

  82:             }else{

  83:                 Write-Host "Updating Property Bag $sPropertyBagKey for $sSiteCollection" -ForegroundColor Green            

  84:                 $spwWeb.AllProperties[$sPropertyBagKey]=$sPropertyBagValue

  85:             }           

  86:             $spwWeb.Update()            

  87:             $sPropertyBag=$spwWeb.AllProperties[$sPropertyBagKey]

  88:             Write-Host "Property bag $sPropertyBagKey has a value $sPropertyBag" -ForegroundColor Green

  89:             }

  90:         "Site" {

  91:             #Código para Property Bags de Sitio aquí

  92:             } 

  93:         "List" {

  94:             #Código para Property Bags de Lista aquí

  95:             }           

  96:         default {

  97:             Write-Host "Requested opeartion is not valid!!" -ForegroundColor DarkBlue            

  98:             }

  99:         }        

 100:     }

 101:     catch [System.Exception]

 102:     {

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

 104:     }

 105: }

 106:  

 107: #Departamentos

 108: $ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path

 109: $sInputFile=$ScriptDir+ "\PropertyBags.csv"

 110: $sSiteCollection="http://<YourSiteCollection>"

 111:  

 112: Start-SPAssignment –Global

 113: #Calling the function

 114: ReadPropertyBagsFromFile -sInputFile $sInputFile

 115:  

 116: Stop-SPAssignment –Global

 117:  

 118: Remove-PSSnapin Microsoft.SharePoint.PowerShell