Calling the Command Line in C# - Scripts, PowerShell, Automation, and more
HTML-код
- Опубликовано: 19 авг 2024
- 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 applications directly.
github.com/Tyr...
Full Training Courses: IAmTimCorey.com
Source Code: leadmagnets.ap...
Mailing List: signup.iamtimc...
Congratulations, you've created the best C# youtube channel ever. I love all the contents, your channel have the most complete content...
Thank you!
Dude, thank you for sharing this. You explain so well what possibilities I gain using this with easy examples. This was exactly what I was searching for!
You are welcome.
Thanks for showing the CliWrap library, didn't know it was out there. I have made many CLI apps for WSUS server automation, automated summary reports for backups and other administrative function automation.
You are welcome.
this looks like such an awesome way of creating personal command scripts and cron jobs
I'd messed with bash and started getting into Python once I saw there was a typing library, but C# is definitely my preferred
wasn't aware of CliWrap until now, but this instantly has me excited to automate more
Awesome!
Thank you for keeping updated with Microsoft technologies! I have bought 5 courses from your site, and one of them was Getting Started with Azure. I would like to learn more Azure Cloud services in depth and would love if you keep updating existing course content of Azure. I am much interested in Azure Advance course.
While I won't just be adding new content to that course, I will be adding more courses that cover other cloud services. There are so many out there that I won't be able to cover them all, but as people express a need for certain services, I'll try to get them covered. You can find out what people are asking for (and ask for some of your own) at suggestions.iamtimcorey.com
@@IAmTimCorey I just suggested there. Thanks for the awesome contents and courses. I would like to understand and study Azure Kubernates with Microservice architecture (Docker) in depth knowledge on Azure Cloud course. If you create a course for it, I will be the one who will buy first for 100% sure. Many many thanks in advance for it.
Nice Tim. As someone that wear two hats, Network Manager and Software Developer, this is going to allow me to developed tools to manage many things on our Servers. Maybe running as a Services program. Thank you so much.
Awesome! I am glad it was so helpful.
Well ... all that is missing now is video about Uno Platform and then all my target tech stack is here on this channel. Well done Tim.
Uno is on my list of topics to cover. That's a great system.
Thanks for sharing C# content! 😇
You are welcome.
Thanks for the tip from Brazil 👏🏻👏🏻
You are welcome.
definitely a missed naming opportunity :/ ... Cling wrap!
lol
this is actually cool, but I was wondering if I could monitor a process that I've started! like starting an AVD and waiting for when it's ready (a specific message to be printed in stdout) then fire-up Appium to control it! this would ease my CI pipelines for my mobile apps a lot!
No way, I didn't know that Nuget package, like a month ago or something like that I was kind of trying to do all of this with Process library, and I managed to do it, but holy shit it's a lot easier with this. Thanks for this, now there's nothing I would use python for, unless this doesn't work as good with WSL/Linux.
I am glad it was so helpful.
Hi Tem,
Greate stuff, I am trying to use this but stuck on Passing Read-Host Input parameter for Powershell script
How to run scripts requiring Administrator access in power Shell from C# code
As always Tim Loved you video !!!
Thanks!
Great! useful stuff.. Thanks for sharing Tim
You are welcome.
Great stuff Tim. Thanks!
You are welcome.
This is an awesome tool, thank you for sharing :)
You are welcome.
For powershell specifically you can also use the powershell sdk and system.management.automation and subscribe to multiple streams
Thanks for sharing.
Wow! I got to about 14:30 when I thought... "I know what I could do with that". My monitoring applications could have really use that. I can think of all kinds of uses. ... Sure, open my eyes, but that's what you do, isn't it.
... Perhaps a funny thing... I've been interviewing some lately and I keep getting asked questions about ASP.Net details like stuff in the Request pipeline. Well, I don't remember. That was 6 months ago I experimented with .Net 6. I've gone on to other things like architectural possibilities like this. I did .Net 6. That was then and my focus has moved on. Now I'm doing something like this or DynamoDb in my Lambda website, but they will never know that
I'm glad it will be helpful.
Hi Tim, great video. Is it possible to use the same tool inside docker ?, and call scripts from host ?
Yes, it is.
question...can this package be used to call excel macros?
Hey Tim, this is a great video. However, I cannot make it work with 'dotnet ef dbcontext scaffold' or 'Scaffold-DbContext''. I would like to make the DB First scaffolding process triggered from a Console App. Could you please help? Thank you so much!
Hell Yeah. This is the video.
Thank you!
hi, Thanks for the amazing videos. I have a problem to place auto-installation of SqlLocalDB into the MSIX package, or something like online installation while installing my Application from MS-Store. I'm using .NET CORE 3.1 and want to run a simple SQL-Database from storage (like D:\Documents\myApp\myDatabase). it's working on my machine, but the users may have not SqlLocalDB installed on their own. would you please help me.
That’s a tricky one to solve because MSIX is designed to be self-contained so installing it in your own isn’t feasible. You might want to look at using SQLite instead.
Thank you for this! Helps a lot. One question though. I am trying to run something that requires Admin priv. Is there a way to to do it with this library?
I looked into this a while ago. I believe the best solution is to run your calling app as administrator. Test that and see if it works.
You could always use an app.manifest file.
Actually you can do command lines in the maui if you decide to only target windows.
Even if that were technically possible, why would you create a MAUI application that could only target Windows? There are much better options for that.
very nice
Thanks!
is it possible to execute a c# script inside a .bat file and get a return?
I've managed to do the first part, but failed in getting the return out of the c# script to the .bat code
By C# script, I am assuming you mean a C# application (like a console application). Yes, it can return a value, but more commonly the Console.WriteLine is the output.
I honestly can't imagine doing this unless there was no API or any other way of obtaining the same information.
This isn't something you do every day, but there aren't APIs for a lot of these tasks. They are specifically command line applications, so you need to call them via the command line for automation purposes. This is what all DevOps systems do for CI/CD.
Thank you
Question : can we use "Cliwrap" inside the docker container ? means I have .Net core application and which uses "Cliwrap" next i have created docker images of this .Net core application and then run this docker image on linux machine. is it possible ?, I have tried this but its throwing error.
Thanks Tim for wonderful stuff, keep posting and helping. One question but not related to this post. Is minimal api (c#) is same as node.js restapi kind of pattern?
That's a mix of a few different things. An API in C# (whether minimal or not) is like a Node.js API. Both of them can implement REST, although the actual REST spec is not something that anyone actually fully implements.
How does this Cliwrap library stack up against just simply using the System.Diagnostics.Process class?
It is an abstraction layer on top that adds a lot of great features.
its like a c# inside a c# in a c# code.. multiverse really exist!
There you go.
Hi, Tim!
I was trying to make this question in your question website, but it has more than 500 chars. Can you explain me or make a video about it? The question:
Let's suppose that we have two libraries, let's call them x and y. These two are dependent on a third library, named z. Library x depends on a version of library z. The y library depends on a different version of the same z. These verses are incompatible.
How does .net manage this?
Does the concept of dlls make this possible? the assemblies(dlls) is similar to containers concept?
In a dll do you have a .net running? if yes, why don't the .net standard 2 and 2.1 libraries talk to each other, if they will run their own dotnet machines, for example?
If I create library in dotnet 5 version, it can be called into a dotnet 6 app? And If I create a dotnet 6 library, it can be called by a dotnet 5 app?
Thanks!
Hi Tim, this is a great tool that I didn't know it existed, I'm trying to run an async process (bat file) and show what's happening to the user but I'm in a windows forms app, how can I do that? It is a big file though. Thanks
I don’t have a video on that. Sorry.
thank you
You are welcome.
One question I have is I tried the git command even using cmd and says does not recognize the command. How does somebody fix that issue. Seems like a bug in the cmd because git is obviously supposed to be recognized.
It isn't a bug, it is how your machine is set up. First, you need to make sure git is installed. Second, you need to check your PATH variable to be sure git is added to it. In Windows, you cannot just call whatever you want wherever you want. You need to navigate to the folder where the EXE is located to run it (or call it by its full path). The way around this is to put that EXE's file location on the PATH variable. That's what git will do by default when you install it.
How exactly did you pull up that terminal? Great stuff btw.
That's the Windows Terminal. I believe you still have to install it yourself on Windows 10 but it comes standard in Windows 11.
Thanks a lot. I will have a try . . . : )
One question is why you are using a nuget package when you can do the command stuff without the nuget package. I would have liked to see how the command line stuff would have been done without the nuget package just in case a person does not want to use that nuget package or even create their own stuff.
We are using the NuGet package because we don't want to reinvent the wheel over and over. The point of development isn't to write code. The point is to accomplish the goal. Almost no one's end goal is to call the command line. That's one of the things they need to do, but their actual goal is beyond that (for example, it might be to automate a process - calling the command line is just the means to that end). If you want to see how to do all of that work on your own, go to the repository and read the code.
I created my own installer that wraps cmd, powershell and calls management api and can run an internal script. Windows flagged it as a virus.
That is tricky to get right.
@@IAmTimCorey Yes, but not too bad and I only have one .exe for the old .Net Fx.
First view.. first comment
Thanks!
Flashbang warning: 3:21
Thanks for the video. Its a nice lib, but their activism in the github repo makes me uneasy, especially since there was a cyber attack on Russian computers with a NodeJS package. I am not pro Russia or Russian myself, but i do not like the way they force you to support their political beliefs to use their software. What about Russian developers who do not support Putin, which is the majority of young people in Russia? Pretty distasteful behavior from the maintainers. I will pass on using this package.
Well, this is where you have to remember that the owner of a package has the right to specify how a package is used. As for the owner "forcing you to support their political beliefs", I don't see that in their statements. What the author (who is Ukranian and who has had their country's civilians and military personnel alike killed by the Russian invaders) is saying is that the invasion by Russia is wrong and that there isn't a "both sides" to one country killing another country's civilians. Let's put this in more personal terms. I imagine you have a family. Imagine if someone came into your house, killed your mom and brother, and was trying to claim your house as theirs while hunting down the rest of your family. Would you be ok with someone saying that action was "political" and that "both sides" have a point? That's the very scenario that the author of this NuGet package is facing. I'm very much not ok with how Russia is acting and I'm very much ok with how Ukrainians feel about it.
So a lesson in coding just turns out to be an advert for third party software.
I'm going to assume you are new to software development and not just being willfully ignorant. In C#, we use NuGet packages to help make our lives easier. Even .NET is broken out into NuGet packages so that we can add only what we need to build an application. We don't just reinvent the wheel with every project. In this case, a developer created a NuGet package (for free) that extends the System.Diagnostics.Process object in .NET. This package makes it easier to work with Process so that we can more quickly, more efficiently, and more safely talk to command line systems.
So, what I just did is show you how to make your development easier and better for free with an open-source library. Where exactly is the problem?
@@IAmTimCorey I never mentioned a problem.
Saying the video is "an advert for third party software" was the problem. This is a lesson in how to write software in C#. It isn't an advertisement. The implication with your statement is that the video somehow isn't actually teaching you how to write C# but is instead just advertising to you to get someone's software. That's not what is happening.
@@IAmTimCorey Perhaps that could be reflected in the title then.
@@stepbackandthink maybe you could Step Back And Think?
Hi Tim. I'm new to C# so I appreciate your video and explanations. I have a question that no one has been able to help me with so far. I wrote a PowerShell script with a GUI in Windows PowerShell ISE that works pretty well. I saw some GUIs made with XAML on RUclips and was immediately interested in learning it and building a GUI with it for the commands in my PowerShell Script. I have searched hi and low and cannot find a single person that can show me how to run my code in C#. I was able to run something simple like this as a test,
private void BtnFindComputer_Click(object sender, RoutedEventArgs e)
{
PowerShell.Create().AddScript("Start-Process Notepad.exe").Invoke();
}
But that's not what I'm looking to run. How can I run something like this,
private void BtnFindComputer_Click(object sender, RoutedEventArgs e)
{
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $textbox1.Clear()
$LFilter = $textbox10.Text
$Username = Get-ADUser -LDAPFilter "(anr== $LFilter)" -Properties *
if ($Null -eq $Username.SamAccountName) {$textbox1.Text = "N\A"} else {$textbox1.Text = $textbox1.Text + ($Username.SamAccountName.ToString()) -replace "(CN=)(.*?),.*",'$2'}
}
Is this even possible? I can send you any code via email that could be helpful in understanding my goal. Thanks for any help that you can provide and thanks again for the video.