PowerShell OS Support Matrix

This post comes as courtesy of Chrissy LeMaire and Carlos Perez. PowerShell is already shipped with Windows Operating System. Since it was released 10 years ago, different versions of PowerShell have been shipped through different versions of Windows. You could also upgrade to another version by installing Windows Management Framework. It would be quite useful to know what version of PowerShell is shipped with what version of Windows and up to what you can upgrade to get most out of it.

Carlos Perez was able to spend some of him time and put together a nice matrix about it, which you can see below: Continue reading “PowerShell OS Support Matrix”

Validate and restrict input integer length in PowerShell

Sometimes, while getting user input, you may want it to restrict it to certain characters. This may be for various reasons depending upon the situation. While there is no direct cmdlet or function to do this, you can use regex to help you to determine if the input matches certain pattern. For example, let’s consider that we want to restrict integer length input by user to 6 characters. for this, we can use below regex:

^\d{6}$

In action:

Continue reading “Validate and restrict input integer length in PowerShell”

Compare files with Visual Studio

Visual Studio 2015 file comparison tool can be considered good enough for most of the use cases. Before learning how to compare files with Visual Studio, I used to use WinMerge, or another tool to compare. Not anymore of that!

What is still missing in the IDE is a small menu that allows us to select 2 files and compare them. From command line, you can do it with devenv.exe /diff which compares two files. It takes four parameters:
SourceFile, TargetFile, SourceDisplayName(optional), TargetDisplayName(optional) Continue reading “Compare files with Visual Studio”

Working with ConvertTo-Json output issues

As you are aware, we can use ConvertTo-Json cmdlet to convert an object to Json output format using PowerShell. However, there is something you need to be aware of while using conversion. By default, it does not work with very large objects (containing of multiple sub-objects) and converts them properly. This is because of the fact that the Depth parameter for ConvertTo-Json has a default value of 2. Let’s understand what this means.

For our example, we’ll create a JSON file with below details first and save it on to your local machine as new-json.json

Continue reading “Working with ConvertTo-Json output issues”

Azure Resource Group Error: A parameter cannot be found that matches parameter name ‘EnvironmentName’

Recently, if you have been trying to deploy Azure Resource Group template using Visual Studio, you might see below error:

[ERROR] Add-AzureRmAccount : A parameter cannot be found that matches parameter name 
[ERROR] 'EnvironmentName'.
[ERROR] At line:1 char:2379
[ERROR] + ... xmg' -AccountId 'myemail@gmail.com' -EnvironmentName 'AzureC ...
[ERROR] +                                                  ~~~~~~~~~~~~~~~~
[ERROR]     + CategoryInfo          : InvalidArgument: (:) [Add-AzureRmAccount], Param 
[ERROR]    eterBindingException
[ERROR]     + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Azure.Commands. 
[ERROR]    Profile.AddAzureRMAccountCommand
[ERROR]  
[ERROR] Run Login-AzureRmAccount to login.

Continue reading “Azure Resource Group Error: A parameter cannot be found that matches parameter name ‘EnvironmentName’”

Save profiles for all Azure subscriptions in one go

As discussed in one of the previous blog posts, we can use PowerShell to help create persistent logins. Now consider scenario, where you have access to multiple azure subscriptions. Off course, you can download and save AzureRM profile for each one of the them. However, there are two major issues:

  1. AzureRM profile downloaded is associated with a token by default and it expires in a few days.
  2. If you have too many subscriptions, it can be tiresome to first select subscription and then save the profile.

Continue reading “Save profiles for all Azure subscriptions in one go”

Working with REST APIs in PowerShell

REST APIs are getting more and more common these days. It is important to learn them to be successful in the DevOps field. In previous blog post, we have used Invoke-WebRequest cmdlet to access the data available to an anonymous user.

PowerShell makes working with rest API’s easy. Starting with PowerShell v3, the cmdlets Invoke-RestMethod and Invoke-WebRequest were introduced. The difference between the two is quite small, Invoke-RestMethod simply being a slightly more convenient wrapper around Invoke-WebRequest as it only returns the content, omitting the headers. Continue reading “Working with REST APIs in PowerShell”