我正在尝试安装以下 powershell 模块以通过 AzureDevOps 管道压缩带有密码的文件
Install-Module -Name 7Zip4Powershell -Repository PSGallery -Force
但是在运行以下命令时它不起作用,它没有将压缩文件放在路径下
Compress-7Zip -Path $SASUri_FilePath -ArchiveFileName "$scriptPath\$SASUri_FileName.zip" -Format SevenZip -Password $key
错误:
System.IO.FileNotFoundException:找不到文件 | '/agent/_work/1/s/Runbook/Scripts/stblog_20240415_SAS.zip'。文件 |名称:'/agent/_work/1/s/Runbook/Scripts/stblog_20240415_SAS.zip'
|在Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo,字符串路径, |布尔 isDirError) at
有人可以帮忙吗?
我同意@Miao Tian-MSFT,它适用于Windows托管代理,如果您使用的是自托管代理,请检查该文件是否存在于您的powershell代码中的路径中,然后运行您的脚本,如下所示:-
$fileFullPath = "C:\agent\_work\1\s\Runbook\Scripts\stblog_20240415_SAS.zip"
if (Test-Path $fileFullPath) {
Write-Host "File exists at: $fileFullPath"
} else {
Write-Host "File does not exist at: $fileFullPath"
exit 1
}
Write-Host "SASUri_FilePath: $SASUri_FilePath"
Write-Host "scriptPath: $scriptPath"
if (-not (Test-Path $SASUri_FilePath)) {
Write-Host "SASUri_FilePath is not pointing to a valid location."
exit 1
}
if (-not (Test-Path $scriptPath)) {
Write-Host "scriptPath is not pointing to a valid location."
exit 1
}
Write-Host "Before compression, SASUri_FilePath: $SASUri_FilePath"
Write-Host "Before compression, scriptPath: $scriptPath"
Compress-7Zip -Path $fileFullPath -ArchiveFileName "$scriptPath\$SASUri_FileName.zip" -Format SevenZip -Password $key
如果您没有使用自托管代理,并且您的 zip 文件位于存储库中或作为工件下载,请使用预定义变量指定正确的文件路径。
我的Azure Devops yaml代码:-
trigger:
- master
pool:
vmImage: 'windows-latest'
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
Install-Module -Name 7Zip4Powershell -Repository PSGallery -Force
$SASUri_FilePath = "$(System.DefaultWorkingDirectory)/temp.zip"
$scriptPath = "$(System.DefaultWorkingDirectory)/output"
$SASUri_FileName = "temp"
$key = "silicon"
if (-not (Test-Path $SASUri_FilePath)) {
Write-Host "File does not exist at: $SASUri_FilePath"
exit 1 }
Write-Host "SASUri_FilePath: $SASUri_FilePath"
Write-Host "scriptPath: $scriptPath"
Compress-7Zip -Path $SASUri_FilePath -ArchiveFileName "$scriptPath\$SASUri_FileName.zip" -Format SevenZip -Password $key
输出:-