I have a C# console program, and I want to run PowerShell commands NOT SCRIPTS, but no matter what command I try to run in the PowerShell runspace, it gets an error saying cmdlet not found. It can be as simple as the following:
static void Main(string[] args)
{
try
{
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddCommand("(Get-Date).AddDays(1)");
var result = ps.Invoke();
Console.WriteLine(result);
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.ToString());
}
}
I can type this command at the PowerShell command line and it runs perfectly fine. I don't have to add any modules to run it. What am I missing?