How can you write an inline Powershell#2 task script with if condition? - azure-powershell

I've got a yaml pipeline with a task like this:
- task: PowerShell#2
displayName: Restart my service
inputs:
targetType: 'inline'
script: |
$service = Get-Service | Where-Object{$_.DisplayName -like "My Service"}
if($service)
{
Write-host $service.Name " service is restarting."
Restart-Service $service
}
However, the last '}' is being flagged as an error with "cannot read a block mapping entry". I assume that it's trying to read some of the script as pipeline stuff and doesn't consider it part of the script. How do I get this to be accepted as the whole inline script?

Looks like this was an indenting issue. I got it to work with the following:
if($service) {
Write-host $service.Name " service is restarting."
Restart-Service $service
}

Related

How to pass variable to REST API that is defined in Environment for a yaml pipeline and how to get the REST API to translate the variable

In Azurce DevOPS I have a REST API defined in Environment and I have a yaml pipline that sets a variable. I want to pass this variable to the REST API but it does not work. I can get it to work fine in a Release Pipeline.
The REST API is used as a gate in Environment.
This is the URL and Parameters in the REST API servicenowchecker/api/ServiceNow/GetServiceNowChangeStatus/$(PipelineChangeNumber)?onlyDate=true and my problem is to get the content of $(PipelineChangeNumber) to work. I cannot fin out how it has to be written in URL and Parameters section
my yaml pipeline looks like this - can someone please help me out - thanks
name: Test_serviceNowCheker-$(Date:yyyyMMdd)$(Rev:.r)
trigger: none
variables:
name: PipelineChangeNumber
value: CHG0070534
stages:
stage: Stage1
jobs:
deployment:
pool:
name: dotNET
environment:
name: P1_Urgent_CRB_Environment
job: Say_Hello
pool:
name: dotNEt
steps:
task: PowerShell#2
displayName: Hello from stage1
inputs:
targetType: 'inline'
script:
write-host "Hello from stage 1";
task: PowerShell#2
displayName: Set Outputvar OutPutCase
inputs:
targetType: 'inline'
script: Write-Host "##vso[task.setvariable variable=ChangeNumber;isoutput=true]CHG0070534";
name: OutPutCase
powershell: |
write-host "OutPutCase.ChangeNumber = $(OutPutCase.ChangeNumber)";
write-host "PipelineChangeNumber = $(PipelineChangeNumber)";
stage: Stage2
dependsOn: Stage1
variables:
Stage2_ChangeNumber: $[ stageDependencies.Stage1.Say_Hello.outputs['OutPutCase.ChangeNumber'] ]
jobs:
deployment:
pool:
name: DotNet
environment:
name: P1_Urgent
job: Say_Hello_again
pool:
name: DotNet
steps:
task: PowerShell#2
displayName: Hello from stage2
inputs:
targetType: 'inline'
script:
write-host "Hello from stage 2";
write-host "Stage2_ChangeNumber = $(Stage2_ChangeNumber)";
write-host "PipelineChangeNumber = $(PipelineChangeNumber)";
I have tried to write the as $(PipelineChangeNumber) and $[ $(PipelineChangeNumber) ] then the REST API does not work at all.
When it is written as $[ $PipelineChangeNumber ] the REST API starts up but is sending https://XXXXXX-xx/servicenowchecker/api/ServiceNow/GetServiceNowChangeStatus/$[ $PipelineChangeNumber ]?onlyDate=true and the ¤PipelineChangeNumber is null.

Passing variable between jobs in Azure Pipeline with empty result

