使用Move-AzDataLakeGen2Item时遇Az.Storage程序集重复加载错误求助
解决Move-AzDataLakeGen2Item模块加载失败(同名程序集已加载)问题
问题描述
我尝试运行PowerShell脚本,通过Move-AzDataLakeGen2Item命令在Azure存储账户中批量移动文件,执行命令如下:
Move-AzDataLakeGen2Item -Context $context -Source $blob.Name -Destination $newBlobName
但出现错误:
Line |
28 | Move-AzDataLakeGen2Item -Context $context -Source $blob.Name …
| 已在模块'Az.Storage'中找到'Move-AzDataLakeGen2Item'命令,但无法加载该模块,原因如下:[同名程序集已加载] 如需更多信息,请运行
| 'Import-Module Az.Storage'。
已执行的操作:
- 将执行策略设置为Unrestricted
- 移除了旧版本的Az和Az.Account模块
执行Get-Module -ListAvailable Az.Storage的结果:
Directory: C:\Users\...\Documents\PowerShell\Modules ModuleType Version PreRelease Name PSEdition ExportedCommands ---------- ------- ---------- ---- --------- ---------------- Script 5.7.0 Az.Storage Core,Desk {Get-AzStorageAccount, Get-AzStorageAccountKey, New-AzStorageAccount, New-AzStorageAccountKey…}
使用VSCode运行脚本,试过Windows PowerShell和PowerShell环境,求解决思路。
解决思路
强制重新加载Az.Storage模块
运行以下命令强制卸载并重新加载模块,规避程序集冲突:# 卸载已加载的模块(忽略不存在的错误) Remove-Module Az.Storage -Force -ErrorAction SilentlyContinue # 重新导入指定版本的模块 Import-Module Az.Storage -RequiredVersion 5.7.0 -Force执行完成后再运行移动文件的命令。
清理重复的Az模块程序集
- 检查全局模块路径(
C:\Program Files\WindowsPowerShell\Modules或C:\Program Files\PowerShell\Modules),若存在其他版本的Az.Storage模块,直接删除。 - 确保用户目录下(
C:\Users\...\Documents\PowerShell\Modules)仅保留当前使用的5.7.0版本Az.Storage。
- 检查全局模块路径(
使用全新的PowerShell会话
- 关闭所有VSCode窗口和PowerShell终端,重新打开一个全新的环境(不加载之前的会话状态)。
- 先执行
Connect-AzAccount重新登录Azure,再导入Az.Storage模块,最后运行移动脚本。
更新Az模块到最新版本
当前Az.Storage 5.7.0版本较旧,可能存在已知的程序集冲突问题,更新到最新版本尝试:Update-Module Az -Force -AllowClobber更新完成后重启PowerShell会话,再执行命令。
调整模块加载优先级
运行$env:PSModulePath查看模块加载路径顺序,确保用户目录的模块路径在全局路径之前,避免加载旧版本的全局模块。若顺序错误,可临时调整:$env:PSModulePath = "$env:USERPROFILE\Documents\PowerShell\Modules;$env:ProgramFiles\PowerShell\Modules;$env:ProgramFiles\WindowsPowerShell\Modules"
内容的提问来源于stack exchange,提问作者Wouter De Raeve
相关产品推荐
相关产品推荐

