« Previous 1 2 3 Next »
Convert Linux shell commands into PowerShell cmdlets
New Harmonies
PowerShell for Linux-Based Commands
PowerShell inherently comes with extensive cmdlets that can handle most administration tasks that occur on Windows. When you use PowerShell on Unix-style operating systems, you have to call many command-line tools manually, which explains why I chose ifconfig
(the Unix equivalent of the command-line IP configuration utility ipconfig
) as the next test candidate. Microsoft provides a turnkey program example with the file ifconfig.Crescendo.json
, which you analyze in the first step:
Import-CommandConfiguration .\ifconfig.Crescendo.json
The output is the command table shown in Figure 2. Note that Crescendo is running on Windows 10, an operating system that knows nothing about ifconfig
. The output containing information about the control file shows various metadata because the Crescendo specification not only lets you specify the commands to be executed and parameters required by them, but Microsoft lets you store information, which PowerShell then includes in the help system.
Open the ifconfig.Crescendo.json
file in an editor of your choice (Listing 3). Both Visual Studio and the basic Visual Studio Code let you edit PowerShell files with syntax highlighting: I do not recommend editing PowerShell scripts with Notepad.
Listing 3
ifconfig.Crescendo.json
01 { 02 "$schema": "../src/Microsoft.PowerShell.Crescendo.Schema.json", 03 "Commands": [ 04 { 05 "Verb": "Invoke", 06 "Noun": "ifconfig", 07 "Description": "This is a description of the proxy", 08 "OriginalName": "ifconfig", 09 ""Aliases": [ 10 "Get-NetworkConfiguration" 11 ], 12 "Usage": { 13 "Synopsis": "Run invoke-ifconfig" 14 }, 15 "Parameters": [ 16 { 17 "Name": "Interface", 18 "OriginalName": "", 19 "Description": "This is the description for a parameter", 20 "ParameterType": "string", 21 "DefaultValue": "" 22 } 23 ] 24 } 25 ] 26 }
Again, the OriginalName
parameter takes the name of the binary you want PowerShell to activate. In the world of Crescendo, you do not generally describe Unix commands by a fixed path, but by the name you need to enter at the command line. However, there is nothing wrong with specifying the full path of the binary in "sensitive" situations.
Next is the metadata. In addition to the Aliases
block, which supplies additional strings intended for activating the command, a Usage
block accommodates help text.
Parameters
In many cases, the commands packaged by Crescendo take parameters that allow the user to customize the program's behavior. PowerShell cmdlets are also parameterizable, so Microsoft has created a tool in the Crescendo environment for passing in parameters. Specifically, the Commands
block expects another field that describes the parameters. It is important to note that you do not need to declare all the parameters supported by the binary to be called in your Crescendo wrapper. It is sufficient to expose only those settings that you want to make addressable by the caller. In the case of the ifconfig
wrapper, the only useful parameter according to the developer is the name of the network interface to be analyzed ("Name": "Interface"
).
Like the Commands
block, the Parameters
block has an OriginalName
field. It now determines the "prefix" under which the binary file to be called expects the information to be delivered. You can use Description
to store information that shows the user more details about the role of the parameter. The ParameterType
line defines the supported data type, and DefaultValue
is the default value to be passed in. You can use the data types area to add all the variable types supported by PowerShell; besides string
, which is used here, integer
is a possible data type.
The parameters array lets you specify optional additional attributes. For example, if you pass in
"Mandatory": true
the caller of the wrapper is required to assign a value to this parameter:
"Parameters": [ { ..."Name": "Id", ..."OriginalName": "", ..."Mandatory": true, ..."ValueFromPipelineByPropertyName": true .} ],
Another neat example of Crescendo parameters can be found in the wrapper around the Unix tar
utility that, first, defines a prefix for the parameter and, second, specifies that the parameter should act as a bit switch:
{ "Name" : "Detail", "OriginalName" : "-l", "ParameterType" : "switch" },
Parameters configured with a "ParameterType" : "switch"
type are not supplied with a value within the wrapper call. The user of the wrapper can only omit or use the parameter: If it is found in the call to the cmdlet generated by Crescendo, it will also show up in the call to the binary.
Once again, the next thing to do in the ifconfig
example is to transpile the wrapper to create PowerShell code. Note that the commands are still running on Windows:
Export-CrescendoModule -ModuleName ".\ifconfig1.psm1" -ConfigurationFile ".\ifconfig. Crescendo.json"
PowerShell proves to be flexible in terms of script execution. Theoretically, nothing prevents you from adding your module to the Windows 10 workstation cmdlets cache (and executing it there):
Import-Module -Name .\ifconfig1.psm1 Invoke-ifconfig
However, because of the lack of an ifconfig
binary, the program fails to execute and outputs an error.
Running Crescendo Wrappers on Ubuntu
Ubuntu 20.04 LTS comes with a version of ifconfig
. Unfortunately, Canonical does not equip the Linux distribution with a PowerShell interpreter out of the box, so you will need to install it:
sudo apt-get update sudo apt-get install -y wget apt-transport-https software-properties-common wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb sudo apt-get update sudo apt-get install -y powershell
Assuming everything works, you can launch PowerShell by typing pwsh
. The next step is to transfer the files from the Windows workstation to the Unix machine and install them:
Import-Module -Name './ifconfig1.psm1' -Force
Now ifconfig
can be called from PowerShell with Invoke-ifconfig
(Figure 3). Thanks to the parameter declaration, you can also specify which network interface you want to run the command against (e.g., Invoke-ifconfig eth0
).
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)