On occasion, one will produce the script that will not work in
_ISE or on some particular version of Powershell. So, before
allowing script to run, I always do several checks depending on
the task at hand. Here's the code:
#region Check
if ($host.Name -ne 'ConsoleHost')
{
#Running in ISE
$host.UI.WriteErrorLine("`t Script can not be run in _ISE. Exiting.")
Exit 1
}
if (($PSVersionTable).PSVersion.Major -lt 3) {
$host.UI.WriteErrorLine("`t Script can not be run in PS 2. Exiting.")
Exit 2
}
$clrV =
((Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?!S)\p{L}'} |
Select Version | Sort Version -Desc | Select -First 1).Version).Split('.')[0]
if ( $clrV -lt 4) {
$host.UI.WriteErrorLine("`t Script can not be …[Read more]