Powershell Script Places Files In Unexpected Location
search cancel

Powershell Script Places Files In Unexpected Location

book

Article ID: 252168

calendar_today

Updated On:

Products

CA Workload Automation DE

Issue/Introduction

I am trying to run a powershell script. When running it directly from the server, it works as expected. When running it via a Windows job, it runs but it results in a bunch of files in the Workload Automation Agent's install directory. 

Environment

Release : 12.0

Cause

The powershell script used in this instance was:

  • Using Get-ChildItem to get a list of files in one location. 
  • Using Start-Process to execute a program against these files and produce an output file. 

The output file generated gets created in whatever directory the script is run from. This is confirmed by opening a command prompt, cd'ing to the Workload Automation Agent's installation directory, and running: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe /c C:\path\to\powershell_script.ps1

 

Resolution

It depends on the version of powershell running on the agent machine. Powershell version 5 does not support stringing along commands together using &&. Powershell version 7 does. So:

    • If you have powershell version 7 then you should be able to use:
      • C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe /c "c: && cd "c:\path\where\you\want\the files" && C:\path\to\powershell_script.ps1
    • If you have a version of powershell earlier than 7 then you should be able to use:
      • C:\Windows\System32\cmd.exe /c "c: && cd "c:\path\where\you\want\the files" && C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\path\to\powershell_script.ps1"
      • C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe /c "c:; cd "c:\path\where\you\want\the files"; C:\path\to\powershell_script.ps1"
        However, this will execute the powershell script even if the cd command was not successful.
    • Alternatively, you could cd to the appropriate folder in the powershell script and code with the appropriate precautions (like checking the return codes of commands - like cd <folder>).