0

Is there a way to make the azure pipeline task PowerShellonTargetMachines hit the powershell core endpoint instead of the windows powershell endpoint?

Changing the machine profile doesn't seem to be an option for us and I tried running Enable-PSRemoting on powershell 7 on a target machine and then trying to run an inline script targetting that machine via the PowerShellonTargetMachines task that include this code to start a powershell 7.4.2 instance but it still says 7.4.2 doesn't exist even though it is installed and clearly from the error message it's still hitting windows powershell instead of powershell core. Why isn't there a newer version of PowerShellonTargetMachines that directly supports powershell core.

if ($PSVersionTable.PSVersion -lt [Version]"7.4.2") {
  Write-Output "Restarting powershell to get correct version"
  powershell -Version 7.4.2 -File $MyInvocation.MyCommand.Definition
  exit
}
2024-11-06T04:51:07.7257943Z 
2024-11-06T04:51:07.7693744Z Cannot start Windows Powershell version 7.4.2 because it is not installed```

2 Answers 2

0

I am afraid that there is no built-in option in PowerShellOnTargetMachines Task can force the task to use PowerShell core.

Is there a way to make the azure pipeline task PowerShellonTargetMachines hit the powershell core endpoint instead of the windows powershell endpoint?

Since you have installed PowerShell core 7.4.2 on your machine, you can use the pwsh command to run the script. In this case, it will directly use the PowerShell core instead of windows powershell.

For example:

pwsh
pwsh -File $MyInvocation.MyCommand.Definition

Pipeline task sample:

- task: PowerShellOnTargetMachines@3
  inputs:
    Machines: 'xx'
    UserName: 'xx'
    UserPassword: 'xx'
    InlineScript: |
      Write-Output "Restarting powershell to get correct version"
      pwsh
      pwsh -File $MyInvocation.MyCommand.Definition

If you see the error below:

pwsh : The term 'pwsh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

This means that you haven't set the Windows Environment variable for the PowerShell core. In this case, you can find the pwsh.exe file path(e.g. C:\Program Files\PowerShell\7\pwsh.exe) and run the pwsh.exe.

For example:

&"C:\Program Files\PowerShell\7\pwsh.exe"
&"C:\Program Files\PowerShell\7\pwsh.exe" -File $MyInvocation.MyCommand.Definition
Sign up to request clarification or add additional context in comments.

5 Comments

Kevin Lu, Thank you. I believe your solution works but I need to test it a bit more before I mark it as the solution. Although I would still argue this is a work around. A newer version of PowerShellOnTargetMachines would be better. So I don't have to use up valuable script space. I've already run into limits for the size an inline script can be before.
@mpmarven Yes. This is a workaround. You can use this method to use powershell core for the time being. On the other hand, you idea is really great. If the task itself can support defining powershell core, it will be easier to use powershell core in PowerShellOnTargetMachines task. I suggest that you can submit a suggestion ticket in this site: developercommunity.visualstudio.com/AzureDevOps/suggest Hope it can be achieved in the future.
Thanks again Kevin, after futher testing this does work so I've marked it as the solution. I will submit a suggestion to create a newer version of the PowerShellOnTargetmachines task. It's frustrating that this doesn't already exist though powershell core has been around since 2006 and I think every other powershell task supports it already.
@mpmarven Thanks for your sharing. I will monitor the suggestion ticket. Hope it can be achieved.
-1

To force an Azure Pipeline task to use PowerShell Core (specifically PowerShell 7+), you can specify the pwsh command in your pipeline YAML. This ensures that the task runs in the context of PowerShell Core instead of Windows PowerShell.

Here’s how you can do it:

Example YAML Pipeline yaml

Copier
jobs:
- job: PowerShellCoreJob
  pool:
    vmImage: 'windows-latest'
  steps:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        # Your PowerShell Core script goes here
        Write-Host "Running in PowerShell Core"
      pwsh: true  # This explicitly tells the task to use PowerShell Core

Key Points

  • Task Type: Use the PowerShell@2 task.
  • owsh: true: This input parameter tells the task to use PowerShell Core. - Inline or File: You can provide an inline script or reference a script file.

Additional Configuration If you're using a script file instead of inline code, make sure to provide the path to the script and still set pwsh: true:

yaml

Copier
steps:
- task: PowerShell@2
  inputs:
    targetType: 'filePath'
    filePath: 'path/to/your/script.ps1'
    pwsh: true

By specifying pwsh: true, your scripts will run using PowerShell Core, allowing you to leverage the features and enhancements of the newer version.

2 Comments

The option: pwsh: true can only available in PowerShell task instead of PowerShellonTargetMachines task.
Yes I'm running PowerShellonTargetMachines V3 so my issue is exactly as Kevin Lu mentioned. There is no way to access the pwsh attribute as far as I can tell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.