You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Microsoft.PowerShell.SDK - AddScript does not work w/ script path or nested Powershell files #16893
Resolution-AnsweredThe question is answered.WG-DevEx-SDKhosting PowerShell as a runtime, PowerShell's APIs, PowerShell Standard, or development of modules
I am attempting to invoke a Powershell file via c# using the Microsoft.PowerShell.SDK (7.2.1, net6)
I can only get back the return value if AddScript is passed the raw script text. However, that method starts failing when nested Powershell files get used.
The AddScript documentation appears incorrect -- it only referes to AddCommand in the examples.
I was looking at the Powershell.AddCommand(CommandInfo info overload, but all the constructors of those implementations have been marked internal, so that public overload does not allow anything to be passed into it? :/
privateobjectTestPowershell(stringpath){varps=PowerShell.Create();vartext=File.ReadAllText(path);ps=ps// .AddScript(text) <- this works when using raw settings. Nested powershell files does not// .AddCommand(path) <- does not work at all..AddScript(path)// <- does not work at all..AddParameter("Env","local");varpsResult=ps.Invoke();returnpsResult;}
Can pass in script path via AddScript + nested powershell files work the same as invoked from terminal
Actual behavior
When using the script path (or a nested script is involved), the result set is always empty.Raw script text w/ just a simple file works fine (so I know I'm at least close!)
I tried to combine two answers from stackoverflow (first & second)
InitialSessionState iss = InitialSessionState.CreateDefault();
// Override ExecutionPolicy
Ilya (@iSazonov) you are a life saver! Was banging my head against the wall on that one for a while.
Would it be worth documenting this code path? It seems like a pretty common pattern to invoke an existing .ps1 file from .NET core. I don't see any samples in the MSDN docs for this use case
For those that come across this, here is my final working example for running an external script using the Powershell SDK:
privateobjectTestPowershell(stringpath){// not required, but nice to do if you don't want the execution policy to blockvariss=InitialSessionState.CreateDefault();iss.ExecutionPolicy=Microsoft.PowerShell.ExecutionPolicy.Bypass;usingvarrunspace=RunspaceFactory.CreateRunspace(iss);runspace.Open();usingvarpipeline=runspace.CreatePipeline();varcmd=newCommand(path);cmd.Parameters.Add("Env","local");pipeline.Commands.Add(cmd);returnpipeline.Invoke();}
Would it be worth documenting this code path? It seems like a pretty common pattern to invoke an existing .ps1 file from .NET core. I don't see any samples in the MSDN docs for this use case
**[Adding and invoking commands - PowerShell](https://docs.microsoft.com/en-us/powershell/scripting/developer/hosting/adding-and-invoking-commands)**
Adding and invoking commands
Resolution-AnsweredThe question is answered.WG-DevEx-SDKhosting PowerShell as a runtime, PowerShell's APIs, PowerShell Standard, or development of modules
Activity
iSazonov commentedon Feb 17, 2022
This must be fixed in the repository (ex., #15568)
iSazonov commentedon Feb 17, 2022
Working example for AddScript https://docs.microsoft.com/en-us/powershell/scripting/developer/hosting/adding-and-invoking-commands?view=powershell-7.2#addscript
iSazonov commentedon Feb 17, 2022
Examples for files
https://stackoverflow.com/questions/68941818/how-to-execute-a-powershell-script-using-c-sharp-and-setting-execution-policy
https://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments
cmeyertons commentedon Feb 17, 2022
Ilya (@iSazonov) you are a life saver! Was banging my head against the wall on that one for a while.
Would it be worth documenting this code path? It seems like a pretty common pattern to invoke an existing .ps1 file from .NET core. I don't see any samples in the MSDN docs for this use case
For those that come across this, here is my final working example for running an external script using the Powershell SDK:
iSazonov commentedon Feb 17, 2022
You can open new issue https://github.com/MicrosoftDocs/PowerShell-Docs
cmeyertons commentedon Feb 17, 2022
Done on my side! Feel free to close if desired!
iSazonov commentedon Feb 18, 2022
We keep the issue opened to fix docs in code #16893 (comment)
sebsub321 commentedon Dec 17, 2022
daxian-dbw commentedon Apr 18, 2023
The doc issue is tracked by MicrosoftDocs/PowerShell-Docs#8594