C
C#2y ago
EliasGPS

Powershell result to c [Answered]

is it possible get a result from a powershell file after execution and store the result in a variable in c sharp? i have a powershell script that gives me an array and i want that array in c#
213 Replies
EliasGPS
EliasGPS2y ago
yeah i found that website to but the problem is they say "Create a System.Management.Automation.Powershell object" how do i do that. I'm knew to this so i'm sorry
Buddy
Buddy2y ago
Read the examples given
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"D:\PSScripts\MyScript.ps1")).Invoke();
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"D:\PSScripts\MyScript.ps1")).Invoke();
EliasGPS
EliasGPS2y ago
yeah but look
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
i can't use create so there is somthing wrong
EliasGPS
EliasGPS2y ago
bruh my powehsell is glitching i alraedey did that
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
sometimes its red and sometimes not
Buddy
Buddy2y ago
You have not imported the namespace System.Management.Automation
EliasGPS
EliasGPS2y ago
like this?
EliasGPS
EliasGPS2y ago
Buddy
Buddy2y ago
No
EliasGPS
EliasGPS2y ago
ow ok
EliasGPS
EliasGPS2y ago
using?
Buddy
Buddy2y ago
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
here you see red
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
and now good how? nothing changed its changing every 2 sec
Buddy
Buddy2y ago
Probably because VSCode / Omnisharp
EliasGPS
EliasGPS2y ago
yeah vscode is glithcing as hell
Buddy
Buddy2y ago
If you're a beginner and you're on Windows, I highly recommend Visual Studio which is different from Visual Studio Code
EliasGPS
EliasGPS2y ago
i'm not a real beginner, but i'm a beginner in c# ohh serious?
EliasGPS
EliasGPS2y ago
but i'm used to program in vscode
Buddy
Buddy2y ago
If you're used to VSCode and you know dotnet CLI. You should be fine. But yeah, VSCode can be a little glitchy. Just something you need to live with I guess <:bing_shrug:583791581497393162>
EliasGPS
EliasGPS2y ago
oh ok thx visual studio is so weird to i open a folder and i see nothing
EliasGPS
EliasGPS2y ago
Buddy
Buddy2y ago
You should not open the folder but the project itself .csproj or if a `.sln file exists, then choose that over *.csproj`
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
i need to click on this right?
Buddy
Buddy2y ago
Make sure you install the workloads
Buddy
Buddy2y ago
EliasGPS
EliasGPS2y ago
yeah thats already done
Buddy
Buddy2y ago
Then press that
EliasGPS
EliasGPS2y ago
ok
Buddy
Buddy2y ago
Buddy
Buddy2y ago
Open that if no *.sln file exists
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
ok
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
bruh
Buddy
Buddy2y ago
press view
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
i cliked solution explorer
Buddy
Buddy2y ago
Now you can navigate it just click on a file and start going
EliasGPS
EliasGPS2y ago
oh thx i'm so noob in visual studio and c# unbelievable hahah
Buddy
Buddy2y ago
I did not notice the class you made is called PowerShell, lol. I recommend renaming it as it can conflict with other classes called 'PowerShell'
EliasGPS
EliasGPS2y ago
i did that ahh
Buddy
Buddy2y ago
Sweet!
EliasGPS
EliasGPS2y ago
does not exists
Buddy
Buddy2y ago
Right-click the project in solution explorer press Manage Nuget packages
Buddy
Buddy2y ago
Buddy
Buddy2y ago
Browse for that nuget package, click it, then press install
Buddy
Buddy2y ago
EliasGPS
EliasGPS2y ago
good but he can't find Powerhsell
Buddy
Buddy2y ago
Class scope versus method scope it can only be inside a method
EliasGPS
EliasGPS2y ago
ahh
EliasGPS
EliasGPS2y ago
he still can't find Powerhsell
Buddy
Buddy2y ago
hover over Powershell, click the lightbulb on the left. What options do you have?
EliasGPS
EliasGPS2y ago
Buddy
Buddy2y ago
Did you install the package successfully?
EliasGPS
EliasGPS2y ago
yes but i used another version
Buddy
Buddy2y ago
Ah, that's why. It is only available in version 7.0.0 and above
EliasGPS
EliasGPS2y ago
because i use .net core 6 and i can't use the latest version how do i fix that
Buddy
Buddy2y ago
You can't?
EliasGPS
EliasGPS2y ago
bruh
Buddy
Buddy2y ago
You should be able to
EliasGPS
EliasGPS2y ago
wait what you said i can't and than you said i can?
Buddy
Buddy2y ago
I asked if you couldn't hence the questionmark
EliasGPS
EliasGPS2y ago
ah i think i need to install .net core 7 thats the only solution because .net core 6 can't install that version
ghoulam
ghoulam2y ago
There is a nugget package called CLI wrap use it to execute you ps script and get its output (read the docs for this option or watch nick chapssas video)
EliasGPS
EliasGPS2y ago
you serious?
ghoulam
ghoulam2y ago
Yep
EliasGPS
EliasGPS2y ago
omg that would save my life maybe
ghoulam
ghoulam2y ago
https://youtu.be/Pt-0KM5SxmI Here is the video where he will execute a cli command and read the output of the command directly from the C# program
Nick Chapsas
YouTube
Stop using the Process class for CLI interactions in .NET
Check out the ABP Framework here: https://bit.ly/3NGacLw Check out my courses: https://nickchapsas.com Become a Patreon and get source code access: https://www.patreon.com/nickchapsas Hello everybody I'm Nick and in this video I will show you how you can control your machine's CLI in an elegant way in C#. .NET's built in Process class is a tot...
EliasGPS
EliasGPS2y ago
oh man if this works that will save my life
ghoulam
ghoulam2y ago
I hope it will wish you all the luck
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
it does not support .ps1 i think
ghoulam
ghoulam2y ago
I think you should put your ps script run command in the Cli command inside a c# app and get its stream via the pipe
EliasGPS
EliasGPS2y ago
huh to run my script is just a .\Script.ps1
ghoulam
ghoulam2y ago
But you're right i think this package doesn't support running ps scripts since they are not lunched like other scipts
EliasGPS
EliasGPS2y ago
uhu yes
ghoulam
ghoulam2y ago
But there should be some way to execute it as a cli Like this command should work powershell -command "your Command" Or powershell -ExecutionPolicy Bypass -Command "your command"
EliasGPS
EliasGPS2y ago
IAmTimCorey
YouTube
Calling the Command Line in C# - Scripts, PowerShell, Automation, a...
The command line is an incredibly powerful tool to take advantage of in development. When you know how to use the command line, you can automate just about anything. This is why tools like Git, Docker, and even .NET are command-line first products. In this video, we are going to see how to build C# applications that can call command line applica...
EliasGPS
EliasGPS2y ago
i think its possible
ghoulam
ghoulam2y ago
Yep its possible I found the right command You shoul use powershell -file filename.ps1 in your cli command inside your C# code
EliasGPS
EliasGPS2y ago
just in my c# file? you mean in the cliwrap?
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
i now got this
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
and get this message so that is better but i am still not getting the ouput
ghoulam
ghoulam2y ago
using CliWrap; await Cli.Wrap("powershell"). WithArguments("-file C:\Users\okbb\Desktop\test.ps1"). WithStandardOutputPipe(PipeTarget.ToDelegate(Console.WriteLine)). ExecuteAsync(); this worked for me and printed hello world from my script
ghoulam
ghoulam2y ago
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
i used the same as yours but you send me a code and your file was written with "" its supposed to be "\" right? " \" i mean dubbel backslash \
ghoulam
ghoulam2y ago
yep you have to use \ double \
EliasGPS
EliasGPS2y ago
ow ok hahaha but than it doesn"t work
ghoulam
ghoulam2y ago
first try to execute it like me to see if everything works
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
it asks me this when i try to run
ghoulam
ghoulam2y ago
to be honest idk why you getting it i don't have any experience with ASP i am even new to C# to be honest
EliasGPS
EliasGPS2y ago
ow ok hahaha ahh wait you are using a consoleapp but i'm using ASP or am i wrong?
ghoulam
ghoulam2y ago
yes am using console app
EliasGPS
EliasGPS2y ago
i will try it with a console app maybe that will work?
ghoulam
ghoulam2y ago
the trick is to redirect the output of that command into a variable and that idk how 😂
EliasGPS
EliasGPS2y ago
jep if i can get that that would be amazing there is no different if i use console app or not so i'm doing something wrong the thing is you are just doing a simple hello world
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
when i run my script it is an array
ghoulam
ghoulam2y ago
okay i got something new after some research Process p = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "powershell.exe"; startInfo.Arguments = "-file C:\Users\okbb\Desktop\test.ps1"; p.StartInfo = startInfo; p.StartInfo.RedirectStandardOutput = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); Console.WriteLine("---------------"); Console.WriteLine(output);
EliasGPS
EliasGPS2y ago
Yeah but thats a process class?
ghoulam
ghoulam2y ago
EliasGPS
EliasGPS2y ago
how do i use that can you send me your code?
ghoulam
ghoulam2y ago
i successfully stored the output in a string variable
EliasGPS
EliasGPS2y ago
i mean a screenshot of your code in your editor are you serious? crazy
ghoulam
ghoulam2y ago
use snipping tool of windows
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
it kinda worked but i don't see otput
EliasGPS
EliasGPS2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam2y ago
but i don't think storing this as array would be the right thing to do as this is a JSON response
EliasGPS
EliasGPS2y ago
ah yeah but wait
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
what the diffrent between those two lines? thx
ghoulam
ghoulam2y ago
Yoo FileName should recieve your command executer
EliasGPS
EliasGPS2y ago
or is that supposed to be powershell.exe?
cap5lut
cap5lut2y ago
ur start.Arguments is probably not treated like u expect it
ghoulam
ghoulam2y ago
since you are executing with powershell use powershell.exe as FileName yes
EliasGPS
EliasGPS2y ago
ah ok but when i use that
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
i don't get anyu output
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
this is the eroor
cap5lut
cap5lut2y ago
u probably have output in stderr, as ur arguments r treated like 7 arguments, not as 2
EliasGPS
EliasGPS2y ago
it stopped because of the space
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam2y ago
damnn things escalated too fast
EliasGPS
EliasGPS2y ago
you mean double backdalsh
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
you maybe want to see my powershellf ile bruih
EliasGPS
EliasGPS2y ago
cap5lut
cap5lut2y ago
thats wrong, it would be something like start.Arguments = "--file \"C:\\some path\\script.ps1\"";
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
i can't follow anymore so you mean that is it actually really easy?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
yeah but you don't understand
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
that script is a twitter api script
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
that pulls information
ghoulam
ghoulam2y ago
i dk why you want to put this in array where the output is clearly a json
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
i used twiiter script for api yes i mean powerhsell script
ghoulam
ghoulam2y ago
you can basically create a class that represents your response and then deserialize it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
i'm getting crazy
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam2y ago
i hope he will send you the official twitter link 😂
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut2y ago
the whole command would be powershell.exe --file "C:\some path\script.ps1" with being the --file "C:\some path\script.ps1" the argument, if u put the latter into a string literal in c#, u need to escape the backslashes and the the double quotes. thus u end up with "--file \"C:\\some path\\script.ps1\"" as string literal
EliasGPS
EliasGPS2y ago
we had to do that so yaeh
ghoulam
ghoulam2y ago
executing a ps script from a supposed twitter script won't finish peacefully 😂
EliasGPS
EliasGPS2y ago
is it so weird to use powerhsell for twitter api?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
ohh man i'm getting really crazy huh --file is supposed to be in the string
cap5lut
cap5lut2y ago
well, i didnt want to confuse further, @"--file ""C:\some path\script.ps1"""; is shorter ofc
ghoulam
ghoulam2y ago
yes @tebeco is right use the rest api and that's it but you can also go on with this problem as challenge but just use another sample script i don't trust the one you have
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
whats wtronnnggg?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam2y ago
-file not --file
EliasGPS
EliasGPS2y ago
yeah i can fix that
cap5lut
cap5lut2y ago
it is in the string, its the first argument, the double quotes within the string literal are to mark the whole path (including the whitespaces) as the second argument
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut2y ago
naah, the folder name is DevOps & Security
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
i made my own script with my twitter development account
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
and than used this
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
EliasGPS
EliasGPS2y ago
it foking word worked thx everyone because
cap5lut
cap5lut2y ago
that was some failed white space escaping to get the path inlcuding whitespaces as 2nd argument (https://discord.com/channels/143867839282020352/1041076192209539115/1041110135076032622)
MODiX
MODiX2y ago
EliasGPS#5554
Quoted by
<@!233139962277658624> from #Powershell result to c (click here)
From EliasGPS#5554
React with ❌ to remove this embed.
EliasGPS
EliasGPS2y ago
last year i needed powerhsell to pull information from twitter with an api so that why i used powerhsell and now i need to make a new project with c# and if i can use tiwtter api and other stuff than that will be ,ice maybe one more question
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
but it worked now so is it possible to set this data in an array?
ghoulam
ghoulam2y ago
using CliWrap; using CliWrap.Buffered; using System.Diagnostics; //Method 1 : using CLI Wrap lib var result = await Cli.Wrap("powershell"). WithArguments("-file C:\\Users\\okbb\\Desktop\\test.ps1"). ExecuteBufferedAsync(); string outresult = result.StandardOutput.ToString(); Console.WriteLine("---------------"); Console.WriteLine(outresult); //Method 2 : usig the built-in Process class Process p = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "powershell.exe"; startInfo.Arguments = "-file C:\\Users\\okbb\\Desktop\\test.ps1"; p.StartInfo = startInfo; p.StartInfo.RedirectStandardOutput = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); Console.WriteLine("---------------"); Console.WriteLine(output);
EliasGPS
EliasGPS2y ago
ghoulam
ghoulam2y ago
this can be done in two ways
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut2y ago
tbh i would recommend starting to learn the c# basics first, u made quite simple mistakes. and instead of ur script i would probably use a library like https://github.com/linvi/tweetinvi
GitHub
GitHub - linvi/tweetinvi: Tweetinvi, an intuitive Twitter C# librar...
Tweetinvi, an intuitive Twitter C# library for the REST and Stream API. It supports .NET, .NETCore, UAP (Xamarin)... - GitHub - linvi/tweetinvi: Tweetinvi, an intuitive Twitter C# library for the R...
EliasGPS
EliasGPS2y ago
yeah i'm a beginner c# i alrady use that in my powerhsell script
ghoulam
ghoulam2y ago
it was a quit challenge i never redirected the stdout let alone running a ps script from C# thanks for it and i hope you got what you needed.
cap5lut
cap5lut2y ago
(u should also keep attention to stderr in such cases - or better avoid it all along ;p)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam2y ago
i think i'll take a deeper look into it tomorrow as it is 11.30 pm where i live i am looking like a zombie already
EliasGPS
EliasGPS2y ago
me too but thx everyone
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPS2y ago
thx
cap5lut
cap5lut2y ago
np, assumed that already ;p good luck on further progress, but i would really recommend just porting that script to c# and use it "natively" in ur program instead of in another process
EliasGPS
EliasGPS2y ago
i have no idea what you mean and how to start to it but i will try lol
cap5lut
cap5lut2y ago
well, instead of the powershell script which u invoke, simply write a method in ur program that does the job ;p
EliasGPS
EliasGPS2y ago
ahh
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
✅ This post has been marked as answered!