我有一个应该在 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 命令。有没有人有类似的问题或对如何解决这个问题有任何想法?
感谢您的帮助。
最佳答案
按照以下官方指南导入模块。
要创建 PowerShell 脚本函数,请检查 this thread .
关于c# - 获取 PowerShell 脚本以在 Azure 上运行 SharePoint 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57614980/