Code – PowerShell in C# – Part 01 of 05

(blog article under construction)

This post is part 1 of a series of 5 on writing C# applications using PowerShell modules of Skype for Business Server 2015.

I used Microsoft Visual Studio Community 2015 running Windows 10 and targeted Windows Desktop Application to .NET Framework 4.6.1.

We assume the “Skype for Business Server 2015 – Administrative Tools” are installed on your machine. they can be installed from the Skype for Business Server 2015 ISO file. After installation there is a directory with module “C:\Program Files\Common Files\Skype for Business Server 2015\ModulesSkypeForBusiness\SkypeForBusiness.psd1”.

In the App.config nothing special, just reference to .NET Framework 4.6.1.

App.config

Also in Program.cs nothing special, just launching the Form1. The code is intended for illustrating the PowerShell module integration concepts, therefor quite old-style monolithic.

Program.cs

I have made a reference to the System.Management.Automation assembly.

I started adding in the user interface buttons “Create workspace” and “Get commands” and a ComboBox.

Form1.cs

When the “Create Workspace” button is clicked, a runspace is initialised and the Skype for Business Server 2015 module is loaded. This process may fail depending on your “Get-ExecutionPolicy -List” settings. More information is available on http://go.microsoft.com/fwlink/?LinkID=135170. On my development machine I performed a “Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser”.

Form1.cs

When the runspace is created, we will perform our first PowerShell command. In our sample program we will perform a “Get-Command * -Module SkypeforBusiness -CommandType cmdlet” to retrieve the available cmdlets of Skype for Business module. Our pipe will consist of one command “Get-Command” and two parameters, hence the usage of methods AddCommand and AddParameter. The return object from Invoke() is a collection of PSObject instances that were written to the output stream during execution. PSObject is a wrapper class that adds PowerShell specific functionality around whatever the base object is. We will add all the retrieved cmdlets in our ComboxBox.

Form1.cs

In upcoming articles we will implement additional functionality and access the Skype for Business Server 2015 environment.