PowerShell and .Net > 2.0


Recently I started working on a Windows machine to configure it as a build agent for jenkins. One of the requirements was to make PowerShell work with .Net Version 4.5.

Windows 7 ships with PowerShell 2.0 by default (Don't go by the folder structure that still says v1.0). You can get the real details by typing $psversiontable in the PS console.

The CLRVersion is the .Net Version loaded by the shell. Most often it is 2.0. Powershell runs by default on .Net version 2.0 or less.

In case you get errors about loading assemblies even after loading them in the code, most likely you are not using the correct .Net Version. To load .Net versions greater than 2.0, the following settings need to be done.

ERROR :
Unable to find type [System.IO.Compression.CompressionLevel]: make sure that th
e assembly containing this type is loaded.
At C:\jenkins\workspace\SSENext-Core-Win\scripts\win\finalize-core-package.ps1:
9 char:56
+ $Compression = [System.IO.Compression.CompressionLevel] <<<< ::Optimal
    + CategoryInfo          : InvalidOperation: (System.IO.Compression.Compres 
   sionLevel:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound


STEP 1 :
In the folder C:\Windows\System32\WindowsPowerShell\v1.0\ and C:\Windows\SysWOW64\WindowsPowerShell\v1.0\ create two files - powershell.exe.config and powershell_ise.exe.config, with the following content

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="<.Net Version>" />
        <supportedRuntime version="<.Net Version>" />
    </startup>
</configuration>

The .Net Version can be picked up from C:\Windows\Microsoft.NET\Framework\ for the config files placed in the PowerShell folder under System32 folder and from C:\Windows\Microsoft.NET\Framework64\ for the config files placed in the PowerShell folder under SysWOW64

STEP 2:
Add the following registry keys to the system


reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1

Relaunch PowerShell and it'll load the most recent .Net Version as opposed to 2.0.

Comments

Popular posts from this blog

To DR or Not To DR

High Availability NAT for AWS VPC with Multiple Private Subnets.

Load Balancer with SSL offloading - nginx + HAProxy