Calling a PowerShell script from Azure batch custom activity - azure

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')

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')

Related

How to concatenate two variables from azure variable group and assign them to one with single quotes

I'm having 2 Variables inside a variable group and their values are as shown below:
cr 2200
tr cd1200
I would like to assign these two variables to a third variable cd as shown below:
cd '2200;cd1200;abc.txt'
I'm trying to use below script but it is showing too many arguments.
Can someone please help in this.
I'm using below code:
cd = \'"$cr";"$tr";abc.txt\'
echo "$cd"
I need output as:
cd = '2200;cd1200;abc.txt'
Using Azure macro syntax ($(<varName>)):
$cd = '$(cr);$(tr);abc.txt'
Note:
PowerShell always needs the $ sigil when accessing a variable - even when assigning to it (unlike in POSIX-compatible shells such as Bash).
Azure's macro syntax - which textually expands references to Azure variables up front, before PowerShell sees the code, is not be confused with PowerShell's subexpression operator ($(...))

Excel Crashes when trying to run powershell command

I am having an issue trying to run a complex HTTP Request through a powershell command from a VBA function.
VBA Code Below: The powershell script runs fine from inside powershell
Public Function PS_GetOutput(id As String) As String
'Setup the powershell command properly
psCommand = "powershell -command " & "C:\Users\promney\temp1\curlCommand.ps1 " & id
'Execute the command
CreateObject("WScript.Shell").Run psCommand, 3, True
'Get an instance of the clipboard to capture the save value
End Function
The PS1 file should grab the html and put a string on the clipboard. Instead, the PS window opens as a blue field, then it and excel crash completely and immediately. What am I doing wrong here?
Note: I would include the PS script but that is a lot of possibly sensitive info to scrub and I'm pretty sure the issue is in the VBA, since it works fine unless called from excel.

can you use IF statement in inline Azure Powershell script in Azure Devops?

I have a task in Azure Devops which builds a VM
Task 1 builds a VM
Task 2 adds datadisks if required
so in task 2 I have an inline powershell script but when i run it it gives an error
" The term 'n' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Not sure why it is giving the error?
"
###Azure Devops pipeline variable is $(datadisk1required) this has the value 'n'
$datadisk1required = $(datadisk1required)
if ($datadisk1required -eq "n")
{
write-host "datadisk1 not required"
write-host $datadisk1required
}
The substitution of AzDO variables in the script happens before PowerShell runs, and that means that if the inline script definition is:
$powerShellVariable = $(azVariable)
and the value of the azVariable variable in the task is "string", the resulting script passed to PowerShell will be:
$powerShellVariable = string
PowerShell will interpret the bare word string as a command name, hence the error.
Put quotation marks in place around the AzDO variable macro and it'll work:
$datadisk1required = '$(datadisk1required)'

Azure DevOps Yaml: Gaining secret variable out of Azure KeyVault Task from Variable

I'm trying to obtain a secret out of my KeyVault.
The variable name is secretVar.
Obtaining the secret like this: $(secretVar) works fine however I would like to retrieve it from a variable like this:
I keep getting command not found and I've no idea why this shouldn't be working.
So the name of the secret I want to extract is inside a bash variable. For this question I've simplified the problem but in my real use case I have a bash for loop which loops through secret names and inside the for loop I want to extract the appropriate value from the KeyVault with the corresponding secret name like this:
for secretname in secrets; do
echo $($secretname) # This should contain the value of the secret but gives command not found
done
If anyone has an idea what could be happening, any help is very appreciated.
Thanks in Advance!
Look at the syntax you're using.
variable=secretVar
You are creating an environment variable with the literal value secretVar
Then you try to execute the value of the variable $variable with $($variable). So it tries to run the command secretVar, which obviously doesn't exist, and you get an error message.
The syntax you're looking for is
variable=$(secretVar)
just like you used in the first echo command in the script.
If you don't want to run the variable value as a command, the syntax would be $variable, not $($variable)
$variable is the syntax for a Bash environment variable.
$(variable) is the syntax for referencing Azure DevOps variables.
First of all, the script keyword is a shortcut for the command-line task. The task runs a script using cmd.exe on Windows and Bash on other platforms. You need to pay attention to the agent you are using.
If you want to set variables in scripts, you can use task.setvariable logging command. For example:
- script: |
echo $(secretvar)
echo "##vso[task.setvariable variable=variable]$(secretvar)"
- script: |
echo $(variable)
You can find more detailed information in this document.

Create and Writing into a .tempo file using PowerShell Script with variables passed, a specific format

I would like to create a abcd.tempo file using powershell and want to add the content in the below format :
Startdate:12/06/2020
ParamVar2:RISPA
ParamVar3:MLPL
CNTRY=USA
the above is the targeted format , i need to insert into the abcd.tempo file.
Startdate is a variable used and similar to this, other variables as well.
what i meant,Startdate, ParamVar2 , ParamVar3, CNTRY are hardcoded labels and these parameters would be filled with the dynamic values fetched from other code block in my same powershell file.
At present, am able to create a new abcd.tempo file using
new-item -filepath "$myfilepath2createFile" -itemtype File
and i added some dummy content inside that. but
am not able understand how can i create the similar structure , written above.
Another requirement is to call a EXE file from this powershell script and pass anotehr 4 parameters to the EXE
Though i used
Start-Process -FilePath $script:testingexeFile
am stuck at how to pass the 4 parameters to this EXE

Resources