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...

Комментарии • 95

  • @jricardobaptista
    @jricardobaptista Год назад +2

    Congratulations, you've created the best C# youtube channel ever. I love all the contents, your channel have the most complete content...

  • @BooBar2521
    @BooBar2521 Год назад +5

    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!

  • @delw1138
    @delw1138 2 года назад +1

    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.

  • @Rhodair
    @Rhodair 2 года назад +3

    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

  • @dilipjain9581
    @dilipjain9581 2 года назад +3

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      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

    • @dilipjain9581
      @dilipjain9581 2 года назад

      @@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.

  • @waynehawkins654
    @waynehawkins654 2 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      Awesome! I am glad it was so helpful.

  • @marvelcz8974
    @marvelcz8974 2 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      Uno is on my list of topics to cover. That's a great system.

  • @swedishprogrammer
    @swedishprogrammer 2 года назад +1

    Thanks for sharing C# content! 😇

  • @alexdpaiva83
    @alexdpaiva83 2 года назад

    Thanks for the tip from Brazil 👏🏻👏🏻

  • @xarioln
    @xarioln 2 года назад +1

    definitely a missed naming opportunity :/ ... Cling wrap!

  • @parsagachkar3140
    @parsagachkar3140 10 дней назад

    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!

  • @anthonysteinerv
    @anthonysteinerv 2 года назад +1

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I am glad it was so helpful.

  • @ajaysingh9001
    @ajaysingh9001 Год назад

    Hi Tem,
    Greate stuff, I am trying to use this but stuck on Passing Read-Host Input parameter for Powershell script

  • @harshitgoel566
    @harshitgoel566 Год назад

    How to run scripts requiring Administrator access in power Shell from C# code

  • @user-xt8dx8zo8i
    @user-xt8dx8zo8i Год назад

    As always Tim Loved you video !!!

  • @pasdilhan
    @pasdilhan 2 года назад

    Great! useful stuff.. Thanks for sharing Tim

  • @ajvenable7937
    @ajvenable7937 2 года назад

    Great stuff Tim. Thanks!

  • @noelfrancisco5778
    @noelfrancisco5778 2 года назад

    This is an awesome tool, thank you for sharing :)

  • @bluehippotech
    @bluehippotech 2 года назад +1

    For powershell specifically you can also use the powershell sdk and system.management.automation and subscribe to multiple streams

  • @mikebreeden6071
    @mikebreeden6071 2 года назад

    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

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      I'm glad it will be helpful.

  • @jacekbialek409
    @jacekbialek409 Год назад

    Hi Tim, great video. Is it possible to use the same tool inside docker ?, and call scripts from host ?

  • @donnyjames2304
    @donnyjames2304 2 года назад

    question...can this package be used to call excel macros?

  • @eduardofelippetto
    @eduardofelippetto Год назад

    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!

  • @ashutoshpattnaik5446
    @ashutoshpattnaik5446 2 года назад

    Hell Yeah. This is the video.

  • @intelligentcoding689
    @intelligentcoding689 Год назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      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.

  • @demariners
    @demariners Год назад

    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?

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      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.

    • @Steaky1300
      @Steaky1300 5 месяцев назад

      You could always use an app.manifest file.

  • @andywalter7426
    @andywalter7426 2 года назад

    Actually you can do command lines in the maui if you decide to only target windows.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      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.

  • @h.kargar71
    @h.kargar71 Год назад

    very nice

  • @FellTheSky
    @FellTheSky Год назад

    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

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      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.

  • @micro2743
    @micro2743 2 года назад

    I honestly can't imagine doing this unless there was no API or any other way of obtaining the same information.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +3

      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.

  • @niravdaraniya7792
    @niravdaraniya7792 Год назад

    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.

  • @Rajeshsingh-ws5th
    @Rajeshsingh-ws5th 2 года назад

    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?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      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.

  • @pxnchx93
    @pxnchx93 2 года назад

    How does this Cliwrap library stack up against just simply using the System.Diagnostics.Process class?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +2

      It is an abstraction layer on top that adds a lot of great features.

  • @bootsector13
    @bootsector13 2 года назад +1

    its like a c# inside a c# in a c# code.. multiverse really exist!

  • @mjuniorjr5942
    @mjuniorjr5942 2 года назад

    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!

  • @victorcapeluto8663
    @victorcapeluto8663 2 года назад

    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

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I don’t have a video on that. Sorry.

  • @hillarysagwa5064
    @hillarysagwa5064 2 года назад

    thank you

  • @andywalter7426
    @andywalter7426 2 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      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.

  • @dreamdancer8212
    @dreamdancer8212 2 года назад

    How exactly did you pull up that terminal? Great stuff btw.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      That's the Windows Terminal. I believe you still have to install it yourself on Windows 10 but it comes standard in Windows 11.

    • @dreamdancer8212
      @dreamdancer8212 2 года назад

      Thanks a lot. I will have a try . . . : )

  • @andywalter7426
    @andywalter7426 2 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +3

      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.

  • @AdamsTaiwan
    @AdamsTaiwan 2 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      That is tricky to get right.

    • @AdamsTaiwan
      @AdamsTaiwan 2 года назад

      @@IAmTimCorey Yes, but not too bad and I only have one .exe for the old .Net Fx.

  • @khanfaizan05
    @khanfaizan05 2 года назад +1

    First view.. first comment

  • @KoScosss
    @KoScosss 2 года назад +1

    Flashbang warning: 3:21

  • @mnezon1314
    @mnezon1314 2 года назад

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +6

      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.

  • @stepbackandthink
    @stepbackandthink 2 года назад

    So a lesson in coding just turns out to be an advert for third party software.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +4

      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?

    • @stepbackandthink
      @stepbackandthink 2 года назад

      @@IAmTimCorey I never mentioned a problem.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +3

      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.

    • @stepbackandthink
      @stepbackandthink 2 года назад

      @@IAmTimCorey Perhaps that could be reflected in the title then.

    • @ChristianHaugland74
      @ChristianHaugland74 2 года назад +3

      @@stepbackandthink maybe you could Step Back And Think?

  • @angelman26
    @angelman26 2 года назад

    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.