0

I am trying to run a PowerShell script from within an Azure, Data Factory, Batch Service, Custom Activity. The closest I've gotten to this working is the following:

powershell powershell -command  '$env:AZ_BATCH_TASK_DIR\wd\processInAzure.ps1'

When I run this I get the following error message

At line:1 char:23
+ $env:AZ_BATCH_TASK_DIR\wd\processInAzure.ps1
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '\wd\processInAzure.ps1' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

I've been able to get a directory listing of $env:AZ_BATCH_TASK_DIR\wd and see that processInAzure.ps1 exists at this location. I've been able to Write-Host "Hello from Azure" so I can see powershell is working. What I'm not getting is how to reference the ps1 script file using an environment variable. Would anyone know the syntax sugar to get this working?

With Architect Jamie's input (including his deleted edit!!! Put edit back it directly led to the solution) I was able to put a few things together to get this to work. The double powershell at the beginning of the command line is not a typo BTW. This is what ended up working:

powershell powershell -command ("$env:AZ_BATCH_TASK_DIR" + '\wd\processInAzure.ps1')

What didn't work is the following:

powershell powershell -command ("$env:AZ_BATCH_TASK_DIR" + "\wd\processInAzure.ps1")
powershell powershell -command ("$env:AZ_BATCH_TASK_DIR\wd\processInAzure.ps1")
powershell -command ("$env:AZ_BATCH_TASK_DIR" + '\wd\processInAzure.ps1')

1 Answer 1

1

Try this:

powershell -command "$($env:AZ_BATCH_TASK_DIR)\ws\processInAzure.ps1"

It's not possible to access object member properties or methods inside single quote qualified strings. Using the double quote in PowerShell allows you to expand variables at runtime.

Edit:

Though the above is true, in this instance the reason for the error is that PowerShell is treating the path being tacked on as part of the environment variable identifier. Using $() variable expansion as above will work, and you should also be able to use ('$env:AZ_BATCH_TASK_DIR' + '\ws\processInAzure.ps1')

Sign up to request clarification or add additional context in comments.

2 Comments

Jamie, this by itself might work in a normal command window, but within the Azure Data Factory Batch Service Custom Activity it did not. If you put your edit with the concatenation of the strings back I'll mark it as the answer.
Rolled it back. Glad this helped somewhat :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.