amidaware / tacticalrmm

A remote monitoring & management tool, built with Django, Vue and Go.
https://docs.tacticalrmm.com
Other
3.05k stars 434 forks source link

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

Open djsvi opened 2 years ago

djsvi commented 2 years ago

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 commented 2 years ago

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

Cheers, -Tiho

dinger1986 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

silversword411 commented 2 years ago

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.

wh1te909 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.

P6g9YHK6 commented 5 months ago

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 commented 5 months ago

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"