lundi 23 mars 2015

Cannot find type [Microsoft.SharePoint.SPSite]: make sure the assembly containing this type is loaded



I'm trying yo Run this script ps1 from C# code but it returns me the following error where trigger "pipeline.Invoke()";



InitialSessionState iss = InitialSessionState.CreateDefault();
PSSnapInException warning;
// iss.ImportPSSnapIn("Microsoft.SharePoint.PowerShell", out warning);

Runspace runspace = RunspaceFactory.CreateRunspace();

Pipeline pipeline = runspace.CreatePipeline();
// open it
runspace.Open();

pipeline.Commands.AddScript(@"C:\Users\Administrator\Desktop\Untitled2.ps1");
try
{
pipeline.Invoke();
}
catch (Exception ex)
{
throw ex;
}
//pipeline.Commands.Add("Get-Process");

// execute the script
PSObject bob;
Collection<PSObject> results = pipeline.Invoke();

// close the runspace

runspace.Close();

// convert the script result into a single string

StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}

return stringBuilder.ToString();



POWERSHELL




#Add SharePoint PowerShell SnapIn if not already added

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue



if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


$site = new-object Microsoft.SharePoint.SPSite("http://sp2010devid/sites/esercizi3");
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);

#Get UserProfileManager from the My Site Host Site context
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()

foreach($profile in $AllProfiles)
{
$DisplayName = $profile.DisplayName
$AccountName = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::AccountName].Value


#Here goes writing Logic to your SharePoint List + Check if account already existing in the SharePoint list then ignore writing.......
write-host "Profile for account ", $AccountName


}
write-host "Finished."
$site.Dispose()







0 commentaires:

Enregistrer un commentaire