4

I would like to run my powershell script in v2 mode. Is it possible to do this without having a wrapper script?

So for example, I can do this now if I can two files.

MainContent.ps1

write-output 'run some code'
Read-Host -Prompt "Scripts Completed : Press any key to exit" 


Wrapper.ps1

powershell -version 2 -file 'MainContent.ps1'

This will work but I'm hoping I don't need to have this second wrapper file because I'm creating a whole bunch of these ps1 scripts, and having wrapper files will double the amount of scripts I'll need.

I'm hoping I can do something like this.

MainContent.ps1

Set-Powershell -version 2
write-output 'run some code'
Read-Host -Prompt "Scripts Completed : Press any key to exit" 

Later on, I would also like each script to ask for a set of credentials as well without using a wrapper file.

Is this currently possible?

To be clear, I'm using version 2 of powershell

Diskdrive
  • 18,107
  • 27
  • 101
  • 167
  • Pardon my curiosity, but why do you need this? Are you just trying to write portable scripts, or do you have a specific compatibility issue? – Ryan Bemrose Jul 03 '15 at 00:58
  • @RyanBemrose = compability issue. Users with powershell 3.0 installed get this error "Microsoft SharePoint is not supported with version 4.0.30319.17929 of the Microsoft .Net Runtime." – Diskdrive Jul 03 '15 at 01:03
  • It's not just the PowerShell version then. It's the underlying .NET runtime you need loaded. Starting a new process is the only way to change it. – Mike Zboray Jul 03 '15 at 01:06
  • @mikez - yeah I'm not sure why that message is showing but this KB says to just change the powershell version and it does work when I do that. https://support.microsoft.com/en-us/kb/2796733 – Diskdrive Jul 03 '15 at 01:08
  • PowerShell 2.0 used .NET 3.5, so it's a way force down the CLR version. – Mike Zboray Jul 03 '15 at 01:14

1 Answers1

6

If your only goal is to avoid creating separate wrapper scripts, you can always have the script re-launch itself. The following script will always re-launch itself once with PS version 2.0.

param([switch]$_restart)
if (-not $_restart) {
  powershell -Version 2 -File $MyInvocation.MyCommand.Definition -_restart
  exit
}

'run some code'
Read-Host -Prompt "Scripts Completed : Press any key to exit"

Or you can make it conditional. This script re-launches itself with version 2 only if the version is greater than 2.0.

if ($PSVersionTable.PSVersion -gt [Version]"2.0") {
  powershell -Version 2 -File $MyInvocation.MyCommand.Definition
  exit
}

'run some code'
Read-Host -Prompt "Scripts Completed : Press any key to exit"
Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
  • ah ok, that's not bad. Is this a common pattern? Is there a way to make it generic so that instead of checking the version, it checks to see if the script has run the first time? Then I can actually put other stuff in there like calling Set-Credential to get the user to run the script in a particular account – Diskdrive Jul 03 '15 at 01:16
  • 1
    Modified to show both cases - conditional restart and always restart. – Ryan Bemrose Jul 03 '15 at 01:27
  • thanks ryan. That's exactly the line of logic that I was implementing, but thanks for completing it for me! – Diskdrive Jul 03 '15 at 01:28