如何访问Azure Runbook存储?PowerShell脚本路径及FTP集成问题
解决Azure自动化Runbook中的文件存储与FTP集成问题
一、Runbook中可用的存储路径
1. 临时存储(沙盒本地)
Azure自动化Runbook运行在沙盒环境中,$env:TEMP/$env:TMP是系统分配的临时目录,Runbook运行期间对该目录有读写权限。之前使用无效大概率是路径拼接错误,正确用法需避免硬编码分隔符,用Join-Path生成合法路径:
# 获取临时目录路径 $tempDir = $env:TEMP # 拼接下载文件的完整路径 $downloadPath = Join-Path -Path $tempDir -ChildPath "your-file-name.ext" # 从REST API下载文件到临时路径 Invoke-WebRequest -Uri "https://your-api-url/file" -OutFile $downloadPath
可添加验证步骤确认路径可用性:
# 输出临时目录路径并检查是否存在 Write-Output "临时目录路径:$tempDir" Test-Path $tempDir
2. 持久化存储(Azure存储账户文件共享)
如果需要文件在Runbook结束后保留,或沙盒临时存储容量不足,建议使用Azure存储账户的文件共享:
# 导入Azure存储模块(若未导入) Import-Module Az.Storage # 用托管身份连接存储账户 $storageAccountName = "your-storage-account" $fileShareName = "your-file-share" $ctx = New-AzStorageContext -StorageAccountName $storageAccountName -UseConnectedAccount # 下载文件到临时目录,再上传至文件共享 $downloadPath = Join-Path $env:TEMP "your-file.ext" Invoke-WebRequest -Uri "https://your-api-url/file" -OutFile $downloadPath Set-AzStorageFileContent -Context $ctx -ShareName $fileShareName -Source $downloadPath -Path "remote-file.ext"
二、集成FTP上传操作
直接在Runbook中用PowerShell完成FTP上传,以下是两种常用方法:
方法1:使用WebClient
$ftpServer = "ftp://your-ftp-server.com" $ftpUsername = "ftp-user" $ftpPassword = ConvertTo-SecureString "ftp-pass" -AsPlainText -Force $ftpCredential = New-Object System.Management.Automation.PSCredential ($ftpUsername, $ftpPassword) $webClient = New-Object System.Net.WebClient $webClient.Credentials = $ftpCredential # 上传临时目录中的文件 $uploadPath = "$ftpServer/remote-folder/your-file.ext" $webClient.UploadFile($uploadPath, $downloadPath) $webClient.Dispose()
方法2:使用FtpWebRequest(更灵活)
$ftpUri = New-Object System.Uri("ftp://your-ftp-server.com/remote-folder/your-file.ext") $ftpRequest = [System.Net.FtpWebRequest]::Create($ftpUri) $ftpRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile $ftpRequest.Credentials = New-Object System.Net.NetworkCredential("ftp-user", "ftp-pass") # 若需SSL连接,启用以下配置 # $ftpRequest.EnableSsl = $true # 读取本地文件并上传 $fileStream = [System.IO.File]::OpenRead($downloadPath) $ftpStream = $ftpRequest.GetRequestStream() $fileStream.CopyTo($ftpStream) $ftpStream.Close() $fileStream.Close()
三、完整整合脚本
将API下载、临时存储、FTP上传整合为一个Runbook脚本:
# 1. 定义参数 $apiUrl = "https://your-api-endpoint/download" $ftpServer = "ftp://your-ftp-server.com/target-folder" $ftpUser = "your-ftp-username" $ftpPass = "your-ftp-password" $fileName = "exported-data.csv" # 2. 获取临时目录并拼接文件路径 $tempDir = $env:TEMP $localFilePath = Join-Path $tempDir $fileName # 3. 从API下载文件 try { Invoke-WebRequest -Uri $apiUrl -OutFile $localFilePath -ErrorAction Stop Write-Output "文件成功下载到:$localFilePath" } catch { Write-Error "下载失败:$_" exit 1 } # 4. 上传至FTP try { $webClient = New-Object System.Net.WebClient $webClient.Credentials = New-Object System.Net.NetworkCredential($ftpUser, $ftpPass) $ftpUploadUrl = "$ftpServer/$fileName" $webClient.UploadFile($ftpUploadUrl, $localFilePath) Write-Output "文件成功上传到FTP:$ftpUploadUrl" } catch { Write-Error "FTP上传失败:$_" exit 1 } finally { # 清理临时文件(可选) if (Test-Path $localFilePath) { Remove-Item $localFilePath -Force } $webClient.Dispose() }
注意事项:
- 若FTP服务器需SSL连接,需在代码中启用SSL配置
- 生产环境中避免硬编码密码,建议将FTP凭据存储在Azure自动化凭据资产中,通过
Get-AzAutomationCredential调用
内容的提问来源于stack exchange,提问作者Jeremy Reynolds
相关产品推荐
相关产品推荐

