Jaime Still Jaime Still - 2 months ago 30
C# Question

Load C# Embedded Resource Path into PowerShell Command Class

Update: The answer below was able to get me where I needed to be. Here is the full solution, if interested in seeing Angular / WebAPI interface with PowerShell on the backend.

enter image description here

I have a

.ps1
file saved as an embedded resource in a Scripts folder in a C# class library. What I would like to do is pass this script into a new
System.Management.Automation.Runspaces.Command
class as follows:

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());
}
}
}
}


The following exception is thrown whenever I hit
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."


I've run the same code, but specifying a URL for a file on my hard drive, for instance,
D:\Desktop\QueryWmi.ps1
, and it works just fine.

Answer

After reading PetSerAl's comment, I've updated the code. Using the Command(string command, bool isScript) constructor.

        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();
                }
            }
        }
Comments