What version of PowerShell.Create() is created?
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
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.
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.b/c that's not a command, it's a variable reference
you want AddScript
oh ffs
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);
Btw, the documentation is wrong. I'm seeing a lot of references for
PowerShell.Create("my-cmdlet").AddCommand(...)
But, Create
only takes in a namespaceActually, even that page you sent is completely wrong. The page is for
AddScript
, but the command references in the snippets are using AddCommand
oof I never noticed that
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.
they are
but you weren't using a cmdlet in your original snippet
$PSVersionTable.PSVersion
is a variable expressionGood point.
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$powershell may know better
Okay, I'll check with them. I figured this would be a c# thing