Reputation: 147
I have a PowerShell script that is supposed to run on SharePoint. I've been trying to work on the code with C# on Visual Studio 2019. I've tried few different ways but none of them worked. Ultimately I want these commands to run on the bot deployed on Azure.
I have done Install-Module SharePointPnPPowerShellOnline
on the PowerShell and Install-Package SharePointPnPCoreOnline -Version 3.12.1908
through PM on Visual Studio.
My script worked fine on PowerShell but it did not work when I tried to invoke the script with C#.
I kept getting this error - System.Management.Automation.CommandNotFoundException: 'The term 'Connect-PnPOnline'
is not recognized as the name of a cmdlet, function, script file, or operable program.
I have found some solutions that use RunspaceConfiguration
from System.Management.Automation.Runspaces
. But seems like RunspaceConfiguration
is removed from the namespace as I cannot find it.
I have tried to invoke the script by doing
PowerShellInstance.AddScript(script)
or PowerShellInstance.AddCommand("Connect-PnPOnline").AddParameter()...
But both didn't work.
Both PowerShellInst.Invoke()
and pipeline.Invoke()
returned the error that Connect-Online
is not recognized. And I got this error only on Visual Studio instead of PowerShell.
This code has the error at pipeline.Invoke()
that Connect-PnPOnline
is not recognized
1st example
{
string text = System.IO.File.ReadAllText(@"C:\xxx\xxx\oo.ps1");
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(text);
Collection<PSObject> PSOutput = pipeline.Invoke();
foreach (PSObject obj in PSOutput)
{
Console.WriteLine(obj.BaseObject.ToString());
}
Console.WriteLine("Done");
runspace.Close();
}
This code has the error at PowerShellInst.Invoke()
that Connect-PnPOnline
is not recognized
2nd example
using (PowerShell PowerShellInst = PowerShell.Create())
{
PowerShellInst.AddCommand("Connect-PnPOnline")
.AddParameter("site", "https://xxxxx.sharepoint.com/xxxxx").AddParameter("url", "site").AddParameter("UseWebLogin", "xxx")
.AddParameter("-Credentials", "xxxxx");
Collection<PSObject> PSOutput = PowerShellInst.Invoke();
foreach (PSObject obj in PSOutput)
{
Console.WriteLine("Read context of the Script: " + obj.BaseObject.ToString());
}
}
Here is my sample script.
$site="https://xxxxx.sharepoint.com/xxxxx"
Connect-PnPOnline -url $site -UseWebLogin xxx -Credentials $xxxx
New-PnPWeb -Template "{xxxxxx}#ooo_projecttemplate" -Title $projecttitle -Url $projectnumber -Locale "777" -Description $theDescription -BreakInheritance
New-PnPGroup -Title $Title -owner $ID
Seems the issue is that SharePoint commands cannot be recognized when I try to execute my script with C# on Visual Studio. Does anyone have similar problems or any idea about how to solve this?
Appreciate your help.
Upvotes: 2
Views: 891
Reputation: 5493
Follow below official guideline to import module.
To create PowerShell script function, check this thread.
My test demo(Get-PnPWeb|select Title, below screenshot not update to latest script):
Upvotes: 1