Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I unzip in PowerShell on a TeamCity Agent?

I have a script that unzips an MVC package, minifies all the CSS and js files, and rezips it. It works perfectly on my machine and on the CI server as an admin or the TeamCity account. When TeamCity pulls it down and runs it, the script gives all the output expected, doesn't report an error, but it will never unzip the file.

write-host "creating shell object"
$shell_app=new-object -com shell.application
write-host "creating zip file"
$zip_file = $shell_app.namespace("C:\temp\PokerLeagueWebSite.zip")
write-host "setting folder to unzip to"
$destination = $shell_app.namespace("C:\temp\zy98")
write-host "about to copy files from zip"
$destination.Copyhere($zip_file.items(), 0x4)
write-host "finished"

Here is the output from TeamCity:

[23:01:03]: Step 5/5: test ps1 file (Powershell)
[23:01:03]: [Step 5/5] Starting: C:\Windows\system32\cmd.exe /c
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -File
C:\TeamCity\buildAgent\work\71e78d4c543e0594\Tools\test.ps1 && exit /b %ERRORLEVEL%
[23:01:03]: [Step 5/5] in directory: C:\TeamCity\buildAgent\work\71e78d4c543e0594
[23:01:03]: [Step 5/5] creating shell object
[23:01:03]: [Step 5/5] creating zip file
[23:01:03]: [Step 5/5] setting folder to unzip to
[23:01:03]: [Step 5/5] about to copy files from zip
[23:01:03]: [Step 5/5] finished
[23:01:03]: [Step 5/5] Process exited with code 0
[23:01:03]: Publishing internal artifacts
[23:01:03]: [Publishing internal artifacts] Sending build.finish.properties.gz file
[23:01:03]: Build finished

If I run the exact command on the server on the TeamCity account it works correctly:

C:\Windows\system32\cmd.exe /c C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -File C:\TeamCity\buildAgent\work\71e78d4c543e0594\Tools\test.ps1 && exit /b %ERRORLEVEL%

I can only think it is either something TeamCity is doing or something related to the fact it is running as a service, but I am stumped and can't figure this one out.

like image 211
Jon Avatar asked Dec 30 '25 11:12

Jon


2 Answers

I used the answer from "user1545905", but I needed to manually restart the TeamCity agent service after the server rebooted. You can give the TeamCity agent service user Full Control on the directory.

like image 56
Bruce Liu Avatar answered Jan 01 '26 00:01

Bruce Liu


I ran into this same issue yesterday. It has something to do with permissions, even though no error was logged. I have a Windows Server machine for my build agent and had it setup to run as a Windows Service. I changed the Log In setting for the service to be a user instead of the SYSTEM account and everything seemed fine until I got the same problem you have. I never found a way to fix it when running the agent as a service so I changed it to run the agent from their /bin/agent.bat file. I could pretty much achieve the same setup by having it run as a scheduled task and run on startup. But, ultimately, changing to not run as a windows service fixed the problem for me.

like image 40
Kevin Jett Avatar answered Jan 01 '26 00:01

Kevin Jett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!