Can I check which version of my PS Script is needed?

Sometimes, when I write a script on my computer under Windows 10 (PowerShell 5.0), I use commands, options or aliases that are not available in earlier versions of PowerShell, for example, the -persist new-psdrive not available in PowerShell 2.0, which all of us Win7 machines.

in order to correctly install the #requires -version x operator, I need to know if there are commands in my script that are not available in previous versions of PowerShell. When you wrote code with 1000 lines, it would be a little difficult to find inaccessible commands in a script.

Is there a way to test this programmatically, except to just run the script in different PowerShell environments and see what happens?

+5
source share
3 answers

Do you think that you are working on your Windows 10 machine, but configure your powershell profile to always run powershell -version 2?

You run PowerShell, which will launch version 2 to develop, and if there are errors in the script section you will know when they are created and the commands that will be executed in the 5th version (or any version of your Win10 machine) will fail.

It should be noted that running powershell looks like this:
powershell -version 2 will keep the logic and act like PowerShell version 2, but the help and exit commmands get-help file will still show version 5 (or any other) syntax, however, Powershell version.

Set up a Powershell profile:
http://www.howtogeek.com/50236/customizing-your-powershell-profile/

You can check the current version with $ PSVersionTable

+1
source

This really does not answer your question

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

Earlier, I thought it would be nice if it were better to check at different levels of the version. either print the minimum version for your script. or you specify the target version, and it will tell you what is wrong.

0
source

We decided to make Powershell v.4 basic for all systems. So I wrote a powershell script file to make sure v.4 is installed on all systems. This is configured to run through shutting down the GPO script in the 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 } 

Is network speed a kind of therapy, and I had problems with it, trying to keep working, and the file did not exist. So I just left if the file does not exist for extraction and installation. Since it takes a reboot, shutting down the script has done the most since then. It was quite successful for me, there were several troubles.

0
source

All Articles