获取PowerShell脚本以在Azure上运行SharePoint命令 [英] Get PowerShell Script to Run SharePoint Commands on Azure
问题描述
我有一个应该在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:'术语'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
第一个例子
<代码> {字符串文本= System.IO.File.ReadAllText(@"C:\ xxx \ xxx \ oo.ps1");运行空间运行空间= RunspaceFactory.CreateRunspace();runspace.Open();管道管道= runspace.CreatePipeline();pipeline.Commands.AddScript(文本);集合< PSObject>PSOutput = pipe.Invoke();foreach(PSOutput中的PSObject obj){Console.WriteLine(obj.BaseObject.ToString());}Console.WriteLine("Done");runspace.Close();}
此代码在 PowerShellInst.Invoke()
处出现错误,无法识别 Connect-PnPOnline
第二个例子
使用(PowerShell PowerShellInst = PowerShell.Create()){PowerShellInst.AddCommand("Connect-PnPOnline").AddParameter("site","https://xxxxx.sharepoint.com/xxxxx").AddParameter("url","site").AddParameter("UseWebLogin","xxx").AddParameter(-Credentials","xxxxx");集合< PSObject>PSOutput = PowerShellInst.Invoke();foreach(PSOutput中的PSObject obj){Console.WriteLine(读取脚本上下文:" + obj.BaseObject.ToString());}}
这是我的示例脚本.
$ site ="https://xxxxx.sharepoint.com/xxxxx"Connect-PnPOnline -url $ site -UseWebLogin xxx-凭证$ xxxxNew-PnPWeb-模板"{xxxxxx} #ooo_projecttemplate"-标题$ projecttitle -URL $ projectnumber-区域设置"777"-说明$ theDescription -BreakInheritanceNew-PnPGroup -Title $ Title-所有者$ ID
似乎问题在于,当我尝试在Visual Studio上使用C#执行脚本时,无法识别SharePoint命令.有没有人有类似的问题或关于如何解决这个问题的任何想法?
感谢您的帮助.
请按照以下官方指南导入模块.
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.
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):
这篇关于获取PowerShell脚本以在Azure上运行SharePoint命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!