Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PowerShell Core 7.x on Windows and Linux #1026

Open
djsvi opened this issue Mar 27, 2022 · 10 comments
Open

Add support for PowerShell Core 7.x on Windows and Linux #1026

djsvi opened this issue Mar 27, 2022 · 10 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@djsvi
Copy link

djsvi commented Mar 27, 2022

I'd like the option within TRMM to natively run scripts using PowerShell Core 7.x on Windows and Linux endpoints.

There's some info about this here (for Windows) - https://docs.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.2

I imagine a lot of TRMM users are far more comfortable with PowerShell than bash and suspect this might help grow the number of useful cross-platform scripts within the TRMM community.

@BMTTeam
Copy link

BMTTeam commented Mar 27, 2022

"I imagine" - you imagine it wrong, but having move options is a good thing in the SysAdmin arsenal

Cheers,
-Tiho

@dinger1986
Copy link
Collaborator

Totally agree with BMT.

Are you sure this isn't implemented? If a powershell script is set to all it will run on linux (or at least attempt to). Have you installed powershell on a linux server and tested?

@djsvi
Copy link
Author

djsvi commented Mar 28, 2022

I haven't tested but if PowerShell Core support is already implemented for Linux that's great. That would just leave Windows which is otherwise stuck on 5.x.

@dinger1986
Copy link
Collaborator

Shall leave this open for you to test it and report back :)

@silversword411
Copy link
Collaborator

Since TRMM is one huge scripting engine, this is probably something you can script on your own if you want to have powershell core on all the machines you want.

If you'd like to get any scripts you build for that purpose into the script library let me know and we'll get it done.

I'll close in a month if no movement.

@silversword411 silversword411 self-assigned this Mar 28, 2022
@silversword411 silversword411 added the help wanted Extra attention is needed label Mar 28, 2022
@wh1te909
Copy link
Member

something that might not be obvious...to use powershell on linux you'll actually need to choose "Shell" as the script type. then you will need to add a shebang as the first line of the script with the path to the powershell binary so like #!/usr/bin/env/powershell or wherever it is installed on your system

@djsvi
Copy link
Author

djsvi commented Mar 29, 2022

to use powershell on linux you'll actually need to choose "Shell" as the script type

(While appreciating Linux support is still beta) that's a bit kludgy :) Why not have TRMM natively run the powershell script using /opt/microsoft/powershell/pwsh ?

Then powershell scripts can all sit nicely side by side within TRMM.

@silversword411
Copy link
Collaborator

silversword411 commented Mar 29, 2022

Why not have TRMM natively run the powershell script using /opt/microsoft/powershell/pwsh

Because that's non-standard, now TRMM has to install and manage powershell core on linux, and detect OS to determine if it should be installed, and install PS Code on windows, and upgrade or repair, and regression test and and....

I'm sure most 'nix folks will not want powershell crufting up their 'nix install ;)

There's a workaround that you can fully implement for now if you want pshw. I'll leave it open, but I don't see much changing unless someone wants to pickup the ball and run with it.

@silversword411 silversword411 removed their assignment Mar 29, 2022
@silversword411 silversword411 added the enhancement New feature or request label Mar 29, 2022
@P6g9YHK6
Copy link

having this on windows only would be nice.

no need to install it just puke an error out if the users try to use it and pwsh is not recognised.

@P6g9YHK6
Copy link

Ok ended up doing a script to sort this out on windows:

It is set as a snippet "CallPowerShell7" and if {{CallPowerShell7}} is called the start of script it will run the whole thing as PowerShell 7

# Check for required PowerShell version (7+)
if (!($PSVersionTable.PSVersion.Major -ge 7)) {
  try {
    # Check if Chocolatey is installed
    if (!(Get-Command choco -ErrorAction SilentlyContinue)) {
        Write-Output 'Chocolatey is not installed. Installing Chocolatey...'
        # Install Chocolatey
        Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
        # Refresh PATH
        $env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'User')
        if (!(Get-Command choco -ErrorAction SilentlyContinue)) {
            Write-Output 'Chocolatey installation failed.'
            exit 1
        }
    }
    # Check if PowerShell 7 is installed
    if (!(Get-Command pwsh -ErrorAction SilentlyContinue)) {
        Write-Output 'PowerShell 7 is not installed. Installing PowerShell 7...'
        # Install PowerShell 7 using Chocolatey
        choco install powershell-core --install-arguments='"DISABLE_TELEMETRY"'-'"ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1"'-'"ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1"'-'"REGISTER_MANIFEST=1"' -y
        if (!(Get-Command pwsh -ErrorAction SilentlyContinue)) {
            Write-Output 'PowerShell 7 installation failed.'
            exit 1
        }
    }
    # Refresh PATH
    $env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'User')
    # Restart script in PowerShell 7
    pwsh -File "`"$PSCommandPath`"" @PSBoundParameters
  }
  catch {
    Write-Output 'Error occurred while installing PowerShell 7.'
    throw $Error
    exit 1
  }
  finally { exit $LASTEXITCODE }
}

#Set the correct rendering for pwsh
$PSStyle.OutputRendering = "plaintext"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants