I’m using an Azure DevOps pipeline to start/stop Azure Web Apps and a Function App on a schedule. The pipeline uses an AzurePowerShell@5
task with a service connection to authenticate to azure, Despite specifying the correct subscription ID, it still fails with ERROR: Please run 'az login'
.
Pipeline YAML:
trigger: none
schedules:
- cron: "0 7 * * Mon-Fri"
displayName: "Start WebApp at 7 AM (Mon-Fri)"
branches:
include:
- main
always: true
- cron: "0 19 * * Mon-Fri"
displayName: "Stop WebApp at 7 PM (Mon-Fri)"
branches:
include:
- main
always: true
stages:
- stage: dev
displayName: 'Start and Stop Dev Environment'
variables:
- name: subscriptionId
value: '12345678-abcd-6789-1122-ab12bc34de56ef'
- name: resourceGroupName
value: 's148d01-vyed'
- name: webAppNames
value: |
- 's148d01-as-vyed-admin-01'
- 's148d01-as-vyed-pub-01'
- name: functionAppName
value: 's148d01-fa-vyed-01'
jobs:
- job: Dev_Webapp
steps:
- task: AzurePowerShell@5
displayName: 'Powershell task'
inputs:
azureSubscription: s148d.azdo-deployment-dfe-gov
ScriptType: filePath
ScriptPath: $(Build.SourcesDirectory)/scripts/s148Autoschdulers.ps1
ScriptArguments: '-subscriptionId "$(subscriptionId)" -resourceGroupName "$(resourceGroupName)" -webAppNames "$(webAppNames)" -functionAppName "$(functionAppName)"'
azurePowerShellVersion: 'LatestVersion'
pwsh: true
PowerShell Script
param (
[Parameter(Mandatory=$true)]
[string]$subscriptionId,
[Parameter(Mandatory=$true)]
[string]$resourceGroupName,
[Parameter(Mandatory=$true)]
[string[]]$webAppNames,
[Parameter(Mandatory=$true)]
[string]$functionAppName
)
# Setting your Azure subscription
az account set --subscription $subscriptionId
# Get the current hour and current day
$currentHour = (Get-Date).Hour
$currentDay = (Get-Date).DayOfWeek
foreach ($webAppName in $webAppNames) {
$webAppStatus = az webapp show --name $webAppName --resource-group $resourceGroupName --query "state" --output tsv
Write-Output "Current status of Azure Web App $webAppName: $webAppStatus"
if ($currentHour -eq 7) {
if ($webAppStatus -eq "Stopped") {
az webapp start --name $webAppName --resource-group $resourceGroupName
Write-Output "Azure Web App $webAppName started successfully."
} else {
Write-Output "Azure Web App $webAppName is already running."
}
} elseif ($currentHour -eq 19) {
if ($webAppStatus -eq "Running") {
az webapp stop --name $webAppName --resource-group $resourceGroupName
Write-Output "Azure Web App $webAppName stopped successfully."
} else {
Write-Output "Azure Web App $webAppName is already stopped."
}
} else {
Write-Output "Current time is not 7 AM or 7 PM. WebApp status will not be changed. It will automatically start or stop the webapp at 7AM and 7PM on Monday to Friday"
}
}
# Get the status of the function app
$functionAppStatus = az functionapp show --name $functionAppName --resource-group $resourceGroupName --query "state" --output tsv
Write-Output "Current status of Azure Function App $functionAppName: $functionAppStatus"
if ($currentDay -eq 'Monday') {
if ($currentHour -eq 7) {
if ($functionAppStatus -eq "Stopped") {
az functionapp start --name $functionAppName --resource-group $resourceGroupName
Write-Output "Azure Function App $functionAppName started successfully."
} else {
Write-Output "Azure Function App $functionAppName is already running."
}
} elseif ($currentHour -eq 19) {
if ($functionAppStatus -eq "Running") {
az functionapp stop --name $functionAppName --resource-group $resourceGroupName
Write-Output "Azure Function App $functionAppName stopped successfully."
} else {
Write-Output "Azure Function App $functionAppName is already stopped."
}
} else {
Write-Output "Current time is not 7 AM or 7 PM on Monday. The function app status cannot be changed."
}
} else {
Write-Output "Today is not Monday. Function App status will not be changed. It will automatically start or stop the function app at 7AM and 7PM on Monday"
}
Error Log
==============================================================================
Task : Azure PowerShell
Description : Run a PowerShell script within an Azure environment
Version : 5.252.0
Author : Microsoft Corporation
Help :
==============================================================================
Generating script.
Formatted command: . '/home/vsts/work/1/s/scripts/s148Autoschdulers.ps1' -subscriptionId "12345678-abcd-6789-1122-ab12bc34de56ef" -resourceGroupName "s148d01-vyed" -webAppNames "- 's148d01-as-vyed-admin-01'
- 's148d01-as-vyed-pub-01'
" -functionAppName "s148d01-fa-vyed-01"
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command . '/home/vsts/work/_temp/3e1608a1-6ae4-48a1-9056-f34660231aed.ps1'
File saved!
Import-Module -Name /usr/share/az_12.1.0/Az.Accounts/4.0.2/Az.Accounts.psd1 -Global
Clear-AzContext -Scope Process
Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
Connect-AzAccount -ServicePrincipal -Tenant 9c7d9dd3-840c-4b3f-818e-552865082e16 -Credential System.Management.Automation.PSCredential -Environment AzureCloud @processScope
Set-AzContext -SubscriptionId 12345678-abcd-6789-1122-ab12bc34de56ef -TenantId 9c8d7ee3-987c-123c-888e-552865082e16
ERROR: The subscription '12345678-abcd-6789-1122-ab12bc34de56ef' doesn't exist in cloud 'AzureCloud'.
ERROR: Please run 'az login' to setup account.
Azure Web App '- 's148d01-as-vyed-admin-01'
- 's148d01-as-vyed-pub-01'
' is currently:
The current time is not 7 AM or 7 PM. WebApp status will remain unchanged. It will automatically start and stop at 7 AM and 7 PM from Monday to Friday.
ERROR: Please run 'az login' to setup account.
Azure Function App 's148d01-fa-vyed-01' is currently:
The Function App status will remain unchanged. It will automatically start and stop at 7 AM and 7 PM on every Monday .
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command . '/home/vsts/work/_tasks/AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62/5.252.0/RemoveAzContext.ps1'
Disconnect-AzAccount -Scope CurrentUser -ErrorAction Stop
Disconnect-AzAccount -Scope Process -ErrorAction Stop
Clear-AzContext -Scope Process -ErrorAction Stop
Finishing: Powershell task
How can I run the script successfully Any help would be greatly appreciated! Thanks in advance.
I’m using an Azure DevOps pipeline to start/stop Azure Web Apps and a Function App on a schedule. The pipeline uses an AzurePowerShell@5
task with a service connection to authenticate to azure, Despite specifying the correct subscription ID, it still fails with ERROR: Please run 'az login'
.
Pipeline YAML:
trigger: none
schedules:
- cron: "0 7 * * Mon-Fri"
displayName: "Start WebApp at 7 AM (Mon-Fri)"
branches:
include:
- main
always: true
- cron: "0 19 * * Mon-Fri"
displayName: "Stop WebApp at 7 PM (Mon-Fri)"
branches:
include:
- main
always: true
stages:
- stage: dev
displayName: 'Start and Stop Dev Environment'
variables:
- name: subscriptionId
value: '12345678-abcd-6789-1122-ab12bc34de56ef'
- name: resourceGroupName
value: 's148d01-vyed'
- name: webAppNames
value: |
- 's148d01-as-vyed-admin-01'
- 's148d01-as-vyed-pub-01'
- name: functionAppName
value: 's148d01-fa-vyed-01'
jobs:
- job: Dev_Webapp
steps:
- task: AzurePowerShell@5
displayName: 'Powershell task'
inputs:
azureSubscription: s148d.azdo-deployment-dfe-gov
ScriptType: filePath
ScriptPath: $(Build.SourcesDirectory)/scripts/s148Autoschdulers.ps1
ScriptArguments: '-subscriptionId "$(subscriptionId)" -resourceGroupName "$(resourceGroupName)" -webAppNames "$(webAppNames)" -functionAppName "$(functionAppName)"'
azurePowerShellVersion: 'LatestVersion'
pwsh: true
PowerShell Script
param (
[Parameter(Mandatory=$true)]
[string]$subscriptionId,
[Parameter(Mandatory=$true)]
[string]$resourceGroupName,
[Parameter(Mandatory=$true)]
[string[]]$webAppNames,
[Parameter(Mandatory=$true)]
[string]$functionAppName
)
# Setting your Azure subscription
az account set --subscription $subscriptionId
# Get the current hour and current day
$currentHour = (Get-Date).Hour
$currentDay = (Get-Date).DayOfWeek
foreach ($webAppName in $webAppNames) {
$webAppStatus = az webapp show --name $webAppName --resource-group $resourceGroupName --query "state" --output tsv
Write-Output "Current status of Azure Web App $webAppName: $webAppStatus"
if ($currentHour -eq 7) {
if ($webAppStatus -eq "Stopped") {
az webapp start --name $webAppName --resource-group $resourceGroupName
Write-Output "Azure Web App $webAppName started successfully."
} else {
Write-Output "Azure Web App $webAppName is already running."
}
} elseif ($currentHour -eq 19) {
if ($webAppStatus -eq "Running") {
az webapp stop --name $webAppName --resource-group $resourceGroupName
Write-Output "Azure Web App $webAppName stopped successfully."
} else {
Write-Output "Azure Web App $webAppName is already stopped."
}
} else {
Write-Output "Current time is not 7 AM or 7 PM. WebApp status will not be changed. It will automatically start or stop the webapp at 7AM and 7PM on Monday to Friday"
}
}
# Get the status of the function app
$functionAppStatus = az functionapp show --name $functionAppName --resource-group $resourceGroupName --query "state" --output tsv
Write-Output "Current status of Azure Function App $functionAppName: $functionAppStatus"
if ($currentDay -eq 'Monday') {
if ($currentHour -eq 7) {
if ($functionAppStatus -eq "Stopped") {
az functionapp start --name $functionAppName --resource-group $resourceGroupName
Write-Output "Azure Function App $functionAppName started successfully."
} else {
Write-Output "Azure Function App $functionAppName is already running."
}
} elseif ($currentHour -eq 19) {
if ($functionAppStatus -eq "Running") {
az functionapp stop --name $functionAppName --resource-group $resourceGroupName
Write-Output "Azure Function App $functionAppName stopped successfully."
} else {
Write-Output "Azure Function App $functionAppName is already stopped."
}
} else {
Write-Output "Current time is not 7 AM or 7 PM on Monday. The function app status cannot be changed."
}
} else {
Write-Output "Today is not Monday. Function App status will not be changed. It will automatically start or stop the function app at 7AM and 7PM on Monday"
}
Error Log
==============================================================================
Task : Azure PowerShell
Description : Run a PowerShell script within an Azure environment
Version : 5.252.0
Author : Microsoft Corporation
Help : https://aka.ms/azurepowershelltroubleshooting
==============================================================================
Generating script.
Formatted command: . '/home/vsts/work/1/s/scripts/s148Autoschdulers.ps1' -subscriptionId "12345678-abcd-6789-1122-ab12bc34de56ef" -resourceGroupName "s148d01-vyed" -webAppNames "- 's148d01-as-vyed-admin-01'
- 's148d01-as-vyed-pub-01'
" -functionAppName "s148d01-fa-vyed-01"
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command . '/home/vsts/work/_temp/3e1608a1-6ae4-48a1-9056-f34660231aed.ps1'
File saved!
Import-Module -Name /usr/share/az_12.1.0/Az.Accounts/4.0.2/Az.Accounts.psd1 -Global
Clear-AzContext -Scope Process
Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
Connect-AzAccount -ServicePrincipal -Tenant 9c7d9dd3-840c-4b3f-818e-552865082e16 -Credential System.Management.Automation.PSCredential -Environment AzureCloud @processScope
Set-AzContext -SubscriptionId 12345678-abcd-6789-1122-ab12bc34de56ef -TenantId 9c8d7ee3-987c-123c-888e-552865082e16
ERROR: The subscription '12345678-abcd-6789-1122-ab12bc34de56ef' doesn't exist in cloud 'AzureCloud'.
ERROR: Please run 'az login' to setup account.
Azure Web App '- 's148d01-as-vyed-admin-01'
- 's148d01-as-vyed-pub-01'
' is currently:
The current time is not 7 AM or 7 PM. WebApp status will remain unchanged. It will automatically start and stop at 7 AM and 7 PM from Monday to Friday.
ERROR: Please run 'az login' to setup account.
Azure Function App 's148d01-fa-vyed-01' is currently:
The Function App status will remain unchanged. It will automatically start and stop at 7 AM and 7 PM on every Monday .
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command . '/home/vsts/work/_tasks/AzurePowerShell_72a1931b-effb-4d2e-8fd8-f8472a07cb62/5.252.0/RemoveAzContext.ps1'
Disconnect-AzAccount -Scope CurrentUser -ErrorAction Stop
Disconnect-AzAccount -Scope Process -ErrorAction Stop
Clear-AzContext -Scope Process -ErrorAction Stop
Finishing: Powershell task
How can I run the script successfully Any help would be greatly appreciated! Thanks in advance.
Share Improve this question asked Mar 26 at 19:21 NishNish 958 bronze badges1 Answer
Reset to default 1Checking your PowerShell script, you are running Azure CLI commands, which require az login
to authenticate with Azure. But Azure PowerShell uses Azure-Connect
to login to Azure. Based on your script, it's suggested that you could use AzureCLI@2
task.
Besides, I noticed that webAppNames
is defined as multi-line variable. The type of variable in Azure DevOps is string. If you pass webAppNames
as you defined, $webAppNames
in your PowerShell script will have only one element and az webapp show
will fail as shown below:
To resolve this issue, define webAppNames
as value: '"s148d01-as-vyed-admin-01", "s148d01-as-vyed-pub-01"'
and remove " "
when passing webAppNames
in the arguments
.
The modified YAML file is:
trigger: none
schedules:
- cron: "0 7 * * Mon-Fri"
displayName: "Start WebApp at 7 AM (Mon-Fri)"
branches:
include:
- main
always: true
- cron: "0 19 * * Mon-Fri"
displayName: "Stop WebApp at 7 PM (Mon-Fri)"
branches:
include:
- main
always: true
stages:
- stage: dev
displayName: 'Start and Stop Dev Environment'
variables:
- name: subscriptionId
value: '12345678-abcd-6789-1122-ab12bc34de56ef'
- name: resourceGroupName
value: 's148d01-vyed'
- name: webAppNames
value: '"s148d01-as-vyed-admin-01", "s148d01-as-vyed-pub-01"'
- name: functionAppName
value: 's148d01-fa-vyed-01'
jobs:
- job: Dev_Webapp
steps:
- task: AzureCLI@2
inputs:
azureSubscription: '{Your ARM SC}'
scriptType: 'pscore'
scriptLocation: 'scriptPath'
scriptPath: '$(Build.SourcesDirectory)/scripts/s148Autoschdulers.ps1'
arguments: '-subscriptionId "$(subscriptionId)" -resourceGroupName "$(resourceGroupName)" -webAppNames $(webAppNames) -functionAppName "$(functionAppName)"'
Result:
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744130056a4559797.html
评论列表(0条)