Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AzurePowershell Task Cannot Find Profile.ps1

In Azure Devops, I have a YAML pipeline that executes a Powershell task that:

  • Appears to invoke my script, but no output is echoed to the logs, and
  • The task ends with an error, Could not find a part of the path 'C:\Users\VssAdministrator\Documents\WindowsPowerShell\profile.ps1'.

How do I:

  • get PS to echo the results to the pipeline logs, and
  • resolve the error at the end of the task

The step:

- task: AzurePowerShell@4
  displayName: 'Azure PowerShell script: SetAzureFirewallRule'
  inputs:
    azureSubscription: '***'
    ScriptPath: SetAzureFirewallRule.ps1
    ScriptArguments: '-ServerName {myserver} -ResourceGroup {myrg} -AzureFirewallName {rulename}'
    azurePowerShellVersion: LatestVersion

The script file is straight forward:

[CmdletBinding(DefaultParameterSetName = 'None')]
param
(
  [String] [Parameter(Mandatory = $true)] $ServerName,
  [String] [Parameter(Mandatory = $true)] $ResourceGroup,
  [String] $AzureFirewallName = "AzureWebAppFirewall"
)
Enable-AzureRmAlias -Scope CurrentUser
$agentIP = (New-Object net.webclient).downloadstring("http://checkip.dyndns.com") -replace "[^\d\.]"
echo "IP is $($agentIP)"
New-AzureRmSqlServerFirewallRule -ResourceGroupName $ResourceGroup -ServerName $ServerName -FirewallRuleName $AzureFirewallName -StartIPAddress $agentIp -EndIPAddress $agentIp

Note I had to deviate from the script recommended by Microsoft by adding Enable-AzureRmAlias -Scope CurrentUser. This script works as expected when execute from my development machine, including echoing of the output.

When it executes, the following is logged:

##[section]Starting: Azure PowerShell script: SetAzureFirewallRule
==============================================================================
Task         : Azure PowerShell
Description  : Run a PowerShell script within an Azure environment
Version      : 4.159.3
Author       : Microsoft Corporation
Help         : [Learn more about this task](https://go.microsoft.com/fwlink/?LinkID=613749)
==============================================================================
##[command]Import-Module -Name C:\Modules\az_2.6.0\Az.Accounts\1.6.2\Az.Accounts.psd1 -Global
##[command]Clear-AzContext -Scope Process
##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
##[command]Connect-AzAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud
##[command] Set-AzContext -SubscriptionId *** -TenantId ***
##[command]& 'd:\a\1\s\SetAzureFirewallRule.ps1' -ServerName *** -ResourceGroup *** -AzureFirewallName ***
##[command]Disconnect-AzAccount -Scope Process -ErrorAction Stop
##[command]Clear-AzContext -Scope Process -ErrorAction Stop
##[error]Could not find a part of the path 'C:\Users\VssAdministrator\Documents\WindowsPowerShell\profile.ps1'.
##[section]Finishing: Azure PowerShell script: SetAzureFirewallRule

I have tried this with different hosts:

  • windows-latest
  • windows-2019
  • vs2017-win2016

with the same results.

like image 682
Eric Patrick Avatar asked Aug 09 '26 11:08

Eric Patrick


1 Answers

In executing the script, it looks like there's a failure loading the user profile.

You can work around this in two ways:

  • create a dummy profile

    New-Item -Path $Profile.CurrentUserCurrentHost -Force
    
  • don't load profiles

    powershell.exe -NoProfile -File ...
    
like image 145
Maximilian Burszley Avatar answered Aug 11 '26 01:08

Maximilian Burszley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!