I am writing an azure pipeline yml requesting to pass variables between jobs but the variables are not passing through. however, it wasn't successful and it returns an empty variable.
here is my pipeline:
jobs:
- job: UpdateVersion
variables:
terraformRepo: ${{ parameters.terraformRepo }}
pool:
vmImage: ubuntu-latest
steps:
- checkout: self
persistCredentials: true
- checkout: ${{ parameters.terraformRepo }}
- task: AzureCLI#2
displayName: PerformVerUpdate
inputs:
azureSubscription: ${{ parameters.azureSubscriptionName }}
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
echo Step 3 result
echo "Reponame $Reponame"
echo "notify $notify"
echo "pullRequestId $pullRequestId"
echo "##vso[task.setvariable variable=pullRequestId;isOutput=true;]$pullRequestId"
echo "##vso[task.setvariable variable=Reponame;isOutput=true;]$Reponame"
echo "##vso[task.setvariable variable=notify;isOutput=true;]true"
Name: PerformVerUpdate
- job: SlackSuccessNotification
dependsOn: UpdateVersion
condition: and(succeeded(), eq(dependencies.UpdateVersion.outputs['PerformVerUpdate.notify'], 'true'))
pool:
vmImage: 'ubuntu-latest'
variables:
- group: platform-alerts-webhooks
- name: notify_J1
value: $[ dependencies.UpdateVersion.outputs['PerformVerUpdate.notify'] ]
- name: pullRequestId_J1
value: $[ dependencies.UpdateVersion.outputs['PerformVerUpdate.pullRequestId'] ]
- name: Reponame_J1
value: $[ dependencies.UpdateVersion.outputs['PerformVerUpdate.Reponame'] ]
steps:
- task: AzurePowerShell#5
displayName: Slack Notification
inputs:
pwsh: true
azureSubscription: ${{ parameters.azureSubscriptionName }}
ScriptType: 'InlineScript'
TargetAzurePs: LatestVersion
inline: |
write-host "Reponame $(Reponame_J1)"
write-host "pullRequest $(pullRequestId_J1)"
I've tried so many different syntax for it but the variables are still not able to pass through between both jobs - e.g. The condition is passing Null result to second job "(Expanded: and(True, eq(Null, 'true'))". Could anyone help with this?
Firstly 'Name' should be 'name' in lowercase
Name: PerformVerUpdate
The rest of syntax seems fine(I have tested it on Bash task because I do not have Azure subscription).
If renaming 'Name' does not help I suppose the problem may be that your Bash task is running within AzureCLI#2 task.
Maybe as workaround you could add new Bash task right after AzureCLI#2 and try to set there output variable for next job?

getting weird "missing output " error when running a nextflow pipeline

For my data analysis pipeline I am using nextflow (which is a workflow management system) and I gave all the required arguments in the main command but I am getting a weird error. Basically in the output section I introduce the output but the error is missing output. I have made 3 files to run the pipeline including:
1- the module containing the main code to run the tool (ASEReadCounter) and named ASEReadCounter.nf :
process ASEReadCounter {
input:
file vcf_file
file bam_file
path(genome_fasta)
output:
file "${vcf_file}.ASE.csv"
script:
"""
gatk ASEReadCounter \\
-R ${params.genome} \\
-V ${params.vcf_infile} \\
-O ${params.vcf_infile}.txt \\
-I ${params.bam_infile}
"""
}
2- the main file which is used to run the pipeline named main.nf :
#!/usr/bin/env nextflow
nextflow.preview.dsl=2
include ASEReadCounter from './modules/ASEReadCounter.nf'
genome_ch = Channel.fromPath(params.genome)
vcf_file_ch=Channel.fromPath(params.vcf_infile)
bam_infile_ch=Channel.fromPath(params.bam_infile)
workflow {
count_ch=ASEReadCounter(genome_ch, vcf_file_ch, bam_infile_ch)
}
3- config file which is named nextflow.config :
params {
genome = '/hpc/hg38_genome/GRCh38.p13.genome.fa'
vcf_infile = '/hpc/test_data/test/test.vcf.gz'
bam_infile = ‘/hpc/test_data/test/test.sorted.bam'
}
process {
shell = ['/bin/bash', '-euo', 'pipefail']
withName: ASEReadCounter {
container = 'broadinstitute/gatk:latest'
}
}
singularity {
enabled = true
runOptions = '-B /hpc:/hpc -B $TMPDIR:$TMPDIR'
autoMounts = true
cacheDir = '/hpc/diaggen/software/singularity_cache'
}
here is the command I use to run the whole pipeline:
nextflow run -ansi-log false main.nf
Here is the error I am getting:
Error executing process > 'ASEReadCounter (1)'
Caused by:
Missing output file(s) `GRCh38.p13.genome.fa.ASE.csv` expected by process `ASEReadCounter (1)`
Command executed:
gatk ASEReadCounter -R /hpc/hg38_genome/GRCh38.p13.genome.fa \
-V /hpc/test_data/test/test.vcf.gz \
-O /hpc/test_data/test/test.vcf.gz.txt \
-I /hpc/test_data/test/test.sorted.bam
Do you know how I can fix the error?
Your ASEReadCounter process expects a file with the pattern ${vcf_file}.ASE.csv as output, as defined at:
output:
file "${vcf_file}.ASE.csv"
I assume the line -O ${params.vcf_infile}.txt makes it so your gatk ASEReadCounter command writes its output to a file with the pattern $params.vcf_infile}.txt. The expected output and the actual output filenames don't match and nextflow throws an error because it doesn't want to contine since a downstream process might need the output file. You can fix it by matching the expected output and actual output patterns,

