Skip to content

Commit 58e356c

Browse files
authored
Preload assemblies on PS5 (#21780)
1 parent ac480cc commit 58e356c

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
-->
2222
## Upcoming Release
2323
* Added new switch parameter `OSImageScheduledEventEnabled` and string parameter `OSImageScheduledEventNotBeforeTimeoutInMinutes` to the cmdlets `New-AzVmssConfig` and `Update-AzVmss`.
24+
* Fixed an issue that `Add-AzVhd` throws `FileNotFoundException` on Windows PowerShell. [#21321]
2425

2526
## Version 5.7.1
2627
* Added a breaking change warning to the `Get-AzVM` cmdlet to show that the `NextLink` parameter and parameter set will be removed in June 2023. The parameter has been non-functional for a long time.

tools/AzureRM.Example.psm1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function Test-DotNet
1515
{
1616
if ((Get-PSDrive 'HKLM' -ErrorAction Ignore) -and (-not (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' -ErrorAction Stop | Get-ItemPropertyValue -ErrorAction Stop -Name Release | Where-Object { $_ -ge 461808 })))
1717
{
18-
throw ".NET Framework versions lower than 4.7.2 are not supported in Az. Please upgrade to .NET Framework 4.7.2 or higher."
18+
throw ".NET Framework versions lower than 4.7.2 are not supported in Az. Please upgrade to .NET Framework 4.7.2 or higher."
1919
}
2020
}
2121
catch [System.Management.Automation.DriveNotFoundException]
@@ -66,6 +66,13 @@ if (Test-Path -Path "$PSScriptRoot\StartupScripts" -ErrorAction Ignore)
6666
}
6767
}
6868

69+
# [windows powershell] preload assemblies
70+
%PRELOAD-ASSEMBLY%
71+
72+
# [windows powershell] preload module alc assemblies
73+
$preloadPath = (Join-Path $PSScriptRoot -ChildPath "ModuleAlcAssemblies")
74+
Preload-Assembly -AssemblyDirectory $preloadPath
75+
6976
if (Get-Module %AZORAZURERM%.profile -ErrorAction Ignore)
7077
{
7178
Write-Warning ("%AZORAZURERM%.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. " +

tools/UpdateModules.psm1

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,30 @@ function New-ModulePsm1 {
9292
}
9393
}
9494

95+
# Scripts to preload dependency assemblies on Windows PowerShell
96+
# https://stackoverflow.com/a/60068470
97+
$preloadAssemblies = ""
98+
$isAzAccounts = $file.BaseName -ieq 'Az.Accounts'
99+
if ($isAzAccounts) {
100+
$preloadAssemblies += 'if ($PSEdition -eq "Desktop") {
101+
[Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::GetAssemblies().Values | ForEach-Object {
102+
$path = $$_.Item1
103+
try {
104+
Add-Type -Path $path -ErrorAction Ignore | Out-Null
105+
}
106+
catch {
107+
Write-Verbose "Could not preload $path"
108+
}
109+
}
110+
}'
111+
}
112+
95113
# Grab the template and replace with information.
96114
$template = Get-Content -Path $TemplatePath
97115
$template = $template -replace "%MODULE-NAME%", $file.BaseName
98116
$template = $template -replace "%DATE%", [string](Get-Date)
99117
$template = $template -replace "%IMPORTED-DEPENDENCIES%", $importedModules
118+
$template = $template -replace "%PRELOAD-ASSEMBLY%", $preloadAssemblies
100119

101120
#Az.Storage is using Azure.Core, so need to check PS version
102121
if ($IsNetcore)
@@ -107,7 +126,7 @@ function New-ModulePsm1 {
107126
}
108127
elseif($file.BaseName -ieq 'Az.Accounts')
109128
{
110-
$template = $template -replace "%AZURECOREPREREQUISITE%",
129+
$template = $template -replace "%AZURECOREPREREQUISITE%",
111130
@"
112131
if (%ISAZMODULE% -and (`$PSEdition -eq 'Core'))
113132
{
@@ -124,7 +143,7 @@ if (%ISAZMODULE% -and (`$PSEdition -eq 'Core'))
124143
}
125144
else
126145
{
127-
$template = $template -replace "%AZURECOREPREREQUISITE%",
146+
$template = $template -replace "%AZURECOREPREREQUISITE%",
128147
@"
129148
if (%ISAZMODULE% -and (`$PSEdition -eq 'Core'))
130149
{
@@ -191,7 +210,7 @@ function Get-Cmdlets {
191210
$nestedModules = $ModuleMetadata.NestedModules
192211
$cmdlets = @()
193212
foreach ($module in $nestedModules) {
194-
if('.dll' -ne [System.IO.Path]::GetExtension($module))
213+
if('.dll' -ne [System.IO.Path]::GetExtension($module))
195214
{
196215
continue;
197216
}

0 commit comments

Comments
 (0)