2

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.

1 Answer 1

1

Follow below official guideline to import module.

https://learn.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-design-pnp-provisioning#upload-the-pnp-powershell-module-for-your-azure-function

To create PowerShell script function, check this thread.

My test demo(Get-PnPWeb|select Title, below screenshot not update to latest script): enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.