Lets Share

My personal place to share knowledge about Sitecore, Powershell, Amazon Web Services and .NET

How to prevent a script from running without the required elements?

posted by Robert Senktas   | 18-01-2018

how-to-protect-powershell-scripts When we want to install Sitecore with Sitecore Install Framework there, a few requirements must be met.
In Powershell, there is an easy way to achieve this goal.

The #Requires statement prevents a script from running unless the Windows PowerShell version, modules,
and rights prerequisites are met. If the requirements are not met, Windows PowerShell does not run the script.

More details about available options you can find in documentation.



In case of Sitecore Install Framework, we shall run the install script:

  • with admin rights
  • the PowerShell 5.1 is required
  • the module Sitecore Install Framework is required
  • we shall use the x64 version of Powershell ISE, not x86
  • we also require our Sitecore Install Extensions module

Below is a code snippet to check all requirements:

#requires -RunAsAdministrator 
#requires -Version 5.1
#requires -module SitecoreInstallFramework
#requires -module SitecoreInstallExtensions

The last check that you should consider if you don't want to have trouble is to verify PowerShell ISE version.
The expected is the x64 version of Powershell ISE, not x86

If(![Environment]::Is64BitProcess) 
{
    Write-Warning "Please run 64-bit PowerShell"
    return
}


Sitecore Sitecore Install Framework Defensive Programming Powershell
This is a personal blog. All opinions here are my own opinions and do not represent my employer’s view in anyway.

Sitecore Automation Module