C
C#3y 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
EliasGPSOP3y 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
Buddy3y 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
EliasGPSOP3y ago
yeah but look
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
i can't use create so there is somthing wrong
EliasGPS
EliasGPSOP3y ago
bruh my powehsell is glitching i alraedey did that
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
sometimes its red and sometimes not
Buddy
Buddy3y ago
You have not imported the namespace System.Management.Automation
EliasGPS
EliasGPSOP3y ago
like this?
EliasGPS
EliasGPSOP3y ago
Buddy
Buddy3y ago
No
EliasGPS
EliasGPSOP3y ago
ow ok
EliasGPS
EliasGPSOP3y ago
using?
Buddy
Buddy3y ago
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
here you see red
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
and now good how? nothing changed its changing every 2 sec
Buddy
Buddy3y ago
Probably because VSCode / Omnisharp
EliasGPS
EliasGPSOP3y ago
yeah vscode is glithcing as hell
Buddy
Buddy3y ago
If you're a beginner and you're on Windows, I highly recommend Visual Studio which is different from Visual Studio Code
EliasGPS
EliasGPSOP3y ago
i'm not a real beginner, but i'm a beginner in c# ohh serious?
EliasGPS
EliasGPSOP3y ago
but i'm used to program in vscode
Buddy
Buddy3y 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
EliasGPSOP3y ago
oh ok thx visual studio is so weird to i open a folder and i see nothing
EliasGPS
EliasGPSOP3y ago
Buddy
Buddy3y ago
You should not open the folder but the project itself .csproj or if a `.sln file exists, then choose that over *.csproj`
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
i need to click on this right?
Buddy
Buddy3y ago
Make sure you install the workloads
Buddy
Buddy3y ago
EliasGPS
EliasGPSOP3y ago
yeah thats already done
Buddy
Buddy3y ago
Then press that
EliasGPS
EliasGPSOP3y ago
ok
Buddy
Buddy3y ago
Buddy
Buddy3y ago
Open that if no *.sln file exists
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
ok
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
bruh
Buddy
Buddy3y ago
press view
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
i cliked solution explorer
Buddy
Buddy3y ago
Now you can navigate it just click on a file and start going
EliasGPS
EliasGPSOP3y ago
oh thx i'm so noob in visual studio and c# unbelievable hahah
Buddy
Buddy3y 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
EliasGPSOP3y ago
i did that ahh
Buddy
Buddy3y ago
Sweet!
EliasGPS
EliasGPSOP3y ago
does not exists
Buddy
Buddy3y ago
Right-click the project in solution explorer press Manage Nuget packages
Buddy
Buddy3y ago
Buddy
Buddy3y ago
Browse for that nuget package, click it, then press install
Buddy
Buddy3y ago
EliasGPS
EliasGPSOP3y ago
good but he can't find Powerhsell
Buddy
Buddy3y ago
Class scope versus method scope it can only be inside a method
EliasGPS
EliasGPSOP3y ago
ahh
EliasGPS
EliasGPSOP3y ago
he still can't find Powerhsell
Buddy
Buddy3y ago
hover over Powershell, click the lightbulb on the left. What options do you have?
EliasGPS
EliasGPSOP3y ago
Buddy
Buddy3y ago
Did you install the package successfully?
EliasGPS
EliasGPSOP3y ago
yes but i used another version
Buddy
Buddy3y ago
Ah, that's why. It is only available in version 7.0.0 and above
EliasGPS
EliasGPSOP3y ago
because i use .net core 6 and i can't use the latest version how do i fix that
Buddy
Buddy3y ago
You can't?
EliasGPS
EliasGPSOP3y ago
bruh
Buddy
Buddy3y ago
You should be able to
EliasGPS
EliasGPSOP3y ago
wait what you said i can't and than you said i can?
Buddy
Buddy3y ago
I asked if you couldn't hence the questionmark
EliasGPS
EliasGPSOP3y 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
ghoulam3y 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
EliasGPSOP3y ago
you serious?
ghoulam
ghoulam3y ago
Yep
EliasGPS
EliasGPSOP3y ago
omg that would save my life maybe
ghoulam
ghoulam3y 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
EliasGPSOP3y ago
oh man if this works that will save my life
ghoulam
ghoulam3y ago
I hope it will wish you all the luck
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
it does not support .ps1 i think
ghoulam
ghoulam3y 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
EliasGPSOP3y ago
huh to run my script is just a .\Script.ps1
ghoulam
ghoulam3y ago
But you're right i think this package doesn't support running ps scripts since they are not lunched like other scipts
EliasGPS
EliasGPSOP3y ago
uhu yes
ghoulam
ghoulam3y 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
EliasGPSOP3y 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
EliasGPSOP3y ago
i think its possible
ghoulam
ghoulam3y 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
EliasGPSOP3y ago
just in my c# file? you mean in the cliwrap?
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
i now got this
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
and get this message so that is better but i am still not getting the ouput
ghoulam
ghoulam3y 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
ghoulam3y ago
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y 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
ghoulam3y ago
yep you have to use \ double \
EliasGPS
EliasGPSOP3y ago
ow ok hahaha but than it doesn"t work
ghoulam
ghoulam3y ago
first try to execute it like me to see if everything works
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
it asks me this when i try to run
ghoulam
ghoulam3y 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
EliasGPSOP3y ago
ow ok hahaha ahh wait you are using a consoleapp but i'm using ASP or am i wrong?
ghoulam
ghoulam3y ago
yes am using console app
EliasGPS
EliasGPSOP3y ago
i will try it with a console app maybe that will work?
ghoulam
ghoulam3y ago
the trick is to redirect the output of that command into a variable and that idk how 😂
EliasGPS
EliasGPSOP3y 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
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
when i run my script it is an array
ghoulam
ghoulam3y 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
EliasGPSOP3y ago
Yeah but thats a process class?
ghoulam
ghoulam3y ago
EliasGPS
EliasGPSOP3y ago
how do i use that can you send me your code?
ghoulam
ghoulam3y ago
i successfully stored the output in a string variable
EliasGPS
EliasGPSOP3y ago
i mean a screenshot of your code in your editor are you serious? crazy
ghoulam
ghoulam3y ago
use snipping tool of windows
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
it kinda worked but i don't see otput
EliasGPS
EliasGPSOP3y ago
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam3y ago
but i don't think storing this as array would be the right thing to do as this is a JSON response
EliasGPS
EliasGPSOP3y ago
ah yeah but wait
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
what the diffrent between those two lines? thx
ghoulam
ghoulam3y ago
Yoo FileName should recieve your command executer
EliasGPS
EliasGPSOP3y ago
or is that supposed to be powershell.exe?
cap5lut
cap5lut3y ago
ur start.Arguments is probably not treated like u expect it
ghoulam
ghoulam3y ago
since you are executing with powershell use powershell.exe as FileName yes
EliasGPS
EliasGPSOP3y ago
ah ok but when i use that
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
i don't get anyu output
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
this is the eroor
cap5lut
cap5lut3y ago
u probably have output in stderr, as ur arguments r treated like 7 arguments, not as 2
EliasGPS
EliasGPSOP3y ago
it stopped because of the space
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam3y ago
damnn things escalated too fast
EliasGPS
EliasGPSOP3y ago
you mean double backdalsh
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
you maybe want to see my powershellf ile bruih
EliasGPS
EliasGPSOP3y ago
cap5lut
cap5lut3y ago
thats wrong, it would be something like start.Arguments = "--file \"C:\\some path\\script.ps1\"";
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
i can't follow anymore so you mean that is it actually really easy?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
yeah but you don't understand
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
that script is a twitter api script
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
that pulls information
ghoulam
ghoulam3y ago
i dk why you want to put this in array where the output is clearly a json
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
i used twiiter script for api yes i mean powerhsell script
ghoulam
ghoulam3y ago
you can basically create a class that represents your response and then deserialize it
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
i'm getting crazy
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam3y ago
i hope he will send you the official twitter link 😂
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut3y 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
EliasGPSOP3y ago
we had to do that so yaeh
ghoulam
ghoulam3y ago
executing a ps script from a supposed twitter script won't finish peacefully 😂
EliasGPS
EliasGPSOP3y ago
is it so weird to use powerhsell for twitter api?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
ohh man i'm getting really crazy huh --file is supposed to be in the string
cap5lut
cap5lut3y ago
well, i didnt want to confuse further, @"--file ""C:\some path\script.ps1"""; is shorter ofc
ghoulam
ghoulam3y 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
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
whats wtronnnggg?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam3y ago
-file not --file
EliasGPS
EliasGPSOP3y ago
yeah i can fix that
cap5lut
cap5lut3y 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 User3y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut3y ago
naah, the folder name is DevOps & Security
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
i made my own script with my twitter development account
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
and than used this
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
EliasGPS
EliasGPSOP3y ago
it foking word worked thx everyone because
cap5lut
cap5lut3y 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
MODiX3y ago
EliasGPS#5554
Quoted by
<@!233139962277658624> from #Powershell result to c (click here)
From EliasGPS#5554
React with ❌ to remove this embed.
EliasGPS
EliasGPSOP3y 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 User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
but it worked now so is it possible to set this data in an array?
ghoulam
ghoulam3y 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
EliasGPSOP3y ago
ghoulam
ghoulam3y ago
this can be done in two ways
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut3y 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
EliasGPSOP3y ago
yeah i'm a beginner c# i alrady use that in my powerhsell script
ghoulam
ghoulam3y 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
cap5lut3y ago
(u should also keep attention to stderr in such cases - or better avoid it all along ;p)
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
ghoulam
ghoulam3y 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
EliasGPSOP3y ago
me too but thx everyone
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
EliasGPS
EliasGPSOP3y ago
thx
cap5lut
cap5lut3y 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
EliasGPSOP3y ago
i have no idea what you mean and how to start to it but i will try lol
cap5lut
cap5lut3y ago
well, instead of the powershell script which u invoke, simply write a method in ur program that does the job ;p
EliasGPS
EliasGPSOP3y ago
ahh
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server