Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
visual-studio-simple-example.md136 linesDownload Raw Back to cmdlet-example
1# Building a C# Cmdlet with Visual Studio2 3This document describes steps for building a C# Cmdlet with Visual Studio in 2 ways:4 5* [Building a C# Cmdlet for PowerShell Core](#building-a-c-cmdlet-for-powershell-core-with-visual-studio)6* [Building a C# Cmdlet for PowerShell Standard 3.0](#building-a-c-cmdlet-for-powershell-standard-30-with-visual-studio)7 8## Building a C# Cmdlet for PowerShell Core with Visual Studio9 10This demonstrates how to build your own C# cmdlet for PowerShell Core with Visual Studio.11Targeting for PowerShell Core means that the cmdlet may not work against Windows PowerShell if you take dependencies on new APIs introduced in PowerShell Core.12 13We will use the free [Visual Studio Community 2017](https://visualstudio.microsoft.com/vs/).14 151. When installing Visual Studio 2017 select `.NET Core cross-platform development` under `Other Toolsets`16  ![Step1](./Images/Step1.png)17 181. Create new C# project `SendGreeting` of type `Class Library (.NET Core)`19  ![Step2](./Images/Step2.png)20 211. Now we need to setup PowerShell Core reference assemblies.22  In `Solution Explorer` right click on project `Dependencies` and select `Manage NuGet Packages...`23In the top-right corner of the package manager click on the small `Settings` sprocket icon that is to the right from `Package source` dropdown.24By default, there will be only `nuget.org` package source in `Available package sources` list.25Add another package source with name `powershell-core` and source `https://powershell.myget.org/F/powershell-core/api/v3/index.json`26![Step3](./Images/Step3.png)27 281. In the package manager select new `powershell-core` in `Package source` dropdown, select `Browse` tab, type in `System.Management.Automation` in the search and select `Include prerelease`.29  It should find `System.Management.Automation` package, select it and it will show package details; install it using `Install` button.30![Step4](./Images/Step4.png)31 321. Add the code of cmdlet:33 34    ```csharp35    using System.Management.Automation;  // PowerShell namespace.36 37    namespace SendGreeting38    {39        // Declare the class as a cmdlet and specify and40        // appropriate verb and noun for the cmdlet name.41        [Cmdlet(VerbsCommunications.Send, "Greeting")]42        public class SendGreetingCommand : Cmdlet43        {44            // Declare the parameters for the cmdlet.45            [Parameter(Mandatory = true)]46            public string Name { get; set; }47 48            // Override the ProcessRecord method to process49            // the supplied user name and write out a50            // greeting to the user by calling the WriteObject51            // method.52            protected override void ProcessRecord()53            {54                WriteObject("Hello " + Name + "!");55            }56        }57    }58    ```59 60    At this point everything should look like this:61    ![Step5](./Images/Step5.png)62 631. Build solution (F6); The `Output` window will print the location of generated cmdlet DLL:64  ![Step6](./Images/Step6.png)65 661. Start PowerShell Core, run `Import-Module` on DLL path from previous step and run cmdlet:67  ![Step7](./Images/Step7.png)68You can also run the same cmdlet on Linux and other systems that PowerShell Core supports:69![Step8](./Images/Step8.png)70 71## Building a C# Cmdlet for PowerShell Standard 3.0 with Visual Studio72 73Steps below show how to build your own C# cmdlet for PowerShell Standard 3.0 with Visual Studio.74Targeting PowerShell Standard 3.0 means that the same module will work against PowerShell Core as well as Windows PowerShell v3 and newer,75however, you are limited to a subset of the available PowerShell APIs.76 77We will use the free [Visual Studio Community 2017](https://visualstudio.microsoft.com/vs/).78 791. When installing Visual Studio 2017 select `.NET Core cross-platform development` under `Other Toolsets`80  ![StdImage1](./Images/Step1.png)81 821. Create new C# project `SendGreetingStd` of type `Class Library (.NET Standard)`83  ![StdImage21](./Images/Std21.png)84 851. On project properties verify that `Target framework` is `.NET Standard 2.0`:<br />86  ![StdImage22](./Images/Std22.png)87 881. Now we need to setup reference assemblies.89  In `Solution Explorer` right click on project `Dependencies` and select `Manage NuGet Packages...`90In the top-right corner of the package manager select `nuget.org` package source, select `Browse` tab, type in `PowerShellStandard.Library` in the search and select `Include prerelease`.91It should find `PowerShellStandard.Library` package, select it and it will show package details; install it using `Install` button.92![StdImage3](./Images/Std3.png)93 941. Add the code of cmdlet:95 96    ```csharp97    using System.Management.Automation;  // PowerShell namespace.98 99    namespace SendGreeting100    {101        // Declare the class as a cmdlet and specify and102        // appropriate verb and noun for the cmdlet name.103        [Cmdlet(VerbsCommunications.Send, "Greeting")]104        public class SendGreetingCommand : Cmdlet105        {106            // Declare the parameters for the cmdlet.107            [Parameter(Mandatory = true)]108            public string Name { get; set; }109 110            // Override the ProcessRecord method to process111            // the supplied user name and write out a112            // greeting to the user by calling the WriteObject113            // method.114            protected override void ProcessRecord()115            {116                WriteObject("Hello " + Name + "!");117            }118        }119    }120    ```121 122    At this point everything should look like this:123    ![StdImage4](./Images/Std4.png)124 1251. Build solution (F6); The `Output` window will print the location of generated cmdlet DLL:126  ![StdImage5](./Images/Std5.png)127 1281. Now cmdlet can be run on systems supported by PowerShell Standard;<br />129  For example:<br />130  On PowerShell Core on Windows:131![StdImage61](./Images/Std61.png)132On PowerShell Core on Linux:133![StdImage62](./Images/Std62.png)134On Windows PowerShell on Windows (this requires [.NET Framework 4.7.1](https://dotnet.microsoft.com/en-us/download/dotnet-framework/net471)135![StdImage63](./Images/Std63.png)136