SimonS
SimonS

Reputation: 1973

Can I check which Version my PS Script needs?

Sometimes when I'm writing a script on my Windows 10 Machine (PowerShell 5.0) I use commands, parameters or aliases which are not available on earlier versions of PowerShell, e.g the -persist parameter of new-psdrive is not available on PowerShell 2.0 which all of our Win7 machines use.

to set my #requires -version x statement correctly, I need to know if there are commands in my script which aren't available to earlier PowerShell Versions. When you wrote a code with 1000 lines it could be a little difficult to find unavailable commands in your script.

Is there a way to check this programmatically, other than just run the script in different PowerShell environments and see what's happening?

Upvotes: 5

Views: 957

Answers (3)

arcadesdude
arcadesdude

Reputation: 126

Have you considered developing on your Windows 10 machine but set your powershell profile to always run a powershell -version 2?

You'll launch powershell which will launch version 2 to develop in and if there are errors in the script you'll know when they're created and commands that would run in version 5 (or whatever version your Win10 machine has) would fail.

It should be noted that launching powershell like this:
powershell -version 2 will keep the logic the same and act like the version 2 powershell but the help file and output from commmands Get-Help will still show the version 5(or whatever) syntax that is true powershell version.

Setting your Powershell Profile:
http://www.howtogeek.com/50236/customizing-your-powershell-profile/

You can check the running version with $PSVersionTable

Upvotes: 1

Tram
Tram

Reputation: 55

We decided to make Powershell v.4 baseline for all systems. So I wrote a powershell script to ensure v.4 is install on all systems. This is set to run via GPO shutdown script under machine configuration.

IF ($PSVersionTable.PSVersion.Major -like "4*"){$StringToWrite | Out-File -FilePath $Logpath\PwrShl\Powershell_UpToDate_$hostname.log -Append; IF (Test-Path -Path $Logpath\PwrShl\Powershell_OutofDate_$hostname.log){Remove-Item $Logpath\PwrShl\Powershell_OutofDate_$hostname.log -Force}; exit}
IF (($PSVersionTable.PSVersion.Major -like "2*") -or ($PSVersionTable.PSVersion.Major -like "3*")){$StringToWrite | Out-File -FilePath $Logpath\PwrShl\Powershell_OutofDate_$hostname.log -Append}

if ($env:PROCESSOR_ARCHITECTURE -eq "x86"){
if (!(Test-Path C:\SchTsk\Temp\Windows6.1-KB2819745-x86-MultiPkg.msu)){Copy-Item "\\ad.dcpds.cpms.osd.mil\SYSVOL\ad.dcpds.cpms.osd.mil\Policies\{31B2F340-016D-11D2-945F-00C04FB984F9}\MACHINE\Scripts\Startup\Windows6.1-KB2819745-x86-MultiPkg.msu" -Destination C:\SchTsk\Temp\Windows6.1-KB2819745-x86-MultiPkg.msu -Force}
}
if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64"){
if (!(Test-Path C:\SchTsk\Temp\Windows6.1-KB2819745-x64-MultiPkg.msu)){Copy-Item "\\ad.dcpds.cpms.osd.mil\SYSVOL\ad.dcpds.cpms.osd.mil\Policies\{31B2F340-016D-11D2-945F-00C04FB984F9}\MACHINE\Scripts\Startup\Windows6.1-KB2819745-x64-MultiPkg.msu" -Destination C:\SchTsk\Temp\Windows6.1-KB2819745-x64-MultiPkg.msu -Force}
}

IF ($env:PROCESSOR_ARCHITECTURE -eq "x86"){
    Set-Location C:\SchTsk\Temp
    if (!(Test-Path C:\SchTsk\Temp\Windows6.1-KB2819745-x86-MultiPkg.msu)){exit}
    expand -F:* .\Windows6.1-KB2819745-x86-MultiPkg.msu C:\SchTsk\Temp
    Start-Process dism.exe -ArgumentList '/online /add-package /PackagePath:C:\SchTsk\Temp\Windows6.1-KB2872035-x86.cab /NoRestart' -Wait
    Start-Process dism.exe -ArgumentList '/online /add-package /PackagePath:C:\SchTsk\Temp\Windows6.1-KB2872047-x86.cab /NoRestart' -Wait
    Start-Process dism.exe -ArgumentList '/online /add-package /PackagePath:C:\SchTsk\Temp\Windows6.1-KB2819745-x86.cab /NoRestart' -Wait
    } 
IF ($env:PROCESSOR_ARCHITECTURE -eq "amd64"){
    Set-Location C:\SchTsk\Temp
    if (!(Test-Path C:\SchTsk\Temp\Windows6.1-KB2819745-x64-MultiPkg.msu)){exit}
    expand -F:* .\Windows6.1-KB2819745-x64-MultiPkg.msu C:\SchTsk\Temp
    Start-Process dism.exe -ArgumentList '/online /add-package /PackagePath:C:\SchTsk\Temp\Windows6.1-KB2809215-x64.cab /NoRestart' -Wait
    Start-Process dism.exe -ArgumentList '/online /add-package /PackagePath:C:\SchTsk\Temp\Windows6.1-KB2872035-x64.cab /NoRestart' -Wait
    Start-Process dism.exe -ArgumentList '/online /add-package /PackagePath:C:\SchTsk\Temp\Windows6.1-KB2872047-x64.cab /NoRestart' -Wait
    Start-Process dism.exe -ArgumentList '/online /add-package /PackagePath:C:\SchTsk\Temp\Windows6.1-KB2819745-x64.cab /NoRestart' -Wait
    }

Are network speed is kind of crapy and I was having problems with it trying to continue to run and the file not be existant. So I simply had it exit if the file isn't there to extract and install. Since it takes a restart, shutdown script made the most since. This was pretty successful for me, had a few trouble childs.

Upvotes: 0

ShoeLace
ShoeLace

Reputation: 3576

This doesnt really answer your question

but i use the "Script Analyser" that comes with https://gallery.technet.microsoft.com/scriptcenter/Getting-started-with-57e15ada

I had previously thought that it would be good if it was enhance to check at different version levels. either output the minimum version for your script. or you specify a target version and it would tell you what was wrong.

Upvotes: 0

Related Questions