Hello I'm trying to access a sharepoint list from powershell. The powershell script is executed from a c# application.
If I run the from c# application I get the following exception:
Exception calling ".ctor" with "1" argument(s): "The Web application at
http://pc/websites/Test4/
could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application."
My Code:
C#
string cmdArg = "C:\\Scripts\\GroupChangeGroup.ps1 1";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
Command newcom = new Command("Add-PSSnapin");
newcom.Parameters.Add("Name", "microsoft.exchange.management.powershell.e2010");
pipeline.Commands.Add(newcom);
Command newcom2 = new Command("Add-PSSnapin");
newcom2.Parameters.Add("Name", "Microsoft.SharePOint.POwershell");
pipeline.Commands.Add(newcom2);
pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
Collection<PSObject> results = pipeline.Invoke();
var error = pipeline.Error.ReadToEnd();
runspace.Close();
Powershell
$site = New-Object Microsoft.SharePoint.SPSite("http://pc/websites/Test4/")
$web = $site.OpenWeb()
$list = $web.Lists["GroupsList - ListInstance1"]
$listitem = $list.Items.GetItemByID(3)
If I execute the application from PS there is no problem. (same user context)
The User is Member of WSS_ADMIN_WPG