WinBoss
WinBoss

Reputation: 923

PowerShell 7 Remoting from Azure DevOps Pipeline

We were using Azure DevOps Pipelines and PowerShell Remoting to execute PS1 scripts:

trigger: none

jobs:
  - job: PSRemoting
    timeoutInMinutes: 5760
    pool:
      name: 'DevOps-Agent2-VM'
    steps:
    - checkout: none

    - task: PowerShellOnTargetMachines@3
      displayName: 'PSRemoting'
      inputs:
        Machines: '$(VM-PublicIP)'
        UserName: '$(VM-UserName)'
        UserPassword: '$(VM-Password)'
        InlineScript: |
          . C:\Scripts\Test-Parallel.ps1

We have added -parallel feature that requires PowerShell 7 and our scripts are failing now:

2022-11-02T13:37:18.3246154Z ForEach-Object : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ & 'C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe' -NoLogo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (ForEach-Object ...med parameters.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 

At C:\Scripts\Test-Parallel.ps1:3 char:10

+ $items | ForEach-Object -Parallel {

+          ~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : MetadataError: (:) [ForEach-Object], ParentContainsErrorRecordExcep 

   tion

    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ForEachObjectCo 

   mmand

 

##[error]Non-Zero exit code: '1' for ComputerName: 'VM1'
##[error]Atleast one remote job failed. Consult logs for more details. ErrorCodes(s): 'RemoteDeployer_NonZeroExitCode'
##[section]Finishing: PSRemoting

Here is Test-Parallel.ps1:

$items = 1..100
 
$items | ForEach-Object -Parallel {
  Write-Host "$(Get-Date) | Sleeping for 1 second..."
  Start-Sleep 1
} -ThrottleLimit 10 

How can we force PowerShell to run version 7 with our Azure DevOps Pipeline task?

Upvotes: 0

Views: 977

Answers (1)

Crunchers3
Crunchers3

Reputation: 180

Try this in your Test-Parallel.ps1 script:

workflow Test-PipelineDeploy {
  $items = 1..100

  foreach -parallel ($item in $items) {
    Write-Host "$(Get-Date) | Sleeping for 1 second..."
    Start-Sleep 1
  } -ThrottleLimit 10 
}

Then call Test-PipelineDeploy

Upvotes: 1

Related Questions