Call a Powershell script from c# code

PowerShell logo This solution given here :
executing-powershell-scripts-from-c is fine, but only works with .NET 4.0 or above…
In my case, I needed to call a powershell script from a c# code source running under .NET 3.5.
So, this is a function that uses a Process objet to run Powershell :

public static int RunPowershellScript(string ps)
{
int errorLevel;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("powershell.exe", "-File " + ps);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
process = Process.Start(processInfo);
process.WaitForExit();
errorLevel = process.ExitCode;
process.Close();
return errorLevel;
}

This code is synchronous, and has a lack of prerequisites checks, but it can be reusable in a more elegant code 😉

How to migrate contacts from Windows Live Mail to Outlook.com

(English translation coming soon !)
Il semble qu’il y ait un problème d’export / import de contacts de « Windows Live Mail » vers « Outlook.com ».
import_outlook
Le format d’export de Windows Live mail (fichier *.CSV) ne semble pas reconnu par Outlook…
Solution : On ne va pas se laisser abattre ! Migrons le format CSV de Windows Live Mail vers le format reconnu par Outlook avec un petit traitement powerShell pour faire cet import :
import_outlook_2
Le script PowerShell peut ressembler à ci-dessous. Tout ce que vous avez à changer est le nom du fichier « windows_live_mail_export_file.csv ». Attention, je récupère les champs les plus utilisés :

#Header line. Every value must be enclosed in double-quotes (that’s not the cas in Windows Live mail export)
$output = "`"Title`",`"First Name`",`"Middle Name`",`"Last Name`",`"Suffix`",`"Given Name Yomi`",`"Family Name Yomi`",`"Home Street`",`"Home City`",`"Home State`",`"Home Postal Code`",`"Home Country`",`"Company`",`"Department`",`"Job Title`",`"Office Location`",`"Business Street`",`"Business City`",`"Business State`",`"Business Postal Code`",`"Business Country`",`"Other Street`",`"Other City`",`"Other State`",`"Other Postal Code`",`"Other Country`",`"Assistant’s Phone`",`"Business Fax`",`"Business Phone`",`"Business Phone 2`",`"Callback`",`"Car Phone`",`"Company Main Phone`",`"Home Fax`",`"Home Phone`",`"Home Phone 2`",`"ISDN`",`"Mobile Phone`",`"Other Fax`",`"Other Phone`",`"Pager`",`"Primary Phone`",`"Radio Phone`",`"TTY/TDD Phone`",`"Telex`",`"Anniversary`",`"Birthday`",`"E-mail Address`",`"E-mail Type`",`"E-mail 2 Address`",`"E-mail 2 Type`",`"E-mail 3 Address`",`"E-mail 3 Type`",`"Notes`",`"Spouse`",`"Web Page`"" + "`r`n"
$output | out-file ".outlook.com_export_file.csv"
# Let’s parse each line of the original export file
Get-Content "windows_live_mail_export_file.csv" | % {
# Values are separated by comas
$infosArray = $_.Split(",")
# Store in variable the wanted values
$FirstName = $infosArray[0]
$Name = $infosArray[1]
$Email = $infosArray[5]
$Street = $infosArray[6]
$Town = $infosArray[7]
$PostalCode = $infosArray[8]
$Country = $infosArray[10]
$HomePhone = $infosArray[11]
$HomePhone2 = $infosArray[12]
$MobilePhone = $infosArray[13]
$WebSite = $infosArray[14]
$Comment = $infosArray[28]
$output = ",`"$FirstName`",,`"$Name`",,,,`"$Street`",`"$Town`",`"$Dpt`",`"$PostalCode`",`"$Country`",,,,,,,,,,,,,,,,,,,,,,,`"$HomePhone`",`"$HomePhone2`",,`"$MobilePhone`",,,,,,,,,,`"$Email`",,,,,,`"$Comment`",,`"$WebSite`""
# Concatenation of new line
Add-Content ".outlook.com_export_file.csv" $output
}

Attention, lors du choix du format de l’import, il faut choisir « Outlook.com » !
import_outlook_3

SharePoint 2010 : Diminuer la durée rétention des données d'utilisation

Better than a Shrink
Voici la commande à passer pour passer à 3 jours de rétention pour les données d’utilisation de la ferme (Console Powershell SharePoint) :
Get-SPUsageDefinition | foreach-Object { Set-SPUsageDefinition -Identity $_.Name -DaysRetained 3 }
Ceci permet de réduire la taille de la base WSS_Logging.

Serveur SharePoint 2010 : Surcharge de la mémoire

Le bac à sable, pour le garde des sceaux
Symptôme : La RAM atteint une consommation inquiétante (et incrémentielle).
Diagnostic : La multiplication des processus SPUCHostService.exe et SPUCWorker*.exe pollue la mémoire. Ces processus correspondent aux solutions qui fonctionnent en mode “bac à sable”. Cela permet d’exécuter du code utilisateur sans risque pour le processus IIS w3wp.exe.
Solution : Relancer le service “Service de code en mode bac à sable Microsoft SharePoint Foundation” (via l’administration centrale ou Powershell (ci-dessous).
Se connecter au serveur avec le compte d’installation de la ferme.
Lancer une fenêtre POWERSHELL sur le serveur (en tant qu’administrateur), et de lancer les commandes suivantes :
Get-SPServiceInstance | Where-Object {$_.TypeName -like "Service de code en mode bac*"}
Copier la valeur de l’ID affichée, et entrer les commandes suivantes :
Stop-SPServiceInstance -Identity "id_copié_précédemment"
Start-SPServiceInstance -Identity "id_copié_précédemment"
 
Aller plus loin : Déterminer quelle solution en mode bac à sable provoque cette multiplication des processus.