Appending a script after powershell is launched?

Welcome Forums General PowerShell Q&A Appending a script after powershell is launched?

This topic contains 8 replies, has 4 voices, and was last updated by

 
Participant
3 weeks, 4 days ago.

  • Author
    Posts
  • #114562

    Participant
    Points: 3
    Rank: Member

    Good morning

    I'm trying to run a script using powershell version 2, however it's not working.

    So i'm using – powershell -Version 2 (script)  = failed

    However if I use powershell -Version 2 first to launch powershell version 2 then paste the script in to the window it works perfectly.

    So my question is, is there a way to automate this, open powershell -Version 2 first then append the script inside this version 2 window?

    Many Thanks

  • #114568

    Moderator
    Points: 81
    Team MemberHelping Hand
    Rank: Member

    Hm. You could try running:

    
    powershell.exe -Version 2 -File (ScriptPath)
    
    

    If that doesn't work, can you provide the error message?

    • #114573

      Participant
      Points: 3
      Rank: Member

      Thanks for the reply James.

      I tried this method too, the window just opens and closes. There is no error message unfortunately. I just know the script hasn't worked. But again, if I manually paste it, then it works perfectly.

  • #114579

    Participant
    Points: 544
    Helping Hand
    Rank: Major Contributor

    @Roger , Is it possible to show the content in your code.

    Are you able to open a version 2.0 shell and execute from there instead of using -file.

    • #114583

      Participant
      Points: 3
      Rank: Member

      Colleagues System Update

      Sub TestSub()
      set objshell = Createobject("wscript.shell")
      objshell.Run("powershell -Version 2 (rest of code goes here)")

      End Sub

  • #114618

    Participant
    Points: 544
    Helping Hand
    Rank: Major Contributor

    Why do you call a PowerShell script inside a VB script ?
    You should just use PowerShell only. Can you explain lil about your use case here ?
    And please checkout the syntax using PowerShell.exe /? in PowerShell or cmd

    • #115150

      Participant
      Points: 3
      Rank: Member

      Many thanks for your replies.

      I understand this seems a bit strange, so i'll try and explain a little further.

      I'm using a hta file to load an onclick button, once clicked it will run the powershell script. I know this is a long winded way of doing things but for the purpose of this experiment this is how it has to be.

      I'm trying to prove that a script that should not be run in Powershell V2 can be, by simply downgrading the version.

      I can successfully downgrade the version however when the script is appended to Powershell -Version 2 (script) it still runs in V5. UNLESS I manually load pwowershell, downgrade the version first and paste in the rest of the script in to that powershell prompt window. This then works as it should and it's ran from with-inside V2.

      So my question is, why doesn't this work in the first instance and how can this be overcome? Is there a way to read in the rest of the script once Powershell is downgraded automatically?

      Hope this clears things up.

      I also know V2 shouldn't be used, all the security issues associated with it and such, however as mentioned this is purely experimental and I would like to understand what's going on.

      Thanks again.

    • #115179

      Participant
      Points: 544
      Helping Hand
      Rank: Major Contributor

      OK,

      Are you using the -file parameter ? I cannot reproduce your issue using below code.

      Sub TestSub()
      set objshell = Createobject("wscript.shell")
      objshell.Run("PowerShell.exe -v 2.0 -f c:\temp\test1.ps1")
      End Sub
      TestSub()
      

      if you are not using -file, please use it, other wise using -command option will be tedious of big scripts.

  • #115092

    Participant
    Points: 206
    Helping Hand
    Rank: Participant

    Jus curious.
    Why are you using PS2?

    MS has already publicly stated it's announced depreciation …

    Jeffrey Snover, a Microsoft Technical Fellow and the "father" of PowerShell, had few tears to shed over the impending demise of Windows PowerShell 2.0. In a Twitter post, he advised moving to new versions of PowerShell:

    "PowerShell V2 is being deprecated (and that is a very good thing). If you still use V2, upgrade now to avoid drama."

    The rationale is that Windows PowerShell 2.0 currently lacks security protections that have been built into later versions, particularly PowerShell 5.0.

    https://mcpmag.com/articles/2017/07/24/powershell-2-support-ending.aspx

    Windows PowerShell 2.0 Deprecation

    ...as well as the threat to downgrade attacks for those with higher PS versions.

    I cannot imagine any reason to still be on PSv2, unless you are being blocked from upgrading for whatever reason. This is like still being on WinXP. WE all know there are folks out there on WinXP, because they have software that will only ruin with it. That is not a thing regarding PS. Also, you loose any ability to audit / monitor / control it's use by internals and externals.

    I can only assume, you are calling PS from VBS because you want to be able to double click to start the script. This is still not prudent either. PS is designed to prevent this double-click (hence the reason by default it's associated with notepad or VSCode if it is installed) to run thing due to risks associated with double-click to ruin things.

    If you are unconcerned with this type of risks (but you should be), then you can just associate .PS1 files with powershell.exe (using Windows Explorer or the like), and double clicking will work. Though I would not advise it from a operational perspective.

You must be logged in to reply to this topic.