c# - 获取 PowerShell 脚本以在 Azure 上运行 SharePoint 命令

标签 c# .net powershell sharepoint

我有一个应该在 SharePoint 上运行的 PowerShell 脚本。我一直在尝试在 Visual Studio 2019 上使用 C# 处理代码。我尝试了几种不同的方法,但都没有奏效。最终,我希望这些命令在部署在 Azure 上的机器人上运行。

我已经完成了Install-Module SharePointPnPPowerShellOnline在 PowerShell 和 Install-Package SharePointPnPCoreOnline -Version 3.12.1908 上通过 Visual Studio 上的 PM。 我的脚本在 PowerShell 上运行良好,但当我尝试使用 C# 调用脚本时它不起作用。 我一直收到此错误 - System.Management.Automation.CommandNotFoundException: 'The term 'Connect-PnPOnline'不被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

我找到了一些使用 RunspaceConfiguration 的解决方案来自 System.Management.Automation.Runspaces .但看起来像 RunspaceConfiguration已从命名空间中删除,因为我找不到它。

我尝试通过执行来调用脚本

PowerShellInstance.AddScript(script)PowerShellInstance.AddCommand("Connect-PnPOnline").AddParameter()...但是两者都不起作用。

两者都是 PowerShellInst.Invoke()pipeline.Invoke()返回错误 Connect-Online不被认可。而且我只在 Visual Studio 而不是 PowerShell 上遇到了这个错误。

此代码在 pipeline.Invoke() 处有错误那Connect-PnPOnline不被认可 第一个例子

{
    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();
} 

此代码在 PowerShellInst.Invoke() 处有错误那Connect-PnPOnline不被认可 第二个例子

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());
    }
}

这是我的示例脚本。

$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

问题似乎是当我尝试在 Visual Studio 上使用 C# 执行我的脚本时无法识别 SharePoint 命令。有没有人有类似的问题或对如何解决这个问题有任何想法?

感谢您的帮助。

最佳答案

按照以下官方指南导入模块。

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

要创建 PowerShell 脚本函数,请检查 this thread .

我的测试演示(Get-PnPWeb|选择标题,下面的截图没有更新到最新的脚本): enter image description here

关于c# - 获取 PowerShell 脚本以在 Azure 上运行 SharePoint 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57614980/

相关文章:

c# - 将类型的通用容器转换为继承类型的容器?

c# - SQL Server 在插入存储过程后运行更新存储过程

.net - 在 .NET 中模拟 VBA 算术

powershell - 如何从文本文件中提取子字符串?

c# - 找不到 HttpConfiguration 方法 EnableCors

java - 数据库查询以获取范围内的记录

c# - NHibernate HQL Generator 支持 SQL Server 2016 时态表

powershell - 将多个成员添加到Powershell哈希表

powershell - 在 Powershell 中返回第一个匹配的文件夹

c# - 如何在发布版本中附加第三方库