You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Automation Runbook中如何通过账号密钥挂载Azure文件共享?

Azure Automation Runbook挂载Azure文件共享失败的问题与解决

问题描述

尝试在Azure Automation的PowerShell Runbook中通过用户名和密钥挂载Azure文件共享(Azure FS),本地Windows PowerShell中运行正常的代码,在Runbook(测试PowerShell 5.1和7.1版本)执行时触发多种错误:

本地可正常运行的代码

$UserName = "localhost\trex4xfs"
$Key = "Zav---mykey-----1Tdw=="
$RemotePath = "\\myshare.file.core.windows.net\mainfs"
$MapDrive = "z:"

Get-Command -Name *SmbMapping* | ft

[securestring]$pass = ConvertTo-SecureString $key -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $pass

New-PSDrive -Name T -PsProvider FileSystem -Root $RemotePath -Credential $credential
Get-PSDrive | ft

Write-Host "--------now import smb"
Import-Module smbshare
Write-Host "--------now smb"
New-SmbMapping -LocalPath $MapDrive -RemotePath $RemotePath -UserName $UserName -Password $Key

Runbook中出现的错误列表

  • 使用New-PSDrive报错:This function is not supported on this system
  • 使用New-SmbMapping(PowerShell 7.1)报错:The 'New-SmbMapping' command was found in the module 'SmbShare', but the module could not be loaded. For more information, run 'Import-Module SmbShare'
  • 使用New-SmbMapping(PowerShell 5.1)报错:Cannot connect to CIM server. The specified service does not exist as an installed service.
  • 使用Import-Module smbshare报错:Failed to generate proxies for remote module 'smbshare'. Cannot overwrite the item C:\Users\Client\Temp\tmp_5t22mi1k.oh0\remoteIpMoProxy_smbshare_2.0.0.0_localhost_f29e4e95-e8cf-4256-a4db-fc9381c6563c.format.ps1xml with itself.
  • 使用New-CimSession报错:The specified service does not exist as an installed service.

问题根源在于Azure Automation Runbook的沙箱环境不支持CIM服务,而SMB挂载相关的命令(如New-SmbMapping、SmbShare模块)均依赖CIM,同时New-PSDrive的FileSystem提供者在该环境也无法使用。

解决方案

Azure Automation Runbook不支持SMB挂载操作,正确方式是使用Az.Storage模块直接操作Azure文件共享,无需挂载驱动器。

替代实现代码

# 导入Az.Storage模块(需确保Automation账户已添加该模块)
Import-Module Az.Storage

# 配置存储账户信息
$storageAccountName = "myshare"
$fileShareName = "mainfs"
$storageAccountKey = "Zav---mykey-----1Tdw=="

# 创建存储上下文
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

# 示例1:列出文件共享内的文件
Get-AzStorageFile -ShareName $fileShareName -Context $storageContext | Select-Object Name, Length, LastModified

# 示例2:下载文件到Runbook临时目录
$localTempFile = Join-Path $env:TEMP "target-file.txt"
Get-AzStorageFileContent -ShareName $fileShareName -Path "source/path/file.txt" -Destination $localTempFile -Context $storageContext

# 示例3:上传文件到文件共享
Set-AzStorageFileContent -ShareName $fileShareName -Source $localTempFile -Path "upload/path/new-file.txt" -Context $storageContext

关键注意事项

  1. 确保Azure Automation账户已导入Az.Storage模块,可在Automation账户的「模块」页面搜索添加。
  2. 存储账户密钥等敏感信息建议存入Automation的凭据资产,通过Get-AzAutomationCredential安全获取,避免硬编码。
  3. Runbook沙箱环境不支持SMB、CIM相关操作,所有文件共享操作需通过Az模块完成。

内容的提问来源于stack exchange,提问作者Tilo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 08:31:13