Is that possible to use "Invoke-SqlCmd" as a part of Visual Studio Online build step?

We try to get our DB updated as part of the Visual Studio Online build as dedicated build step, but the PowerShell script that we locally using fails to work on the hosted build controller with following error.
2015-07-28T20:35:26.8546681Z ##[error]import-module : The specified module 'sqlps' was not loaded because no valid module file was found in any module
2015-07-28T20:35:26.8546681Z ##[error]directory.
Is there any way to use "Invoke-SqlCmd" in VSO hosted build controller?
Got it working with those lines!
Add-PSSnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue
Add-PSSnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue
Here how I do it:
$moduleName = "SqlServer"
if (Get-Module $moduleName -ListAvailable) {
Write-Host "Module '$moduleName' is available"
} else {
Write-Host "Module '$moduleName' does not exist"
try {
Install-Module -Name $moduleName -Force -AllowClobber
Write-Host "Module '$moduleName' successfully installed"
} catch {
Throw "Installation of module '$moduleName' failed: $($_.Exception)"
}
}

PSake Error Executing MSBuild Command

I have 3 projects in the solution,
A WPFApplication and 2 ClassLibrary projects
When i build the Solution i get error below..
properties {
$base_dir = resolve-path .
$build_dir = "$base_dir\build"
$buildartifacts_dir = "$build_dir\BuildArtifacts"
$sln_file = "$base_dir\Hello.sln"
}
task default -depends Compile
task Clean {
Write-Host "Cleaning solution" -ForegroundColor Green
remove-item -force -recurse $buildartifacts_dir -ErrorAction
SilentlyContinue
}
task Init -depends Clean {
Write-Host "Creating BuildArtifacts directory" -ForegroundColor Green
new-item $buildartifacts_dir -itemType directory
}
task Compile -depend Init {
Write-Host "Compiling ---" $sln_file -ForegroundColor Green
Exec { msbuild $sln_file "/p:OutDir=$build_artifacts_dir"
/p:Configuration=Release /v:quiet }
}
i get the following error's -- what am i doing wrong?
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(2868­,9):
error MSB3023: No destination specified for Copy. Please supply either "DestinationFiles" or "DestinationFolder". [D:\Nusrofe\GrokPSake2\ClassLibrary1\ClassLibrary1.csproj]
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3471­,9):
error MSB4044: The "FindUnderPath" task was not given
a value for the required parameter "Path". [D:\Nusrofe\GrokPSake2\ClassLibrary1\ClassLibrary1.csproj]
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(2868­,9):
error MSB3023: No destination specified for Copy. Ple
ase supply either "DestinationFiles" or "DestinationFolder". [D:\Nusrofe\GrokPSake2\ClassLibrary2\ClassLibrary2.csproj]
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3471­,9):
error MSB4044: The "FindUnderPath" task was not given
a value for the required parameter "Path". [D:\Nusrofe\GrokPSake2\ClassLibrary2\ClassLibrary2.csproj]
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3471­,9):
error MSB4044: The "FindUnderPath" task was not given
a value for the required parameter "Path". [D:\Nusrofe\GrokPSake2\WpfApp\WpfApp.csproj]
build2.ps1:Error executing command: msbuild $sln_file
"/p:OutDir=$build_artifacts_dir" /p:Configuration=Release /v:quiet
Thanks --
Corku
Looks like in your Compile task you have a stray underscore in your $buildartifacts_dir variable. MSBuild probably doesn't know what to do because essentially you are passing it an empty location for the OutDir.