If you look into the .NET framework architecture, you might wonder where does PowerShell fit in…
One of the assumptions can be that it is the .NET compliant language and the whole PowerShell ecosystem resides within that box. But that is not the case.
PowerShell leverages Common Language Runtime (CLR) and Base Class Library for the operation and capabilities. Basically it utilizes and extends .NET capabilities rather than being part of .NET compliant languages.
OK, so now when I am done with lecturing about PowerShell and .NET components and their relationship, lets see how we can utilize and benefit from the .NET ecosystem in our PowerShell code.
As already mentioned, (aka spoilered) in the title we will be focusing on Add-Type
cmdlet. We will see how we can utilize this “Swiss knife cmdlet” as the gateway to the .NET.
Add-Type
has been created to enhance the functionality of your PowerShell code. Since PowerShell v5 you can add/define classes directly in PowerShell. (And that is the topic for another post. Coming soon…)
All of us have been in the situation when you need to utilize some DLL to enhance you PowerShell scripts, this is when Add-Type
comes to the rescue.
Add-type -AssemblyName .\ITTask.dll -PassThru -Verbose
This is only part of the core functionality of the Add-Type
cmdlet. Initially it allows you to define .NET class within the PowerShell and/or use existing assembly.
In addition to adding .NET compiled assemblies we can add type definition using source code.
$ITTaskDefinition=@" public class ITTask { public string TaskName { get; set; } public string TaskDescription { get; set; } public bool IsAutomated { get; private set; } public string Automate() { this.IsAutomated = true; return "Task '" + this.TaskName + "' sucessfully automated."; } public override string ToString() { return this.TaskName + " - " + (this.IsAutomated ? "Automated" : "Pending"); } } "@ Add-Type -TypeDefinition $ITTaskDefinition -PassThru -Verbose
Including ability to use source code files.
Add-Type -Path .\ITTasks.cs -PassThru -Verbose $p=@{ "TaskName"="User onboarding" "TaskDescription"="Create user, assign roles and licenses } $Task=New-Object -TypeName ITTask -Property $p $Task.Automate()
Moreover, in Windows PowerShell we can use other .NET language like Visual Basic. PowerShell Core supports only C#.
Visual Basic Example
Let’s look into the variation of our ITTask class.
Add-Type -Language VisualBasic -TypeDefinition @" Public Class ITTask Public Property Name As String Public Property Description As String Public Property IsAutomated As Boolean Public Sub New(ByVal name As String, ByVal description As String, ByVal isAutomated As Boolean) Me.Name = name Me.Description = description Me.IsAutomated = isAutomated End Sub Public Function Execute() As String If Me.IsAutomated Then Return String.Format("Executing automated task: {0}", Me.Name) Else Return String.Format("Executing manual task: {0}", Me.Name) End If End Function End Class "@ $automatedTask = New-Object ITTask("Backup Data", "Automate the data backup process.", $true) $manualTask = New-Object ITTask("Install Updates", "Manually install software updates.", $false)
In addition, Windows PowerShell also have JScript language support. Which is Microsoft’s implementation of ECMAScript (the standard upon which JavaScript is based). JScript is similar to JavaScript but was developed by Microsoft for use in Internet Explorer and Windows scripting. Nowadays, it is considered legacy technology, but it is still out there, so let’s review the example.
JScript Example
$jsType = @" class ITTask { var name; var description; var isAutomated; function ITTask(name, description, isAutomated) { this.name = name; this.description = description; this.isAutomated = isAutomated; } function Execute() { if (this.isAutomated) { return 'Executing automated task: ' + this.name; } else { return 'Executing manual task: ' + this.name; } } } "@ Add-Type -Language JScript -TypeDefinition $jsType -PassThru -Verbose $a=[ITTask]::new("Backup Data", "Automate the data backup process.", $true) $a.Execute()
Conclusion
Because PowerShell it built on top of .NET, after adding .NET & .NET Core types into our PowerShell we ca use them not only in our scripts but interactively. With PowerShell we have the luxury to add any external application and .NET Classes, as well as implement tasks at hand with the help of .NET if PowerShell realization is not sufficient for some reason.
Referecence(s)
Thanks a lot for reading.