Azure cli running batch azure devops error - azure-devops

I get this error when using azure cli with multiple lines. Single commands are working fine.
The term 'call' is not recognized as the name of a cmdlet, function, script file, or operable program. Check...

I've just created a new release and replicated the behavior. You actually have Powershell selected in the Script Type field. Change it to Batch and it will work as expected.
EDIT
I thought I'd also share how to run Batch scripts from your repo and not worry if the dev has a call statement at the start of each line. I simply have a standardise script in my build which injects call before publishing the artifact
$Deploy_File = Get-Item "Azure-Commands.cmd"
$Deploy_File_Script = $Deploy_File | Get-Content
$Deploy_File_Script_Mod = #()
##Standardise commands for Azure CLI Deployment task
# Loop through Commands
foreach ($Command in $Deploy_File_Script) {
#Insert "CALL" before each azure command
if ($Command -like "az *") {
$Command = "call " + $Command
}
# Build Modified script array
$Deploy_File_Script_Mod += "$Command"
}
#Save to file
$Deploy_File | Set-Content -Value $Deploy_File_Script_Mod
#Output modified commands in file
$Deploy_File | Get-Content
Now you can just call the standardised deploy file from the task above

Related

Can you pipe a string or variable into a downloaded .ps1 file?

I have file with the following contents hosted on dropbox:
(I know it is not currently calling anything)
function test_echo {
[CmdletBinding()]
param (
[Parameter (Position=0,Mandatory = $True, ValueFromPipeline = $True)]
[string]$e
)
echo $e
}
Is there a way to pipe information into this file before it is downloaded and executed?
ex:
"test" | iwr DropBoxLink | iex
and to make this echo out test
this honestly probably has no practical application, but I found myself wondering if it is possible so thought I'd ask.
I know I could define the string as a variable first and execute it but I just want to know if you can pipe it for principles sake
$testEcho = "string"; iwr DropBoxLink | iex
> string
"test" | % -begin { iex (irm $DropBoxUrl) } -process { $_ | test_echo }
The above uses a ForEach-Object call's -Begin block to download and evaluate the script (the usual caveats re iex (Invoke-Expression) apply - you should trust the source), which defines the test_echo function contained in the downloaded script.
The -Begin script block executes before pipeline input is processed, which means that by the time the -Process script block processes (each) pipeline input object, the test_echo function is already defined.
Also note that irm (Invoke-RestMethod) rather than iwr (Invoke-WebRequest) is used, given that you're only interested in the content of the script.
Of course, this doesn't gain you much, as you could simply use two statements, which has the added advantage that all pipeline input (should there be multiple input objects) are handled by a single test_echo invocation:
iex (irm $DropBoxUrl) # Download and effectively dot-source the script.
"test" | test_echo # Pipe to the script's `test_echo` function.
A general caveat is that if the downloaded script contains an exit statement that is hit during evaluation, the calling shell exits as a whole.
Since in effect you need to dot-source the downloaded script in order to make its functions available, the only way to solve the exit problem is to download to a (temporary) file first, and dout-source that.
If dot-sourcing isn't needed, calling via the PowerShell CLI (child process) may be an option - see below.
GitHub issue #5909 proposes a future enhancement that would allow piping Invoke-WebRequest (iwr) calls to Invoke-Command (icm) for direct-from-the-web downloading and execution, without the exit problem (iwr $DropboxLink | icm).
Note that if your downloaded script were to accept pipeline input directly, you could use [scriptblock]::Create() as follows (Invoke-Expression is not an option, because it doesn't accept pipeline input):
# Assumes that the script located at $DropBoxUrl
# *itself* accepts pipeline input.
# Use . (...) if you want to *dot-source* the script, as
# Invoke-Expression would implicitly do.
"test" | & ([scriptblock]::Create((irm $DropBoxUrl))
To work around the exit problem, you can call via the CLI and a script block, using pwsh, the PowerShell (Core) 7+ CLI, in this example; use powershell.exe for Windows PowerShell:
# Assumes that the script located at $DropBoxUrl
# *itself* accepts pipeline input.
'test' |
pwsh -NoProfile {
$input | & ([scriptblock]::Create($args[0]))
} -args (irm $DropBoxUrl)

Enable Schedule Trigger in Azure Data factory through poweshell script

I am enabling schedule trigger in data factory through powershell script in release pipeline of Azure DevOps.
I have written script as below:
$triggersADF | ForEach-Object { Start-AzureRmDataFactoryV2Trigger -ResourceGroupName -DataFactoryName -Name $_.name -Force }
But I am getting below error.
The term 'Start-AzureRmDataFactoryV2Trigger' 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.
PowerShell exited with code '1'
How it can be done?
Install/update latest AzureRM.DataFactoryV2 module. And then
Restart your powershell.

The Azure CLI task on my classic pipeline shows an error using inlinescript

I use the Azure CLI task on my Azure classic pipeline (no YAML). The pipeline works appropriately, the only problem is that this task shows an error. I use this inline script for the task:
$containers = $(call az storage container list --connection-string '...') | ConvertFrom-Json -Depth 5
$containers | where{$_.name -like "..."}
$lastContainers = $containers | Select-Object -Last 5
$containers.ForEach({ If ($_ -notin $lastContainers) {call az storage container delete --name $_.name --connection-string '...' } });
I replaced my real connection string with .... I get this error about the last task (Azure CLI) when I run the pipeline (all of the previous steps end successfully):
D:\a\9\s>$containers = $(call az storage container list --connection-string '...') | ConvertFrom-Json -Depth 5
'$containers' is not recognized as an internal or external command, operable program or batch file.
##[error]Script failed with error: Error: The process 'D:\a\_temp\azureclitaskscript1587632019088.bat' failed with exit code 255
I have a similar YAML pipeline that uses this same script and everything works just fine. I use call in front of Azure commands as suggested on GitHub.
In AzureCLI#2 you select script type:
Type of script: PowerShell/PowerShell Core/Bat/Shell script. Select
bash/pscore script when running on Linux agent or batch/ps/pscore
script when running on Windows agent. PowerShell Core script can run
on cross-platform agents (Linux, macOS, or Windows)
Additionally there may appear some differences in Linux and Windows (this issue with call statement), but if you select there pscore for instance you should get rid of 'call` statement.
So please provide more info in what system and terminal you run this script to give you more help.
I ran you script without call in powershell and all was fine. Please try run it in that way.

Azure Function - PowerShell, installing az cmdlet :: The term 'az' is not recognized as the name of a cmdlet

I am attempting to install the az cmdlet onto Kudu for my Azure Function. I am currently following this guide:
How to install a PowerShell module in an Azure Function
... however - I am still getting the following error within my Azure Function:
az : The term 'az' 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.
at run.ps1: line 1
Steps I have done till now:
Created a module folder under D:\home\site\wwwroot\Communication_with_Azure_Container_Registry\>
Within the module folder I have added the contents of azure-cli/2.0.35/..., (which looks like this):
... Azure Function code is very simple to proof out the ability to install the cmdlet:
if (-not (Get-Module -Name "az"))
{
Write-Output "azure-cli not installed";
}
else
{
Write-Output "azure-cli installed";
}
$test = 'az --help'
Invoke-Expression $test
Write-output `n$test
Question:
Is there something within my configuration that is not allowing for the az cmdlet to install?
Is there an alternative way to gain access to the azure-cli without implementing the node module?
I solved the following part of your problem
az : The term 'az' 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. at run.ps1: line 1
If you execute
Test-Path -Path 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin'
You will probably get False. This means that you need to install the Azure CLI eg from https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest
I haven't testing this myself, but according to https://blogs.msdn.microsoft.com/powershell/2017/02/24/using-powershell-modules-in-azure-functions/ you should be able to do an Import-Module. In their example...
Write-Output “Loading Wunderlist Module”
import-module ‘D:\Home\site\wwwroot\HttpTriggerPowerShellDemo\Modules\Wunderlist\1.0.10\Wunderlist.psm1’
$Result = Get-Help Get-WunderlistUser | Out-String
Write-Output $Result
Install Azure CLI
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
This should give you the az command

Powershell PSEXEC not working via Control-M

PS Version: 2.0
Hi All,
I am trying to run the batch file from a powershell script using psexec.
The script runs fine while triggering manually or using windows task scheduler; however, powershell get triggered from Control-m but do not complete the part where psexec is used. The rest part of powershell script runs fine.
Below is the function which is not working, besides it do not give any error, it just freezes the script:
function Archive_Logs($Server_Name,$Tool_Path,$Name)
{
foreach($TPath in $Tool_Path){
C:\Windows\System32\PsExec.exe \\$Server_Name "$TPath\ziplogs.bat"
if($LastExitCode -eq 0)
{
"$Name Server logs archive Started successfully at $(Get-Date)" | Out-File $LOGFILE -Append
}
}
}
The account used by Control-M is local admin on the servers.
How are you calling the Powershell? When you create a .bat file and invoke it from there it fixes a lot of issues, e.g, bat file contains 1 line -
powershell -command "& C:\MyPSscripts\archiver.ps1"
See -
https://communities.bmc.com/thread/117415