admin管理员组

文章数量:1391998

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?

本文标签: How to use Powershell WhereObject in GitLab CI with SSHStack Overflow