PowerShell Open Source – Take advantage of ‘#Requires -Version’ and new variables

Now that PowerShell Open Source is evolving, we need taking advantage of some if it features to build cross-platform scripts. Also, a good way to test the script functionality is to use either Linux or Windows 10 Bash environments.

Just keep in mind, for now you won’t be able to run you existing Windows PowerShell scripts or modules in another OS. Unless, you concentrate that write your scripts only using the PowerShell Core module. PowerShell Open Source is evolving.

Also, there are already modules available that can be use cross-platform thru PowerShell Gallery, and NuGet. Just make sure to search of Linux or Mac OS scripts.

Find-Module -tag linux


Using PowerShell existing features

Now, when creating cross-platform scripts, I found very useful to have the “ #Requires -Version x” statement. This statement is available in all versions of PowerShell.

Next, is to take advantage of the new PowerShell Open Source variables:
– IsCoreCLR
– IsLinux
– IsWindows


By just adding a conditional statement (“if”) to check which OS environment you are running the script. This makes it so convenient and portal across multiple OS environments.

Sample Script

To demostrate, *here’s a sample script for searching Debian packages I use on my Window 10 and Linux systems:

<# .NOTES =========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.129 Created on: 10/31/2016 2:18 PM Created by: Maximo Trinidad Organization: PutItTogether Filename: CrossPlatTest.ps1 =========================================================================== .DESCRIPTION A description of the file. #>

