将 C# 嵌入式资源路径加载到 PowerShell 命令中 Class
Load C# Embedded Resource Path into PowerShell Command Class
更新:下面的答案能够让我到达我需要去的地方。 Here is the full solution,如果有兴趣查看 Angular / WebAPI 与后端 PowerShell 的接口。
我有一个 .ps1
文件作为嵌入式资源保存在 C# class 库的脚本文件夹中。我想做的是将此脚本传递到新的 System.Management.Automation.Runspaces.Command
class 中,如下所示:
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
using (Runspace rs = RunspaceFactory.CreateRunspace(iss))
{
rs.Open();
Command queryWmi = new Command("PowerShellAPIFramework.Core.Scripts.QueryWmi.ps1");
queryWmi.Parameters.Add("query", model.query);
queryWmi.Parameters.Add("properties", model.properties);
queryWmi.Parameters.Add("computername", model.computername);
queryWmi.Parameters.Add("wmiNamespace", model.wmiNamespace);
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = rs;
ps.Commands.AddCommand(queryWmi);
var results = ps.Invoke();
if (ps.HadErrors)
{
if (ps.Streams.Error.Count > 0)
{
foreach (var error in ps.Streams.Error)
{
Console.WriteLine(error.Exception.GetExceptionMessageChain());
}
}
}
else
{
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
}
}
}
每当我点击 ps.Invoke()
时抛出以下异常
"The term 'PowerShellAPIFramework.Core.Scripts.QueryWmi.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
我有 运行 相同的代码,但为我硬盘上的文件指定了 URL,例如 D:\Desktop\QueryWmi.ps1
,它工作得很好。
阅读 PetSerAl 的评论后,我更新了代码。使用 Command(string command, bool isScript) 构造器。
using (Stream st = new MemoryStream(Properties.Resources.yourResource))
{
using (StreamReader sr = new StreamReader(st))
{
string script = sr.ReadToEnd();
using (PowerShell ps = PowerShell.Create())
{
Command cmd = new Command(script, true);
//Add Parameters
ps.Commands.AddCommand(cmd);
ps.Invoke();
}
}
}
更新:下面的答案能够让我到达我需要去的地方。 Here is the full solution,如果有兴趣查看 Angular / WebAPI 与后端 PowerShell 的接口。
我有一个 .ps1
文件作为嵌入式资源保存在 C# class 库的脚本文件夹中。我想做的是将此脚本传递到新的 System.Management.Automation.Runspaces.Command
class 中,如下所示:
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
using (Runspace rs = RunspaceFactory.CreateRunspace(iss))
{
rs.Open();
Command queryWmi = new Command("PowerShellAPIFramework.Core.Scripts.QueryWmi.ps1");
queryWmi.Parameters.Add("query", model.query);
queryWmi.Parameters.Add("properties", model.properties);
queryWmi.Parameters.Add("computername", model.computername);
queryWmi.Parameters.Add("wmiNamespace", model.wmiNamespace);
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = rs;
ps.Commands.AddCommand(queryWmi);
var results = ps.Invoke();
if (ps.HadErrors)
{
if (ps.Streams.Error.Count > 0)
{
foreach (var error in ps.Streams.Error)
{
Console.WriteLine(error.Exception.GetExceptionMessageChain());
}
}
}
else
{
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
}
}
}
每当我点击 ps.Invoke()
"The term 'PowerShellAPIFramework.Core.Scripts.QueryWmi.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
我有 运行 相同的代码,但为我硬盘上的文件指定了 URL,例如 D:\Desktop\QueryWmi.ps1
,它工作得很好。
阅读 PetSerAl 的评论后,我更新了代码。使用 Command(string command, bool isScript) 构造器。
using (Stream st = new MemoryStream(Properties.Resources.yourResource))
{
using (StreamReader sr = new StreamReader(st))
{
string script = sr.ReadToEnd();
using (PowerShell ps = PowerShell.Create())
{
Command cmd = new Command(script, true);
//Add Parameters
ps.Commands.AddCommand(cmd);
ps.Invoke();
}
}
}