How can I get a current date-time and pass it as a variable to some Deployment task?
You can define a variable with any value, and then modify the variable as current date. Detail steps as below:
Assume the variable name is time
, and we set the value as none
. If you need to use the variable for a environment, you can define it in environment variables. Else you should define it in variables Tab.
Type: Inline Script.
Inline script:
$date=$(Get-Date -Format g);
Write-Host "##vso[task.setvariable variable=time]$date"
MM/DD/YYYY HH:MM AM/PM
here. You can use other date formats.$(time)
.Documentation for Defining Variables: Set Variables Using Expressions has a nugget of gold for the answer to this question in the example for creating a counter value that is reset daily.
a: $[counter(format('{0:yyyyMMdd}', pipeline.startTime), 100)]
The pipeline.startTime
variable used here is subtle and not mentioned anywhere in the Pipeline Predefined Variables documentation, even when being careful to land on the correct documentation based on the pipeline method being used. As is suggested HERE and in some of the answers on this thread, certain variables may have different values or not exist at all depending on where you are while trying to access them.