Determine .NET Framework versions installed using PowerShell

Users can install and run multiple versions of the .NET Framework on their computers. When you develop or deploy your app, you might need to know which .NET Framework versions are installed on the user’s computer. Note that the .NET Framework consists of two main components, which are versioned separately:

  1. A set of assemblies, which are collections of types and resources that provide the functionality for your apps. The .NET Framework and assemblies share the same version number.
  2. The common language runtime (CLR), which manages and executes your app’s code. The CLR is identified by its own version number (see Versions and Dependencies). Continue reading “Determine .NET Framework versions installed using PowerShell”

Ignore self-signed certificates in PowerShell

When using windows PowerShell as REST client, you may encounter certificate invalid issues for various reasons. The most likely reason should be a self-signed certificate or a invalid common name certificate or sometimes not adding SAN names in the certificates. This may cause your script to break if it relies on fetching data from remote server when communicating on HTTPS.

To avoid SSL Certificate trust issues if using HTTPS, we can use below PowerShell function to help: Continue reading “Ignore self-signed certificates in PowerShell”

Using POST method in REST API in PowerShell

In one of previous blog post, we discussed how to work with using REST APIs in PowerShell. In this blog post, we’ll discuss how to use POST method in REST API.

POST involve sending both a body and headers.

  $person = @{
      first='joe'
      lastname='doe'
   }
   $body = (ConvertTo-Json $person)
   $hdrs = @{}
   $hdrs.Add("X-API-KEY","???")
   $hdrs.Add("X-SIGNATURE","234j123l4kl23j41l23k4j")
   $hdrs.Add("X-DATE","12/29/2016")
   Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType 'application/json' -Headers $hdrs
   ConvertTo-Json

The above on several lines is easier to read than one long line: Continue reading “Using POST method in REST API in PowerShell”

Debugging PowerShell in Visual Studio Code – Part 02

In this blog post, we’ll continue from where we left earlier in the previous post on debugging PowerShell using Visual Studio code. You may be beginning to notice that the debugging features offered in Visual Studio code are far ahead in terms of usability.

Not only that, we can set line breakpoints (which can be specified by pressing F9 on code line in reference), we can also set conditional breakpoints, functional breakpoints and trace breakpoints.

A functional breakpoint can be defined as breakpoint which is invoked on calling of a particular function. You can set a function breakpoint to break into the debugger not only on a particular function invocation but also on an alias, a built-in command, or application invocation.  Continue reading “Debugging PowerShell in Visual Studio Code – Part 02”

Debugging PowerShell in Visual Studio Code – Part 01

In one of the previous post, we discussed how to use visual studio code for PowerShell development. Writing scripts in Visual Studio code is now more or less at par with ISE (Integrated scripting environment) and in a few cases, is more useful. However, you do need to be able to make sure that whatever code you have written in PowerShell, should be able to run as intended. Also, if its not working as intended, you should be able to debug and see where you went wrong. In this blog post, we’ll discuss how to use Visual Studio code for debugging. These features are provided by the PowerShell extension, or, more accurately, by the PowerShell Editor Services module which comes with the PowerShell extension. PowerShell Editor services not only provides language services to the Extension (which in turns provide services to Visual Studio code) but also provides useful debugging features. Continue reading “Debugging PowerShell in Visual Studio Code – Part 01”

Difference between URI, URL and URN

It has been my observation that many of the tech engineers do not know the difference between URI, URN and URLs. For many of them, they are same. For very of them, rest of them and few others are confused. So through this blog post, I intend to clarify that difference. I guess this is more of the classical questions like what to formally call a web address and what do you mean by a web address in first place. In this age of varying protocols and APIs and methods, what is called as a web address is itself of a question. for example, ftp://files.metavrs.in may serve entirely different content than http://files.metavrs.in which may further differ that mailto:files.metavrs.in (these urls are just for understanding purposes only). Continue reading “Difference between URI, URL and URN”

Prevent overwrite of files by Copy-Item in PowerShell

The PowerShell command Copy-Item will overwrite a file if it exists by default. This is unless that file is marked Read Only in which case you can use the -Force switch to overwrite the file.

What if you want to only copy the file if it doesn’t exist? Here’s a quick PowerShell script that will complete this task:

$sourceFile = 'c:\temp\something.txt'
$destinationFile = 'c:\temp\1\something.txt'
if (-not (test-path $destinationFile))
{
  $opts = @{'path' = $sourceFile; 'destination' = $destinationFile; 'confirm' = $false}
  copy-item @opts
}
else
{
   $opts = @{'path' = $sourceFile; 'destination' = $destinationFile; 'confirm' = $true}
   copy-item @opts
}