Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixes] Ensure $profile is always available #1747

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions utilities/pipelines/sharedScripts/Set-EnvironmentOnAgent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ function Set-EnvironmentOnAgent {
}

# MS-hosted agents have pre-installed modules in a specific path. Let's make them discoverable if available.
# Always create the $profile if it does not exist (to avoid later need of case handling)
if (-not (Test-Path $profile)) {
$null = New-Item -Path $profile -Force
}
if ((Test-Path '/usr/share/') -and ((Get-ChildItem -Path '/usr/share/az_*' -Directory).Count -gt 0)) {
$preInstalledModulePaths = Get-ChildItem -Path '/usr/share/az_*' -Directory
$maximumVersionPath = '/usr/share/az_{0}' -f (($preInstalledModulePaths | ForEach-Object { ($_ -split 'az_')[1] }) | ForEach-Object { [version]$_ } | Measure-Object -Maximum ).Maximum
Expand All @@ -208,19 +212,13 @@ function Set-EnvironmentOnAgent {
# Set job module path (machine)
[Environment]::SetEnvironmentVariable('PSModulePath', ('{0};{1}' -f ([Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')), $maximumVersionPath), 'Machine')
# Set PS-Profile (for non-ps tasks)
if (-not (Test-Path $profile)) {
$null = New-Item -Path $profile -Force
}
Add-Content -Path $profile -Value "`$env:PSModulePath += `";$maximumVersionPath`""
} else {
# Set step module path (process)
$env:PSModulePath += ":$maximumVersionPath"
# Set job module path (machine)
[Environment]::SetEnvironmentVariable('PSModulePath', ('{0}:{1}' -f ([Environment]::GetEnvironmentVariable('PSModulePath', 'Machine')), $maximumVersionPath), 'Machine')
# Set PS-Profile (for non-ps tasks)
if (-not (Test-Path $profile)) {
$null = New-Item -Path $profile -Force
}
Add-Content -Path $profile -Value "`$env:PSModulePath += `":$maximumVersionPath`""
}
}
Expand Down