英文:
C# Powershell Modules
问题
I keep running in circles with trying to use PowerShell within a C# Application. I know everything I need to do is capable within PowerShell, I just want to wrap a GUI around it to make things easier.
我一直在尝试在C#应用程序中使用PowerShell,但一直没有取得进展。我知道我需要的一切都可以通过PowerShell实现,我只想在其周围创建一个GUI以简化操作。
I have PowerShell.SDK and Management.Automation installed with Nuget.
我已经使用NuGet安装了PowerShell.SDK和Management.Automation。
I initially created a PowerShell and tried to do the commands.
我最初创建了一个PowerShell并尝试执行命令。
PowerShell powerShell = PowerShell.Create();
PowerShell powerShell = PowerShell.Create();
But that's when I found that this creates a basic shell and didn't have my modules or policies. I did the follwing and it errors out saying it tried to do a user interaction. How do I get it to allow user interaction? I would be done if this worked.
但后来我发现这只创建了一个基本的Shell,没有我的模块或策略。我尝试以下操作,但出现错误,表示尝试进行用户交互。如何使其允许用户交互?如果这个方法能够运行,我就完成了。
PropertyInfo[] propertyInfo = initialSessionState.GetType().GetProperties();
propertyInfo[2].SetValue(initialSessionState, ExecutionPolicy.Unrestricted, null);
initialSessionState.ImportPSModule(new string[] { "Microsoft.Online.SharePoint.PowerShell" });
Runspace runspace = RunspaceFactory.CreateRunspace(initialSessionState);
runspace.Open();
PowerShell powerShell = PowerShell.Create(runspace);
powerShell.AddCommand("Connect-SPOService").AddParameter("-Url", "<https://site admin.sharepoint.com>").AddParameter("-Credential", "name@site.com").Invoke();
PropertyInfo[] propertyInfo = initialSessionState.GetType().GetProperties();
propertyInfo[2].SetValue(initialSessionState, ExecutionPolicy.Unrestricted, null);
initialSessionState.ImportPSModule(new string[] { "Microsoft.Online.SharePoint.PowerShell" });
Runspace runspace = RunspaceFactory.CreateRunspace(initialSessionState);
runspace.Open();
PowerShell powerShell = PowerShell.Create(runspace);
powerShell.AddCommand("Connect-SPOService").AddParameter("-Url", "<https://site admin.sharepoint.com>").AddParameter("-Credential", "name@site.com").Invoke();
I then tried to manually create the Credential needed by the connect. But it now throws exceptions in the Output Windows and does nothing. I have no way of knowing why since they happened within Invoke. How do I see the actual errors from PowerShell?
然后,我尝试手动创建连接所需的凭据。但现在它在输出窗口中引发异常并没有执行任何操作。我无法知道原因,因为这些异常发生在Invoke中。如何查看PowerShell的实际错误?
foreach (char a in "FakePassword") { passwordSecure.AppendChar(a); }
System.Management.Automation.PSCredential passwordCredential = new System.Management.Automation.PSCredential("name@site.com", passwordSecure);
runspace.SessionStateProxy.SetVariable("PasswordCredential", passwordCredential);
powerShell.AddCommand("Connect-SPOService").AddParameter("-Url", "https://site-admin.sharepoint.com").AddParameter("-Credential", "name@site.com").Invoke();
foreach (char a in "FakePassword") { passwordSecure.AppendChar(a); }
System.Management.Automation.PSCredential passwordCredential = a new System.Management.Automation.PSCredential("name@site.com", passwordSecure);
runspace.SessionStateProxy.SetVariable("PasswordCredential", passwordCredential);
powerShell.AddCommand("Connect-SPOService").AddParameter("-Url", "https://site-admin.sharepoint.com").AddParameter("-Credential", "name@site.com").Invoke();
How do I create a PowerShell in C# that inherits my personal PowerShell Environment? I think dependencies are hitting me too.
如何在C#中创建一个PowerShell环境,继承我的个人PowerShell环境?我认为依赖关系也在影响我。
英文:
I keep running in circles with trying to use PowerShell within a C# Application. I know everything I need to do is capable within PowerShell, I just want to wrap a GUI around it to make things easier.
I have PowerShell.SDK and Management.Automation installed with Nuget.
I initially created a PowerShell and tried to do the commands.
PowerShell powerShell = PowerShell.Create();
But that's when I found that this creates a basic shell and didn't have my modules or policies. I did the follwing and it errors out saying it tried to do a user interaction. How do I get it to allow user interaction? I would be done if this worked.
InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
PropertyInfo[] propertyInfo = initialSessionState.GetType().GetProperties();
propertyInfo[2].SetValue(initialSessionState, ExecutionPolicy.Unrestricted, null);
initialSessionState.ImportPSModule(new string[] { "Microsoft.Online.SharePoint.PowerShell" });
Runspace runspace = RunspaceFactory.CreateRunspace(initialSessionState);
runspace.Open();
PowerShell powerShell = PowerShell.Create(runspace);
powerShell.AddCommand("Connect-SPOService").AddParameter("-Url", "<https://site admin.sharepoint.com>").AddParameter("-Credential", "name@site.com").Invoke();
I then tried to manually create the Credential needed by the connect. But it now throws exceptions in the Output Windows and does nothing. I have no way of knowing why since they happened within Invoke. How do I see the actual errors from PowerShell?
System.Security.SecureString passwordSecure = new System.Security.SecureString();
foreach (char a in "FakePassword") { passwordSecure.AppendChar(a); }
System.Management.Automation.PSCredential passwordCredential = new System.Management.Automation.PSCredential("name@site.com", passwordSecure);
runspace.SessionStateProxy.SetVariable("PasswordCredential", passwordCredential);
powerShell.AddCommand("Connect-SPOService").AddParameter("-Url", "https://site-admin.sharepoint.com").AddParameter("-Credential", "name@site.com").Invoke();
How do I create a PowerShell in C# that inherits my personal PowerShell Environment? I think dependencies are hitting me too.
答案1
得分: 1
How do I get it to allow user interaction?
通过实现自定义宿主应用程序。
How do I see the actual errors from PowerShell?
在Invoke()
返回后检查PowerShell
实例:
if (powerShell.HadErrors) {
foreach (var errorRecord in powerShell.Streams.Error) {
// 在此处检查每个`errorRecord`
}
}
英文:
> How do I get it to allow user interaction?
By implementing a custom host application.
> How do I see the actual errors from PowerShell?
Inspect the PowerShell
instance after Invoke()
returns:
if (powerShell.HadErrors) {
foreach (var errorRecord in powerShell.Streams.Error) {
// inspect each `errorRecord` here
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论