We have a multi-line script in powershell and Invoke-Commnand whithin this multi-line script is not working. Here is my job:
deploy:dev1:
stage: deploy
variables:
SERVER: "SERVER_PATH"
script:
- |
$env:PROJECT_LIST -split "," | ForEach-Object ({
$ProjectName = $_
Write-Host "Preparing '$ProjectName' for deployment"
if ($ProjectName -eq "DEBUG_TEST_PROJECT") { # Just for debugging
if ($ProjectName.ToLower().EndsWith('.api')) {
$ProjectType = "services"
} else {
$ProjectType = "intranet"
}
$SourceDir = "$env:MSBUILD_OUTPUT_DIR\$ProjectName\_PublishedWebsites\$ProjectName"
$ServerDestinationDir = "\\$SERVER\c$\inetpub\wwwroot\$ProjectType\$ProjectName"
$AppPoolName = "${ProjectType}_${ProjectName}"
Write-Host "Stopping AppPool '$AppPoolName'"
Write-Host "Invoke-Command -ComputerName '$SERVER' -ScriptBlock { Stop-WebAppPool -Name '$AppPoolName' }"
Invoke-Command -ComputerName '$SERVER' -ScriptBlock { Stop-WebAppPool -Name '$AppPoolName' }
Write-Host "Deploying '$ProjectName'"
Copy-Item $SourceDir\*.* -Destination $ServerDestinationDir
Copy-Item "$env:BUILD_OUTPUT_DIR\webconfigs\$ProjectName\Web.$CI_ENVIRONMENT_NAME.config" -Destination $ServerDestinationDir\Web.config
Write-Host "Starting AppPool '$AppPoolName'"
Write-Host "Invoke-Command -ComputerName '$SERVER' -ScriptBlock { Start-WebAppPool -Name '$AppPoolName' }"
Invoke-Command -ComputerName '$SERVER' -ScriptBlock { Start-WebAppPool -Name '$AppPoolName' }
}
})
when: manual
environment:
name: Dev1
dependencies:
- build
Unfortunatelly, I get the following error:
It seems like git lab is not properly evaluating the command Invoke-Command
. I run the exact same script locally and on the server (where the runner is installed) and it works, but not from the pipeline.
Any idea how to fix this?