C
C#6mo ago
engineertdog

What version of PowerShell.Create() is created?

PowerShell ps = PowerShell.Create().AddCommand("$PSVersionTable.PSVersion");
ps.Invoke();
PowerShell ps = PowerShell.Create().AddCommand("$PSVersionTable.PSVersion");
ps.Invoke();
This throws an error, stating that $PSVersionTable is not a function. I have PowerShell 7 installed where the code is running, and I've verified the commands still work there. So what is the SDK creating behind the scenes that's not compatible?
15 Replies
engineertdog
engineertdog6mo ago
I found this version which appears to be a bit more verbose for importing the cmdlets I need, but I still get an error that the command isn't valid.
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new[] { "OSIsoft.PowerShell" });
iss.LanguageMode = PSLanguageMode.FullLanguage;
var _o365Runspace = RunspaceFactory.CreateRunspace(iss);
_o365Runspace.Open();
var _o365Shell = PowerShell.Create();
_o365Shell.Runspace = _o365Runspace;
_o365Shell.Commands.AddCommand("Connect-PIDataArchive").AddParameter("PIDataArchiveMachineName", "192.168.1.27");
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new[] { "OSIsoft.PowerShell" });
iss.LanguageMode = PSLanguageMode.FullLanguage;
var _o365Runspace = RunspaceFactory.CreateRunspace(iss);
_o365Runspace.Open();
var _o365Shell = PowerShell.Create();
_o365Shell.Runspace = _o365Runspace;
_o365Shell.Commands.AddCommand("Connect-PIDataArchive").AddParameter("PIDataArchiveMachineName", "192.168.1.27");
I think I figured out the reason. The DLL for the module is 32-bit. I was targeting any cpu. I've changed it to x86, but still getting the cmdlet not found error.
jcotton42
jcotton426mo ago
b/c that's not a command, it's a variable reference you want AddScript
engineertdog
engineertdog6mo ago
oh ffs
jcotton42
jcotton426mo ago
PowerShell.AddScript Method (System.Management.Automation)
Add a piece of script to construct a command pipeline. For example, to construct a command string "get-process | foreach { $.Name }" PowerShell shell = PowerShell.Create("get-process"). AddCommand("foreach { $.Name }", true);
engineertdog
engineertdog6mo ago
Btw, the documentation is wrong. I'm seeing a lot of references for PowerShell.Create("my-cmdlet").AddCommand(...) But, Create only takes in a namespace
engineertdog
engineertdog6mo ago
No description
engineertdog
engineertdog6mo ago
No description
engineertdog
engineertdog6mo ago
Actually, even that page you sent is completely wrong. The page is for AddScript, but the command references in the snippets are using AddCommand
jcotton42
jcotton426mo ago
oof I never noticed that harold
engineertdog
engineertdog6mo ago
Thanks for the clarification. It doesn't seem I'll be able to do some of the dynamic things I was looking at....but I shouldn't need to I didn't realize that cmdlets weren't considered commands.
jcotton42
jcotton426mo ago
they are but you weren't using a cmdlet in your original snippet $PSVersionTable.PSVersion is a variable expression
engineertdog
engineertdog6mo ago
Good point.
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new[] { "OSIsoft.PowerShell" });
iss.LanguageMode = PSLanguageMode.FullLanguage;
var _o365Runspace = RunspaceFactory.CreateRunspace(iss); // using
_o365Runspace.Open();
var _o365Shell = PowerShell.Create(_o365Runspace); // using
var connect = new Command("Connect-PIDataArchive");
connect.Parameters.Add("PIDataArchiveMachineName", "192.168.1.27");
_o365Shell.Commands.AddCommand(connect);
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new[] { "OSIsoft.PowerShell" });
iss.LanguageMode = PSLanguageMode.FullLanguage;
var _o365Runspace = RunspaceFactory.CreateRunspace(iss); // using
_o365Runspace.Open();
var _o365Shell = PowerShell.Create(_o365Runspace); // using
var connect = new Command("Connect-PIDataArchive");
connect.Parameters.Add("PIDataArchiveMachineName", "192.168.1.27");
_o365Shell.Commands.AddCommand(connect);
That throws the cmdlet not found as well, but it works if I run it as a script Sort of. ps.Invoke() for the script returns 0 records when running the same script manually is fine. And I thought spawning new processes in Framework made things difficult lol
jcotton42
jcotton426mo ago
$powershell may know better
engineertdog
engineertdog6mo ago
Okay, I'll check with them. I figured this would be a c# thing
Want results from more Discord servers?
Add your server
More Posts