Eric Patrick
Eric Patrick

Reputation: 2247

AzurePowershell Task Cannot Find Profile.ps1

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

How do I:


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:

with the same results.

Upvotes: 3

Views: 861

Answers (2)

Codingpan
Codingpan

Reputation: 318

Try to add a $(System.DefaultWorkingDirectory) here might fix your path issue.

ScriptPath: '$(System.DefaultWorkingDirectory)/SetAzureFirewallRule.ps1'

Upvotes: 0

Maximilian Burszley
Maximilian Burszley

Reputation: 19694

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 ...
    

Upvotes: 1

Related Questions