Skip to content

Commit 7a3db63

Browse files
Update profile.ps1 template to set Disable-AzContextAutosave scope to process instead of current user. (#6615)
* Update profile.ps1 template for PowerShell function apps. (#6327)
1 parent 7dd95ae commit 7a3db63

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/WebJobs.Script/FileProvisioning/PowerShell/profile.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
#
99
# You can define helper functions, run commands, or specify environment variables
1010
# NOTE: any variables defined that are not environment variables will get reset after the first execution
11+
1112
# Authenticate with Azure PowerShell using MSI.
1213
# Remove this if you are not planning on using MSI or Azure PowerShell.
13-
14-
if ($env:MSI_SECRET -and (Get-Module -ListAvailable Az.Accounts)) {
15-
    Connect-AzAccount -Identity
14+
if ($env:MSI_SECRET) {
15+
Disable-AzContextAutosave -Scope Process | Out-Null
16+
Connect-AzAccount -Identity
1617
}
1718

1819
# Uncomment the next line to enable legacy AzureRm alias in Azure PowerShell.
1920
# Enable-AzureRmAlias
21+
2022
# You can also define functions or aliases that can be referenced in any of your PowerShell functions.

test/WebJobs.Script.Tests/FileProvisioning/PowerShellFileProvisionerTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class PowerShellFileProvisionerTests
3232
private const string PSGalleryEmptyFeedResourceFileName =
3333
"Microsoft.Azure.WebJobs.Script.Tests.Resources.FileProvisioning.PowerShell.PSGalleryEmptyFeed.xml";
3434

35+
private const string ProfileFileResourceFileName =
36+
"Microsoft.Azure.WebJobs.Script.Tests.Resources.FileProvisioning.PowerShell.profile.ps1";
37+
3538
private readonly string _scriptRootPath;
3639
private readonly ILoggerFactory _loggerFactory = new LoggerFactory();
3740
private readonly TestLoggerProvider _loggerProvider = new TestLoggerProvider();
@@ -58,6 +61,10 @@ public async Task AugmentFiles_Test()
5861
string expectedContent = FileUtility.ReadResourceString(RequirementsPsd1PSGalleryOnlineResourceFileName);
5962
Assert.Equal(expectedContent, requirementsContent, StringComparer.OrdinalIgnoreCase);
6063

64+
string profileContent = File.ReadAllText(Path.Combine(_scriptRootPath, ProfilePs1FileName));
65+
string expectedProfileContent = FileUtility.ReadResourceString(ProfileFileResourceFileName);
66+
Assert.Equal(expectedProfileContent, profileContent, StringComparer.OrdinalIgnoreCase);
67+
6168
ValidateLogs(_loggerProvider, _scriptRootPath);
6269
}
6370

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Azure Functions profile.ps1
2+
#
3+
# This profile.ps1 will get executed every "cold start" of your Function App.
4+
# "cold start" occurs when:
5+
#
6+
# * A Function App starts up for the very first time
7+
# * A Function App starts up after being de-allocated due to inactivity
8+
#
9+
# You can define helper functions, run commands, or specify environment variables
10+
# NOTE: any variables defined that are not environment variables will get reset after the first execution
11+
12+
# Authenticate with Azure PowerShell using MSI.
13+
# Remove this if you are not planning on using MSI or Azure PowerShell.
14+
if ($env:MSI_SECRET) {
15+
Disable-AzContextAutosave -Scope Process | Out-Null
16+
Connect-AzAccount -Identity
17+
}
18+
19+
# Uncomment the next line to enable legacy AzureRm alias in Azure PowerShell.
20+
# Enable-AzureRmAlias
21+
22+
# You can also define functions or aliases that can be referenced in any of your PowerShell functions.

test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@
109109
<None Remove="Resources\FileProvisioning\PowerShell\PSGallerySampleFeed.xml" />
110110
<None Remove="Resources\FileProvisioning\PowerShell\requirements_PSGalleryOffline.psd1" />
111111
<None Remove="Resources\FileProvisioning\PowerShell\requirements_PSGalleryOnline.psd1" />
112+
<None Remove="Resources\FileProvisioning\PowerShell\profile.ps1" />
112113
</ItemGroup>
113114

114115
<ItemGroup>
115116
<EmbeddedResource Include="Resources\FileProvisioning\PowerShell\PSGallerySampleFeed.xml" />
116117
<EmbeddedResource Include="Resources\FileProvisioning\PowerShell\PSGalleryEmptyFeed.xml" />
117118
<EmbeddedResource Include="Resources\FileProvisioning\PowerShell\requirements_PSGalleryOffline.psd1" />
118-
<EmbeddedResource Include="Resources\FileProvisioning\PowerShell\requirements_PSGalleryOnline.psd1">
119+
<EmbeddedResource Include="Resources\FileProvisioning\PowerShell\requirements_PSGalleryOnline.psd1"/>
120+
<EmbeddedResource Include="Resources\FileProvisioning\PowerShell\profile.ps1">
119121
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
120122
</EmbeddedResource>
121123
</ItemGroup>

0 commit comments

Comments
 (0)