【发布时间】:2019-12-28 02:40:06
【问题描述】:
我有一个应该在 SharePoint 上运行的 PowerShell 脚本。我一直在尝试在 Visual Studio 2019 上使用 C# 处理代码。我尝试了几种不同的方法,但都没有奏效。最终,我希望这些命令能够在 Azure 上部署的机器人上运行。
我已经在 PowerShell 上完成了Install-Module SharePointPnPPowerShellOnline,在 Visual Studio 上通过 PM 完成了 Install-Package SharePointPnPCoreOnline -Version 3.12.1908。
我的脚本在 PowerShell 上运行良好,但当我尝试使用 C# 调用脚本时它不起作用。
我不断收到此错误 - System.Management.Automation.CommandNotFoundException: 'The term 'Connect-PnPOnline' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。
我从System.Management.Automation.Runspaces 中找到了一些使用RunspaceConfiguration 的解决方案。但似乎 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 命令。有没有人有类似的问题或关于如何解决这个问题的任何想法?
感谢您的帮助。
【问题讨论】:
标签: c# .net powershell sharepoint