#requires -version 6
function Get-DebianFiles
Param (
[string]$FileExtension = "*.deb"
write-host "Only in PowerShell 6.0"
if ($IsWindows -eq $true)
$PackageLocation = 'C:\Users\mtrinidad\Downloads';
Write-Verbose "Windows Selected" -Verbose;

if($IsLinux -eq $true)
$PackageLocation = '/mnt/c/Users/mtrinidad/Downloads';
Write-Verbose "BASH Selected" -Verbose;

Get-ChildItem $PackageLocation -File -Filter $FileExtension -Recurse;

#. C:\TempPoshXplat\Scripts\PowerShell\CrossPlatTest.ps1

## Get-DebianFiles -FileExtension "*.deb";
Get-DebianFiles -FileExtension "*.deb";

## ---------- end of scripts -----------##

*Note: In order for the ” #Requires -Version..” statement to work, it need to execute from a script file *.ps1. The statement won’t work from inside a function.

The above script is required to run in PowerShell 6 because is using both new variables: IsWindows and IsLinux. Keep in mind, this sample have hardcoded path to where I’ve stored my debian packages and it can be improved more.



There still a lot of work to be done in both Windows 10 Bash and PowerShell Open Source. The variables to properly identify the OS in non-Windows system hasn’t been implemented yet. But, it will be available sometime soon.

In the meantime, keep exploring, testing, and contributing in Github!

Posted in PowerShell | Comments Off on PowerShell Open Source – Take advantage of ‘#Requires -Version’ and new variables

SAPIEN PowerShell Studio – Simple sample using ComboBox in forms


The Form

This is a simple form showing the ComboBox component using PowerShell Studio.



For this sample I’m using a Simple Form in order to add components to it. In this case a ComboBox Component. At the same time we are adding a Label Component which I will use to display the selected item from the ComboBox.


The Code

Notice in the “Script” pane, when you create the form and added the ComboBox, it will also add a new function: “Load-ComboBox“.


This function,m under commented help, will give you examples on how to load items into the ComboBox component for the form to use.


This will make your life easy while building your forms.

But, you can provide your own script code to populate the component. Here’s an example code snippet:

## - Custom Code to Load into ComboBox:
[array]$lstItems = ("Server1", "Server2");
$cbPickLstItems.Text = $lstItems[0].ToString();

Of course, you can put the code into a function to keep consistency across the application.

Next, would be to create the code to add items into the ComboBox and compile the application. This code will be added into the “$ComboBox_Load” event. I’m using the SAPIEN already provided “Load-ComboBox” function.


Now, just run the application, save, and see the results.




The code we provided will only select the item. You’ll notice the results is not displayed yet.

In order to display the selected item we need to add another event called “.._SelectedIndexChanged“.

In order to create this event, go to the form and double-click on the ComboBox component and then we add the following code in the “$combobox1_SelectedIndexChanged” event.

$results = $ComboBox1.SelectedItem.ToString();
$labelResultsHere.Text = $results;

Then, run the application event and we finally see the displayed selected results.




It’s always fun to build quick apps with PowerShell Studio!

Posted in PowerShell | Comments Off on SAPIEN PowerShell Studio – Simple sample using ComboBox in forms

PowerShell – OneDrive Script Editing Available

In case you didn’t know, you can edit PowerShell scripts in your OneDrive folder. This makes it ideal for saving your code in the cloud and then access the scripts when needed from anywhere.



You can be very creative, and specially if working across plaform.

Check it out! Give it a try. The only thing you need is to have a Microsoft Outlook web account.

Feel free to experiment!


Posted in PowerShell | Comments Off on PowerShell – OneDrive Script Editing Available

PowerShell – SSMS v17 RC1 changes in SMO support for SQL Server vNext


Download available now. Get it here:

Yes! The SQL Server Management Studio v17 RC1 is the one updated for SQL Server vNext on Linux. This version brings new properties in SMO object to properly identify SQL Server vNext. So, using previous version of SMO object under Information collection won’t give you the correct information.


When using SSMS v17 RC1 you’ll get the correct information:


Also, you will notice the following properties added to the SMO Object “Information” class. Running the following command will give you the total count of properties on the “Information” Class:

($SQLSrvObj.Information | gm -MemberType Properties).count

1. On SQL Server 2014 – “Information” Class has 47 properties.
2. On SQL Server vNext – “Information” Class has 54 properties.

Using a previous version SSMS, in SMO, if we use the “Platform” property to identify a Windows machine as “NT x64”, this won’t work on a Linux machine. it still will show “NT x64” even in the GUI.


This is why we need to use SSMS v17 RC1. This have the needed changes to SMO to properly identify the Linux platform correctly.


Below is the list of added properties in the SMO “Information” class:


As you can see, we’ll need to revisit our customs SMO scripts if you need do inventory on our SQL Servers on our infrastructure.

Remember! The fun never stop when you’re learning new stuff!

Posted in PowerShell | Comments Off on PowerShell – SSMS v17 RC1 changes in SMO support for SQL Server vNext

SQL Server for Linux Cross-Platform Database Restore

Now that SQL Server vNext for Linux CTP1 is available, you want jump in and test you installing existing databases. You may want to use databases with compatibility to SQL Server 2012 and greater. As I’m not a full Linux Admin but this opportunity to setup and use SQL Server on Linux definitely has help stepup to learn about Linux OS.


Check out Microsoft SQL Server vNext for Linux page:

So, before you start working or trying to restore your Windows SQL Databases to SQL Server on Linux, there are a few things you may need to do. This will involved installing components which are not installed by default when first build your Linux System.

One thing I had to say with confidence! You WILL find all your answer on the internet. Any of the Linux Distro forum have your resolution to any of the issue you may encountered. Also, most of the time you Linux system will tell you (or gives a tip) what do with missing dependencies.

Other than that, there are Linux Application Updater solution to help keep your system up-to-date.


Here’s some tips on what’s needed to connect Windows to Linux shared folders in Ubuntu LTS 16.04.1

1. understand your SQL Server database Linux path – as you may know this is totally different than you Windows database location. In Linux, your databases will be preferably stored at ‘var/opt/mssql/data‘ which ti will be seem on SSMS as ‘C:\data’ folder and this may change on later builds.

Linux path to SQL Databases = ‘var/opt/mssql/data

Windows in SSMS shows Linux path as = ‘C:\var\opt\mssql\data’  (path corrected for SQL Server CTP1)


2. Create a Windows Shared folder – This one is easy to do using the Windows Explorer. But, you will need to play with assigning permission. To speed up the processes (for testing) use “Everyone” with Read-Only.


3. Creating a Linux Shared folder – You could use bash ‘sudo nautilus’ to create the folder using GUI. Or, straight command line:

$ sudo su
# mkdir /var/opt/mssql/data/TempSQLBackup
# chmod 777 TempSQLBackup

4. Install Linux application/Libraries/Dependencies – As I mention before, the internet will provide answers to any issues. Here I will provide some to the point solutions:

a. Install samba:

$ sudo apt-get install samba

b. *Add your domain name(or workgroup) to samba configuration:

$ sudo gedit /etc/samba/smb.conf

*note: Look for -> workgroup = WORKGROUP (or Domain)

c. Restart samba process:

$ sudo service smbd restart

5. Testing Linux can see the Windows Shared folder – Use this command:

$ smbclient -L IPAddr_or_Sysname -U UserName


5. Connect to your Linux to Windows Shared folder command: (It will prompt for the user password)

$ sudo mount.cifs // /var/opt/msql/data/TempSQLBackup -o user=max_t

6. Verify you can see the windows backup files on Linux. (use nautilus or bash command)


Now, we can successfully restore a database from a Windows Shared folder. No need to FTP your Windows backup to Linux FTP site.

Using PowerShell

From your Windows client, Using PowerShell to restore your Windows Database Backup to Linux.  Using the SQL PowerShell cmdlet “Restore-SQLDatabase” available when installing SQL Server Management Studio.
Here’s a sample script:

Restore-SqlDatabase -ServerInstance orion -Database AdventureWorks2014.bak -BackupFile "C:/data/TempSQLBackup";

Keep in mind, for now you can manage your “SQL Server vNext for Linux” (up to some extend) using either the existing SQL PowerShell cmdlets or buidling your own PowerShell scripts with SMO.

Here’s an example using SMO:


In above sample, although I have connected to a SQL Server Linux engine, the platform still showing as “NT x64”.  This issue has been addressed and will be corrected soon.

So, try running your Windows PowerShell scripts against your SQL Server Linux systems.

Keep Learning PowerShell!!

Posted in PowerShell | Comments Off on SQL Server for Linux Cross-Platform Database Restore

SQL Server vNext for Linux CTP1 is available now.

Yes! The doors are finally open for all interested in trying SQL Server for Linux. This is the first preview version and there are more to come. This was announced at the Microsoft Connect() event today.


Go to this Link which is very well documented and download SQL Server for you Linux Disto:


You can start connecting to your Linux version using the latest SSMS and even use PowerShell from your Windows desktop.


Go and Get It!

Posted in PowerShell | Comments Off on SQL Server vNext for Linux CTP1 is available now.

SAPIEN PowerShell Studio Deploying Your Application


In our previous blog we build a GUI application and created the executable file using either the “Build” or “Build & Run” option under the “Deploy | Packager” section.


Although, this is optional, we could have created an MSI installer which will produce all the necessary steps to deploy the GUI application.

The flow to build your MSI installer would be:

1. “Deploy | Installer” – “Settings” – To enter all the information needed about this GUI application.


2. Under “Product Details” – Describes your application in details. Notice you can chose any PowerShell version available.


3. “Files/ Folder” – This section will set where your MSI files will be stored. You can add more resource file with it.


4. “Signing” – (optional) Only if you want to sign your application solution with your signed certificate.


After everything under setting has been configured, proceed to “Build” the installer.


Now, we have created the GUI application MSI installer which can be distributed to other machines.

What about application “Deployment“?


Under the “Deployment” section, basically we can use it to collect all the script files and store documentation to be deploy somewhere on the network or media device.



PowerShell Studio is a RAD full developer solution for PowerShell development:

Download the product for a evaluation:

More information about PowerShell Studio:

Posted in PowerShell | Comments Off on SAPIEN PowerShell Studio Deploying Your Application

SAPIEN PowerShell Studio GUI Simplicity

In my last blog I show how easy it is to use a template to build a simple GUI application with a datagrid. Then, we enabled one of the sample code with the Script section of the “Load” button in order the process and display the results. And, this was just a start!



Now, how about making the GUI more functional so you can type any PowerShell command and the provide the results in the datagrid pane.
We can use the same GUI application we build and add a text box to be use to type our command.


Then, we need to add the following snippet code in the “Load” button script section:

$scriptblock = $executioncontext.invokecommand.NewScriptBlock($textbox1.Text);
$MyGridObject = $null; $MyGridObject = Invoke-Command -Scriptblock $scriptblock;
Load-DataGridView -DataGridView $datagridviewResults -Item $MyGridObject -AutoSizeColumns DisplayedCells;


So, after adding both the Text box and the PowerShell code, we got ourselves a function GUI that can run your PowerShell script and display the results on the datagridview. By typing the following one-liner then press the “Load” button:

$d = Dir -file; $d | Select Name, length, directory;


And, this is just out of the box UX with little customization.

Next Step

Now, you won’t be able to run the PowerShell GUI application outside of PowerShell Studio. The next step will be to create an executable GUI application with the following steps from the menu:

1. Go to the Deploy menu, and click on “Settings“.


2. In the “Engine Settings” , you make necessary changes to the application information.


3. Under “Execution Restrictions“, you can identify which environments can execute this application.


4. In “Version Information“, you can provide useful information for source control.


5. Finally, “Build Options“, in case you need some provide either Pre or Post commands during the build on this application.


6. *Now you can click either execute “Build” or “Build & Run” to create the executable.


*Note: The executable will be stored on the same folder you had created the application.


For more information

Download the product for a evaluation:

More information about PowerShell Studio:

This how simple it is to create a PowerShell executable GUI (also non-GUI) application in PowerShell Studio.

Next blog I’ll be covering buidling the MSI installer for this application.

Posted in PowerShell | Comments Off on SAPIEN PowerShell Studio GUI Simplicity

Creating Datagrid GUI in SAPIEN PowerShell Studio

Lets cut to the chase! Here’s a quick way to create Datagrid views using PowerShell Studio.


First, take advantage of the forms wizard to start building your GUI application.
Go to “File” then select “New Form” and Select a template: “Grid Template”.



This template already provide us with two buttons: “Load” and “Exit”. Take a look in the script tab and you’ll notice some events been already created for us to use. Also, read the commented lines because it give you a hint in how to populate the datagrid in the form.


Now, in the commented sample, you’ll find the function “Load-DataGridView” is used to push the data to the datagrid component. This function is located at line #2 in the “#region Control Helper Functions”. Just expand the regions to view the *function:

1. ConvertTo-DataTable
2. Load-DataGridView



*Note: these functions are only available when you select the Grid template. They are not available in their Snippets section.

You can go ahead the follow the commented sample to populate the form datagrid. Uncomment the following line and run the application:

$processes = Get-WmiObject Win32_Process -Namespace "Root\CIMV2"
Load-DataGridView -DataGridView $datagridviewResults -Item $processes -AutoSizeColumns DisplayedCells

This will load the datagrid with data and you can resize the window application.



As you can seen, right from the template you can start building PowerShell Gui’s with SAPIEN PowerShell Studio product.

Download the product for a evaluation:

More information about PowerShell Studio:

Posted in PowerShell | Comments Off on Creating Datagrid GUI in SAPIEN PowerShell Studio

Come and Celebrate PowerShell 10th Anniversary!

I just can’t believe I’ve been hooked to PowerShell since 2006. And, that I can’t stop using it.  This product change my life and it will be with us for a long time.

Here’s an image I showed on my first PowerShell presentation back in 2007 and I even build a CD with free resource information.


What a accomplishment from Microsoft PowerShell father Mr. Jeffrey Snover! He created PowerShell and nowaday is mostly available in most Operating Systems: Windows, Linux, and Mac OS. Here are a few slide from one of Jeffrey Snover PowerShell webcast back in April 2007.



I totally got it and I couldn’t stop learning about PowerShell. It did made my work easy thanks to this new .NET Scripting Automation Technology.

Now, let us not forget about all of our existing PowerShell Communities that throughout the years has done an excellent jobs to provide guidance adn direction to everyone wanting to learn PowerShell.


Join Microsoft PowerShell 10 Years Anniversary Celebration


Date: November 14th 2016 (A lond day event)
Where: MSDN Channel 9 – https://channel9.msdn.com/Events/PowerShell-Team/PowerShell-10-Year-Anniversary


Keep Learning about PowerShell!

Posted in PowerShell | Comments Off on Come and Celebrate PowerShell 10th Anniversary!