Calling the Command Line in C# - Scripts, PowerShell, Automation, and more

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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're going to see how to build a c-sharp application that can call command line applications directly now if you don't know me my name is tim corey it's my goal to make learning c-sharp easier i do that by providing videos here on youtube multiple times per week plus i have weekly podcast i also provide courses on c-sharp web development and much more at iamtimcorey.com the profits from those sales are what pays for the free content here on youtube so everyone can have a great education in c sharp not just those who can afford it now this video as of most of my videos i'm going to create some source code if you want to copy that source code use the link in the description all right let's go over to visual studio this is visual studio 2022 community edition in case you're wondering and we're going to build a simple console application now a console application is something that you can then run from the command line but that's not the point here we're going to use the console application to run other commands so we're going we're going to call things like powershell and we're going to call into things like docker and and get or anything else you want to do right from our c-sharp application which means that even though i'm doing a console application here we could turn this into another type of application for example a a desktop application like winforms or jpf or a web application even where it would call these command lines on the server like blazer server or mvc or razer pages or api so there's a lot of different flexibility here on where we can do this work i just want to point that out that we'll be using the console application but we could do this c sharp code pretty much anywhere we can't do it in blaze or webassembly or in the new.net maui stuff that's about it all right let's create a new project console application we're going to call this let's call this our cli demo and our cli demo app and we'll put an attempt directories fine dot net 6 long term support this is a new feature right here i'm going to point out if you really don't like top level statements meaning the the initial file does not have a namespace or class declaration it's just program.cs and all the codes in there if you don't like that you can check this box and go back to the old way of doing things personally i like the new way of doing things i think that it makes a whole lot of sense to do this way but there is that option go back if you really feel like you want to that would be to check the box here we're going to leave it unchecked and hit create okay so once this gets started we're going to use let's get rid of our sample code here we are going to use a um a third-party library to help us out with this this third-party library is great it really makes a lot of this work so simple to do so i want to point this out this is the library we're going to use it's called cli wrap and i'll leave the link for this down the description definitely check it out give them a star as well it's always nice to be appreciated but we're going to do the install a cli wrap from our nuget package and then it has a lot of documentation here that can really help you out with how to do just about everything with this very very simple to use tool so there's even a video um up here that walks you through how to do it as well so just note that's available to you to uh to use it's down the description so right click on dependencies and say manage nuget packages and we're going to install cli wrap and we're going to say install and okay and now we have that done we're going to do is we're going to start playing with this which means we start with just a using cli wrap that will allow us to make calls to this okay where i start the very basics of how to do this just we can see it in use and then from there we can kind of play around with other things we could do so let's start off with actually going to add one more thing another using cli wrap dot buffered this makes life a little easier for us i'll explain what does basically what it does is it adds the ability to capture the input and output more easily so i'm going i've got docker installed on my machine now you may not have docker install your machine which means this command wouldn't work but if the docker is installed let's run a quick docker command so var it's called docker results equals await this is async await which means you may be wondering where is the um the async on this method because this is the top level statement which means you can't even see the method static void main well by default it allows now the use of asynchronous code in this file because it is marked as async so we can put async code in here without having to do anything special to to call it inside program.cs so we're gonna say await cli dot wrap that's the kind of the root of this the heart of it docker okay and then we're going to do something with this so we could just do execute async but i'm going to say dot with arguments i'm going to pass in some arguments and here's what i like to do now you can pass in arguments as one big string okay so you could say uh dot dot verge dash dash version that's my arguments cool but i prefer to pass in array all right so i prefer to say new array and then put inside of the curly braces um i'll move curly brace down here that item and the reason why because i can just add a comma here and put others and not have to worry about um escaping quotes and all the other stuff you have to do just kind of throw it in there it will put multiple versions make sure the spacing is right and all the rest so with arguments now it's still yelling at me because i'm not actually calling this yet so it really wants me to execute async but i'm going to do execute buffered async all right now this is where this right here this using cli wrap.buffered helps us out because normally it's just execute async is all we do but then this docker results won't have the command line information that i kind of want which is fine in certain circumstances so if this was a fire and forget thing then we wouldn't have to capture necessarily the command line output okay but in this case i want to capture the command line now let me pull up the terminal here so we have some comparison so i'm going to open up actually i'll command prompt and i'm going to say docker space dash dash version that's what we're doing with this command okay so this just gives me the version of docker it's installed on my machine it's all it does so this is the output of that command that's all we're really worried about at this point is getting that value okay so i'm going to run the docker command that's this command right here and i'm passing arguments that's just this is the only argument i have right now but i could pass in multiple if i wanted to and the results i want to see are this so i'm going to execute buffered async which then i have this docker results variable so i can say let's do a cw for console.writeline i'm going to say docker results dot we have a few things standard output or center error standard output and we have start time runtime and a few others okay so if you did not have if you just execute let's just do this execute async then when you see this you will not see the standard error or standard output you just see start time runtime exit code and things like that so i wanted to have a little bit addition here with the buffered async so that i can say dot standard output this is a string that is the output of that command which remember is going to be something like this so let's run this just as it is and there you go it's output this right here for me now this extra stuff that's actually comes from the from the console app itself the one the c sharp application so let's do this let's say console.readline at the end that way we don't get that message so we can tell what's actually coming from the uh the cli wrap so that that's this right here those two lines which there's a blank line notices a blank line right here um that's what we're actually capturing okay so we could use this to capture what the output is of a certain command so we've seen how it works how i set it up and how to use it but let's talk about why this is useful right now okay so with this right here we're just getting the version just to have something to capture but imagine for a minute that we had to run some kind of batch process or script of some kind that did some work and want to capture what work was done or what happened we could do that using this tool now we don't have to capture the output if we don't want i prefer to for pretty much everything i just put in a database so we can review it later but let's just say for a minute you want to run a batch process where we create a docker we did some work with docker let's say create a docker image and then we create from that container and spun that container up we could do all of that with just the arguments that's what we do on the command line but instead of running it from the command line we can run it right inside of our c sharp application automatically so we can do this let's say every night create a new instance of let's say our sql server populate the right information start it up and set it running every night that way we have a new version of our database that's refreshed or whatever every single night using this automated c-sharp application so that's one way i do this we can even change this let's just say i'll keep the code here so we have it let's actually copy all of this and then we'll comment it all out and i will paste it back in but this time i say git git for the source control system not g-e-t so let's just say git and actually git has the same dash dash version option so we can run this as well and we can get the value of get version is 2.36.0 dot windows.1 so that's what's installed on my machine but we could do git commands maybe you could clone down a repository maybe you can do a a get pull and get push every evening we could do merges or we could do you know check for new uh things on the server whatever you want to do with git we could do automatically or powered by c-sharp this means we can even create a get client of our own using a c-sharp application maybe you have some standard things you do get that you want to just automate instead of having to remember the commands or or try to get some client to do it through three or four or five steps well you could put this right into a c-sharp application have one button that you push and it just does this work for you so lots of cool stuff you could do there using the command line but let's say maybe you want to do some work with a batch file maybe you already have some kind of a batch file that you run but you want to execute it inside of a c-sharp application no problem we can do that so to do that let's first create a batch file so let's go over to this directory it's my d directory in temp and i'm going to um let's open the vs code vs code is like the best command line editor yes i trust everything so i'm going to say new file and we're going to say demo.bat sorry over here demo.bat and in here we can put a number of commands that a bat file is just a batch of commands from your command prompt so you could say a number of things we could say dir that gives you a directory we could and let's just do that just that just give me a directory now for you you'd probably do something else like cleaning things up or moving files around deleting files archiving files whatever you want to do but for us we're just gonna run dir that's it now that's already saved so let's uh move this off the screen and we're going to copy this again i'll comment it out and we're going to say um cmd results this time and what we're going to do here is a little bit different is i'm going to say d colon backslash temp slash demo dot bat that's going to run that batch file not to put the the at symbol in front which indicates this is a string literal so that the slashes don't get don't be aren't treated as an escaped character otherwise we could do is put two slashes but i find that to be ugly now we don't want to have this these command options here the the arguments so we can just get rid of that however well let's just just execute that let's just execute just this first so let's run this and here is what it captured it captured all this information about our command so our command ran and it ran notice in the c temp cli demo cli demo ursula emo app cli demo bin debug.net 6.0 and then here is the files in that directory and how big they are so that's where it ran this this command but maybe that's not exactly what i wanted to have happen maybe i wanted to run somewhere else no problem we can say dot with working directory and we'll put the again the app symbol for the uh the string literal we can say d colon slash temp so the place where the the demo file is running which will do a directory on that file not on or that folder not somewhere else let's run this again and now you can see that it ran the directory command on just this d colon temp directory and we have our demo.bat file and then the two options for this directory and go up one directory okay so that's how you run with command line arguments now imagine for a minute that i had some kind of error inside my batch file maybe there's some kind of problem where i'm trying to do some work and it's not happening or it's not working correctly so let's bring our command here and we're gonna say del testing.123 and that's going to delete the testing.123 file from the location where the batch file is called do you know where that what file that is well it doesn't exist so it's going to cause an error so if we were to run this let's just bring our command line up here so let's do a change directory to d temp whoops i have to do a d colon first now i'm in the temp directory of d there we go if i do a directory here cool that works but if i do a dl testing.123 it says i could not find that file that's an error something went wrong it didn't work the way i expected it to all right so let's run this command now we have both of these commands in this batch file all right so let's run this command now and we're going to get this so we have the directory call and that that worked just fine the next line is hey call delete testing.123 and then nothing what's what's the problem here what why isn't it showing us the rest of the command well that means we're capturing a standard output but if we capture the standard error they run this we'll see could not find d colon slash temp slash testing.123 it captures just the error so this way we can capture the standard output and see what you know the happy stuff that did work we can also capture the errors if there are any so we can check that standard error to see if there's anything in there that we need to know about that allows us to verify that our our scripts ran correctly that's great but you may not ever use batch files you probably are more familiar with powershell if you're doing anything with you know scripting as far especially when it comes to servers or working with systems so powershell is a really powerful set of tools that you can do a lot with inside of your computer so let's talk about running powershell that's that's a really cool thing to do so let's copy this we're going to comment it out and let's see a demo of running powershell i'll say powershell results here like so and we're not going to run this command we're just going to run powershell itself because powershell is actually something you can execute can say powershell and then execute a command so what do we want to execute well we're going to execute a script of some kind so let's create one so we're going to say new file and we'll say demo.ps1 for a powershell file and we're going to do here is we're going to just write some contents to the directory actually not in the directory we're just going to write it right to the output okay so we're going to say write dash host hello world that's a really simple powershell command now powershell you can do a ton of stuff with where you can actually do querying so you can instead of saying directory you can say directory and then let's grab just the name and the extension and the um the size of the file you can pipe all of that means like do a four each on those things and filter it down all the rest really cool stuff you can do a powershell but let's just write to the host now again if we want to see this actually work here is my powershell tab open in terminal i can just say write host hello world and see that that's what it does just writes out hello world okay just to demonstrate we are doing some kind of work now just you know like dir still works looks a little different okay but it it still works you can do a lot with that but um oops right host just writes out that value so that's we're going to do with demo.ps1 so we're going to say in the temp directory i'm going to say dot with arguments i'm going to say new array of arguments and inside here we're going to say um i'm going to give it the the full path just we have it so dtemp slash demo.ps1 that's the commit that's the uh the powershell script to run okay and we're gonna execute this buffered async and we're gonna grab not the standard error but the standard output if we run this it captures hello world so we have now executed our powershell script so as you can see we can call things directly such as docker or git don't forget net is also a command line tool we can run so we can do things like creating whole new projects and setting up new things in projects all automatically from another c sharp application so you can run docker git power shell dot net and so much more inside of a c-sharp application we can script all of this and then have it execute whatever you want using our c-sharp application we can create our own guise for some of these uh these tools where maybe you've found you know there are certain things lacking or your process just doesn't line up with a standard gui tool one of the things that i often notice is that with docker i have certain things i like to do well i could create a little gui to do things like clean up all of the images i'm not using anymore or make sure that i've shut down all the images that are running and clear everything out or something like that we can create one button that does all of that in one using this cli wrap so there's lots of really great power you have accessible here and don't forget that if we actually go to the cli demo and we go to open folder and file explorer and then go to bin debug net 6.0 clidemo.exe that actually creates an executable file this console application does they could then run from a c sharp application so it's kind of inception like we could create a c sharp application that executes another c sharp application and even passes in arguments to it so that we can automate a lot of stuff and call it automatically so there's a lot you can do a cli wrap tool and it allows really opens the doors to as much automation as you want to do you can create your own continuous integration continuous deployment process right inside of a c-sharp application if you wanted to do that so if you want to keep things in-house you don't want to go out to like github actions or azure devops or something like that well you could do it in-house using a tool like this and create all of your logic in c sharp which you already know or you can mix it with other things as well like power shell or batch commands or whatever else so lots of stuff you can do here but i wanted to show you this because i wanted to kind of open your eyes to the possibilities of how you can do more with automation inside of c sharp the really the the uh opportunities here are pretty much endless and this is the reason why most things anymore have a command line option especially a command line first option people often ask me why should you ever learn get command line because there's always always great get gui's well yes there are but i've often found that they fall down at times in fact i did a full course on git and when i was showing off different gui's one of the gooeys failed to work fully it had an error and i'm like no problem we've already learned how to complete this we dropped into the command line and just completed the action so knowing how to use the command line is really powerful because those guys are actually built on top of the command line but more than that if you want to do anything with automation where a tool calls these products you have to have a command line version this is why we have those command line versions because we want continuous integration we want continuous deployment we want to have automated testing we want to have all these things so that we can make our lives easier make our jobs go by faster make get things done more quickly the way i do that is to automate as much as possible and this is a great tool for helping us get there okay so i would love for you to go visit this site give him a give him a star tell him thank you for for his work to commute for the community this tool is awesome if you have anything to do with command line or want to practice more check this tool out it makes life so much easier and i just scratched the surface of everything that can be done in the command line from a c sharp application thanks for watching if you have any comments leave them down below let me know what you think as always i am tim cory [Music] [Applause] you
Info
Channel: IAmTimCorey
Views: 34,215
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6, powershell, batch script, bat, bat file, automation, c# automation
Id: WrSZrGoPYyc
Channel Id: undefined
Length: 27min 39sec (1659 seconds)
Published: Mon Jun 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.