How to provide a SAS token to Azure CLI within PowerShell - powershell

I'm using the Azure CLI 2.0 from PowerShell to manage a storage account. I have a SAS token (which I am storing in a variable) and I want to use it in a command. Here's the script I'm running:
$sasToken = 'st=2017-11-13T10%3A55%3A06Z&se=2017-11-13T11%3A27%3A06Z&sp=w&spr=https&sv=2017-04-17&sr=c&sig=%2BA6LDTwHes6JdxEAHXSvbYc70y30OcznjMVSyFbCXog%3D'
az storage blob upload `
--account-name mystorageaccount `
--container-name mycontainer `
--file c:\temp\file.txt `
--name file.txt `
--sas-token $sasToken
When I run this, I get this error:
The specified resource does not exist.
'se' is not recognized as an internal or external command,
operable program or batch file.
'sp' is not recognized as an internal or external command,
operable program or batch file.
'spr' is not recognized as an internal or external command,
operable program or batch file.
'sv' is not recognized as an internal or external command,
operable program or batch file.
'sr' is not recognized as an internal or external command,
operable program or batch file.
'sig' is not recognized as an internal or external command,
operable program or batch file.
It appears to me that PowerShell is truncating the SAS token every time it sees an ampersand, and the Azure CLI isn't getting this as all part of the same string.
Is there a way to force PowerShell to call the Azure CLI with the SAS token exactly as-is?

Try wrapping the SAS token in a 2nd set of quotes:
$sasToken = '"st=2017-11-13T10%3A55%3A06Z&se=2017-11-13T11%3A27%3A06Z&sp=w&spr=https&sv=2017-04-17&sr=c&sig=%2BA6LDTwHes6JdxEAHXSvbYc70y30OcznjMVSyFbCXog%3D"'
so that the CLI command looks like this:
--sas-token "st=2017-11-13T10%3A55%3A06Z&se=2017-11-13T11%3A27%3A06Z&sp=w&spr=https&sv=2017-04-17&sr=c&sig=%2BA6LDTwHes6JdxEAHXSvbYc70y30OcznjMVSyFbCXog%3D"

I get the problem this morning and solved the problem like that when SAS Uri is stored in a variable:
$sasUri = "`"$pkgSasUri`""
....
az deployment group create `
--resource-group $rg `
--name create_deployment_cses `
--template-file .\template.json `
--parameters .\parameters.json configurationSasUri=$confSasUri packageSasUri=$pkgSasUri
based on Microsoft devblogs

If you are getting the SAS key from the Azure CLI, do note that if you run it with "-o json" you will get as output the value of the SAS key in quotes. You can directly assign this to a variable and it can be used by the azure CLI without issues (at least, I could place it in an AKV secret)
This is the function I wrote to get that variable as part of my ps1 code:
function Get-SASToken {
param (
# Storage Account
[Parameter(Mandatory=$true)][alias("a")][string]$StorageAccount,
# Storage Container name
[Parameter(Mandatory=$true)][alias("c")][string]$StorageContainer,
# Parameter help description
[Parameter(Mandatory=$true)][alias("p")][string]$SAPolicy
)
echo (az storage container generate-sas --account-name $StorageAccount -n $StorageContainer --policy-name $SAPolicy -o json)
}

Related

calling Powershell script from Terraform script

I am trying to execute a powershell script from a terraform script. The powershell commands in the file is not being recognized while execution. Below is the code:
resource "null_resource" "example2" {
provisioner "local-exec" {
command = "powershell -file ./sample.ps1"
}
}
Below is the error:
null_resource.example2 (local-exec): Add-AzIotHubDevice : The term 'Add-AzIotHubDevice' is not recognized as the name of a cmdlet, function, script file, or operable
enter image description here
I think this is already answered here:
How to run a powershell script in terraform?
Also, I recommend calling the exact powershell .exe you want. Some servers have PowerShell v5 and v7. Execution policy can cause issues too.
Recommend command that you can combine with the other answer:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "./sample1.ps" -ExecutionPolicy "Bypass"
another example:
https://ripon-banik.medium.com/run-powershell-script-in-terraform-54d6586c2827

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.

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

Azure Powershell - Add-AzureVirtualIP cmdlet not found

I need to add another VIP for my Azure cloud service using Powershell for Azure, since there is no way to do it via the web console. In the past I was able to do it by using the Add-AzureVirtualIP command.
However, that command seems to no longer be found since MS switched everything to Azure Resource Manager mode. How can I use this command again, or is there another way to add a VIP to my cloud service?
Add-AzureVirtualIP : The term 'Add-AzureVirtualIP' 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
According to your error code, it seems the Azure PowerShell is not loaded correctly. we can install Azure PowerShell with this command:
Install-Module -Name Azure
More information about Azure PowerShell, please refer to the link.

How to use Azure Automation to start and stop the Service Fabric Nodes?

In our project, we want to use Azure Automation to start and stop Service Fabric Cluster nodes.
At first, we use the:
Enable-ServiceFabricNode -NodeName "Node1"
Disable-ServiceFabricNode -NodeName "Node1"
To start and stop the Nodes, it works in my Windows Powershell Script, but when we add it to the Azure Automation Script, it show
“The term ‘Enable-ServiceFabricNode’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name … ”
Then, we try to use the Azure Resources Manager to get the scale set VM, then start and stop the VM:
$Cred = Get-AutomationPSCredential -Name '***#***.com';
Add-AzureRmAccount -Credential $Cred
Set-AzureRmContext -TenantId ‘000000-786D-4361-A787-2C398163274F’
InlineScript
{
Stop-AzureRmVmss -ResourceGroupName "testRG" -VMScaleSetName "Test"
}
Also it works in my windows powershell script, but we try to add it to Azure Automation, it shows:
“The term ‘Stop-AzureRmVmss’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name … ”
The Azure Automation is use the Azure Powershell Script, right?
So is there any way that we can use the Azure Automation Script to Start and Stop the Service Fabric Cluster nodes?
Thanks so much!
Is this an on-premise cluster? Can you confirm if you enable Powershell script execution by running command below?
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope CurrentUser
By default, Windows blocks these scripts from running. To enable them,
you must modify your PowerShell execution policy. Open PowerShell as
an administrator and enter the following command:
You most likely don't have the latest Azure Modules. You can get the latest modules by following these steps -> https://learn.microsoft.com/en-us/azure/automation/automation-update-azure-modules