PowerShell

Format-Table & Select

L'exemple suivant récupère la liste des SPSites d'une ferme SharePoint, et affiche, pour chaque site,
  • Son Url
  • Le WebTemplate de son site racine

Get-SPSite -Limit All | Select Url, @{Label="WebTemplate"; Expression={$_.RootWeb.WebTemplate}} | ft Url, WebTemplate

!!Chargement des Snappins SQL Server
#
# Initialize-SqlpsEnvironment.ps1
#
# Loads the SQL Server provider extensions
#
# Usage: Powershell -NoExit -Command "& '.\Initialize-SqlPsEnvironment.ps1'"
#
# Change log:
# June 14, 2008: Michiel Wories
#   Initial Version
# June 17, 2008: Michiel Wories
#   Fixed issue with path that did not allow for snapin\provider:: prefix of path
#   Fixed issue with provider variables. Provider does not handle case yet
#   that these variables do not exist (bug has been filed)

$ErrorActionPreference = "Stop"

$sqlpsreg="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps"

if (Get-ChildItem $sqlpsreg -ErrorAction "SilentlyContinue")
{
    throw "SQL Server Powershell is not installed."
}
else
{
    $item = Get-ItemProperty $sqlpsreg
    $sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path)
}


#
# Preload the assemblies. Note that most assemblies will be loaded when the provider
# is used. if you work only within the provider this may not be needed. It will reduce
# the shell's footprint if you leave these out.
#
$assemblylist = 
"Microsoft.SqlServer.Smo",
"Microsoft.SqlServer.Dmf ",
"Microsoft.SqlServer.SqlWmiManagement ",
"Microsoft.SqlServer.ConnectionInfo ",
"Microsoft.SqlServer.SmoExtended ",
"Microsoft.SqlServer.Management.RegisteredServers ",
"Microsoft.SqlServer.Management.Sdk.Sfc ",
"Microsoft.SqlServer.SqlEnum ",
"Microsoft.SqlServer.RegSvrEnum ",
"Microsoft.SqlServer.WmiEnum ",
"Microsoft.SqlServer.ServiceBrokerEnum ",
"Microsoft.SqlServer.ConnectionInfoExtended ",
"Microsoft.SqlServer.Management.Collector ",
"Microsoft.SqlServer.Management.CollectorEnum"


foreach ($asm in $assemblylist)
{
    $asm = [Reflection.Assembly]::LoadWithPartialName($asm)
}

#
# Set variables that the provider expects (mandatory for the SQL provider)
#
Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0
Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30
Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $false
Set-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000

#
# Load the snapins, type data, format data
#
Push-Location
cd $sqlpsPath
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Update-TypeData -PrependPath SQLProvider.Types.ps1xml 
update-FormatData -prependpath SQLProvider.Format.ps1xml 
Pop-Location

Write-Host -ForegroundColor Yellow 'SQL Server Powershell extensions are loaded.'
Write-Host
Write-Host -ForegroundColor Yellow 'Type "cd SQLSERVER:\" to step into the provider.'
Write-Host
Write-Host -ForegroundColor Yellow 'For more information, type "help SQLServer".'


SharePoint

Nom des NoteField associés au TaxonomyFields

private string AddHiddenTextField(SPFieldCollection fields)
{
    string str;
    string str2;
    int num = 0;
    while (fields.ContainsField(base.Title + "_" + num.ToString(CultureInfo.InvariantCulture)))
    {
        num++;
    }
    if (this.textField == Guid.Empty)
    {
        this.textField = Guid.NewGuid();
    }
    if (this.IsKeyword)
    {
        str = "TaxKeywordTaxHTField";
        str2 = "TaxKeywordTaxHTField";
    }
    else
    {
        str = base.Title + "_" + num.ToString(CultureInfo.InvariantCulture);
        str2 = base.Id.ToString("N");

        // Ceci est la partie importante : on élimite les "_" du Guid du TaxonomyField
        // et on remplace le premier caractère s'il est numérique 
        // car les noms de champs ne peuvent commencer par un nombre
        if ((str2[0] >= '0') && (str2[0] <= '9'))
        {
            char ch = str2[0];
            str2 = new Regex(ch.ToString()).Replace(str2, ((char) (str2[0] + '7')).ToString(), 1);
        }
    }
    return fields.AddFieldAsXml(
                        TaxonomyFeatureReceiver.BuildFieldSchema(
                             SPFieldType.Note.ToString(), 
                             str, 
                             str2, 
                             string.Empty, 
                             string.Empty, 
                             string.Empty, 
                             this.textField.ToString("B"), 
                             string.Empty, 
                             false, 
                             true, 
                             true, 
                             false, 
                             false), 
                        false, 
                        SPAddFieldOptions.AddFieldInternalNameHint | SPAddFieldOptions.AddToAllContentTypes);
}

Creation d'un ClientContexte pour les WorkerApps

Uri targetWeb = new Uri("https://contoso.sharepoint.com"); 

string targetRealm = TokenHelper.GetRealmFromTargetUrl(targetWeb); 
var responseToken = TokenHelper.GetAppOnlyAccessToken(
      TokenHelper.SharePointPrincipal, 
      targetWeb.Authority, 
      targetRealm); 

ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(
      targetWeb.ToString(), 
      responseToken.AccessToken);

activation de features pour les colonnes de métadonnées gérées

Enable-SPFeature -id "73EF14B1-13A9-416b-A9B5-ECECA2B0604C" -Url <Site-URL>
Enable-SPFeature -id "7201D6A4-A5D3-49A1-8C19-19C4BAC6E668" -Url <Site-URL>

Last edited May 26, 2014 at 12:31 PM by lmondeil, version 7