PowerShell: Check .net runtime and .net Core SDK is installed in a machine

I love to work on brand new machines since they uncover a plethora of developer issues including code loopholes. Today, I installed the Sitecore MVP instance on a Windows 11 machine. Although the Sitecore MVP site runs on Docker, there were a few PowerShell errors when I executed the Start-Environment.ps1. These are classic errors uncovered only in  a brand new machine (not necessarily Windows 11). So, here is the blog article discussing the errors and code fixes!

Since we are developers, we wish to catch exceptions in the code and provide proper messages for the user/developer rather than asking the next developer to follow instructions written in a github readme page! So, let us check the exceptions and fix the code. I have added the errors to the end of the article so that they don't disturb the flow of the article!

The Dotnet Errors are thrown due to the following line of code:


Now, no prizes for guessing the reason or solution. Since this is a brand-new machine, dotnet runtime wasn't installed and, so there is no dotnet folder under c:\program files:

Executing dotnet on the cmd line also proves the same:


Add the following lines before dotnet tool restore:





Execute Start-Environment.ps1 and now, get a relevant message rather than an exception:


There is also the url presented for download. 

There are two components to be installed:

- .NET Runtime
- .NET Core SDK

So, I use the url to install the dotnet runtime first:


I installed .NET 6.0 runtime. 


Note that I didn't install the .net core 3.1 sdk purposefully and if I execute the Start-Environment.ps1 now, I still get a relevant message:



Now, in case if I install a higher version of SDK say 6.0.x, the MVP site is not compatible so, the second check acts as a safety net there!

So, install .NET Core 3.1 SDK and that will add the following folder:


Just for reference in case if there are multiple SDK versions:


Now, run Start-Environment.ps1. Finally, all good:


PowerShell Code:

if ([Environment]::Is64BitProcess -eq [Environment]::Is64BitOperatingSystem)
{
$programFilesPath=${env:ProgramFiles}
}else
{
$programFilesPath=${env:ProgramFiles(x86)}
}

$dotnetFolder= "$programFilesPath\dotnet";
$dotnetexe="$dotnetFolder\dotnet.exe";
$sdk="$dotnetFolder\sdk";

#Generic Code that checks for .net sdk and .net runtime
if (!(Test-Path "$dotnetexe") -or !(Test-Path "$sdk")) {
Write-Host "Install dotnet Core 3.1 runtime and sdk from https://dotnet.microsoft.com/en-us/download" -ForegroundColor Yellow
exit 0
}

#Generic Code that checks for .net sdk 3.1.* 
$result=Get-ChildItem  "$sdk" -filter "3.1.*" -Directory | % { $_.fullname }
if (!$result) {
Write-Host "Install dotnet core SDK from https://dotnet.microsoft.com/en-us/download" -ForegroundColor Yellow
exit 0
}


Dotnet Errors:

dotnet : The term 'dotnet' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\mvp-site\Start-Environment.ps1:94 char:1

+ dotnet tool restore

+ ~~~~~~

    + CategoryInfo          : ObjectNotFound: (dotnet:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


Error - 2:

dotnet : The term 'dotnet' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\mvp-site\docker\tools\Scripts\Functions-SitecoreCLI.ps1:33 char:5

+     dotnet sitecore login --cm $CmHost --auth $IdHost --allow-write t ...

+     ~~~~~~

    + CategoryInfo          : ObjectNotFound: (dotnet:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


Pushing latest items to Sitecore...

dotnet : The term 'dotnet' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\mvp-site\docker\tools\Scripts\Functions-SitecoreCLI.ps1:40 char:5

+     dotnet sitecore ser push

+     ~~~~~~

    + CategoryInfo          : ObjectNotFound: (dotnet:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


dotnet : The term 'dotnet' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\mvp-site\docker\tools\Scripts\Functions-SitecoreCLI.ps1:45 char:5

+     dotnet sitecore publish

+     ~~~~~~

    + CategoryInfo          : ObjectNotFound: (dotnet:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


Dotnet SDK and Sitecore errors:

Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
  * You intended to execute a .NET program:
      The application 'sitecore' does not exist.
  * You intended to execute a .NET SDK command:
      It was not possible to find any installed .NET SDKs.
      Install a .NET SDK from:
        https://aka.ms/dotnet-download
Push-Items : Unable to log into Sitecore, did the Sitecore environment start correctly? See logs above.
At C:\mvp-site\Start-Environment.ps1:104 char:1
+ Push-Items -IdHost "https://id.$($HostDomain)" -CmHost "https://cm.$( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Push-Items

Pushing latest items to Sitecore...
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
  * You intended to execute a .NET program:
      The application 'sitecore' does not exist.
  * You intended to execute a .NET SDK command:
      It was not possible to find any installed .NET SDKs.
      Install a .NET SDK from:
        https://aka.ms/dotnet-download
Push-Items : Serialization push failed, see errors above.
At C:\mvp-site\Start-Environment.ps1:104 char:1
+ Push-Items -IdHost "https://id.$($HostDomain)" -CmHost "https://cm.$( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Push-Items



Dotnet Core 3.1 Errors:

It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '3.1.0' (x64) was not found.
  - The following frameworks were found:
      6.0.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=3.1.0&arch=x64&rid=win10-x64
Push-Items : Unable to log into Sitecore, did the Sitecore environment start correctly? See logs above.
At C:\mvp-site\Start-Environment.ps1:104 char:1
+ Push-Items -IdHost "https://id.$($HostDomain)" -CmHost "https://cm.$( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Push-Items

Pushing latest items to Sitecore...
It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '3.1.0' (x64) was not found.
  - The following frameworks were found:
      6.0.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=3.1.0&arch=x64&rid=win10-x64
Push-Items : Serialization push failed, see errors above.
At C:\mvp-site\Start-Environment.ps1:104 char:1
+ Push-Items -IdHost "https://id.$($HostDomain)" -CmHost "https://cm.$( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Push-Items

It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '3.1.0' (x64) was not found.
  - The following frameworks were found:
      6.0.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=3.1.0&arch=x64&rid=win10-x64
Push-Items : Item publish failed, see errors above.
At C:\mvp-site\Start-Environment.ps1:104 char:1
+ Push-Items -IdHost "https://id.$($HostDomain)" -CmHost "https://cm.$( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Push-Items



Comments