How to use Powershell Where-Object in GitLab CI with SSH - Stack Overflow

I'm trying to set up a GitLab pipeline. My runner connects to a windows server with ssh and runs a

I'm trying to set up a GitLab pipeline. My runner connects to a windows server with ssh and runs a powershell script on the server.

I want to keep the script in my code because I need it for multiple servers. So creating the script on the server and executing it with my runner isn't my favourite option.

The ssh connection works and I can run simple powershell commands but I'm having trouble when using Where-Object.

The runner's using an alpine image in docker.

This is my code:

- sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no "$WEBSERVER" <<EOF
      powershell.exe -Command "
      Write-Host 'Backup start';
      \$maxAgeDate = (Get-Date).AddDays(-2);
      \$filesToCopy = Get-ChildItem \$localDirectory | Where-Object { \$_.LastWriteTime -gt \$maxAgeDate };
      Write-Host \"Files to copy - \$(\$filesToCopy.Count)\";
      Write-Host 'Backup end'
      "

I get an error message about the command Where-Object being wrong or misspelled.

This works:

- sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no "$WEBSERVER" <<EOF
      powershell.exe -Command "
      Write-Host 'Backup start';
      \$maxAgeDate = (Get-Date).AddDays(-2);
      \$filesToCopy = Get-ChildItem \$localDirectory;
      Write-Host \"Files to copy - \$(\$filesToCopy.Count)\";
      Write-Host 'Backup end'
      "

The Get-Command Where-Object; also works. Any ideas?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744660304a4586419.html

相关推荐

  • How to use Powershell Where-Object in GitLab CI with SSH - Stack Overflow

    I'm trying to set up a GitLab pipeline. My runner connects to a windows server with ssh and runs a

    11天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信