使用Runbook Power Shell解密Azure存储blob中的PGP文件

t3irkdon  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(123)

我正在尝试使用PSPGP PowerShell模块解密PGP文件。该文件存储在Azure Blob容器中。由于我对PowerShell不太熟悉,下面是我使用的脚本:

param (  
    [Parameter()]  
    [string] $storageAccount = "mystorageaccount",   
    [string] $srcBlob = "/input/file.txt.pgp",
    [string] $srcContainer = "myontainer",  
    [string] $destContainer = "myontainer",
    [string] $pgpprivatekey = "/input/PGPKeyPrivate.pgp",
    [string] $ResourceGroup = "myressourcegroup" 
      )

# Connect to Azure  

Write-Verbose -Message 'Connecting to Azure'

$AzureContext = (Connect-AzAccount -Identity).context  
Disable-AzContextAutosave -Scope Process  
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext   
$storageContext = New-AzStorageContext -StorageAccountName $storageAccount -UseConnectedAccount  

   try {
        $accKey = (Get-AzStorageAccountKey -ResourceGroupName $ResourceGroup -StorageAccountName $storageAccount).Value[0]
        $context_storageAcct = New-AzStorageContext -StorageAccountName $storageAccount -StorageAccountKey $accKey
        write-output $context_storageAcct
    }
    catch {
        $errors += [PSCustomObject]@{Item = "azcontext"; Error = $_.Exception }
        write-output "error get AZcontext"
    }

# decript PGP Files
try {
Unprotect-PGP -FilePathPrivate $srcContainer+$pgpprivatekey  -Password "mypsw" -FolderPath $srcContainer+$srcBlob -OutputFolderPath $srcContainer+"/input"
} 
    catch   
    {  
        $errmsg = $_.Exception.Message    
        Write-Output $errmsg   
    }

字符串
当我执行Runbook时,它返回以下内容:

的数据
有人能帮我修改一下剧本吗?
谢谢。

kq4fsx7k

kq4fsx7k1#

警告:Unprotect-PGP - Remove PGP encryption failed because private key not given.
我在我的环境中尝试了相同的脚本,得到了相同的错误。
x1c 0d1x的数据
发生上述错误的原因是它没有从blob存储中获取文件路径,或者可能是命令无法使用blob存储执行。
也可以将**pgpprivatekeyfile.txt.pgp下载到本地路径。
您可以指定路径运行
unprotect-pgp**命令。

命令:

try{
Unprotect-PGP -FilePathPrivate "C:\Users\xxxx\Documents\<Your-pgpprivatekey>.asc"  -Password "xxxx" -FolderPath "C:\Users\xxxx\Documents\sample.txt.pgp" -OutputFolderPath "C:\Users\v-xxxx\Documents\input"
Write-Host "The file is decrypted to outputfolderpath"
}
  catch   
    {  
        $errmsg = $_.Exception.Message    
        Write-Output $errmsg   
    }

字符串



上述命令成功解密了文件,文件被保存在输入文件夹中。
现在使用**azcopy**工具可以上传到您的容器。

azcopy copy "C:\Users\xxx\Documents\input\sample.txt" "https://venkat123.blob.core.windows.net/test?sv=2022-11-02&ss=bfqt&srt=co&sp=rwdlacupiytfx&se=2023-07-26T19:52:33Z&st=2023-07-26T11:52:33Z&spr=https&sig=xxxxxx" --recursive=true

输出:

INFO: Scanning...
INFO: Any empty folders will not be processed, because source and/or destination doesn't have full folder support
Job 0547e87xxxxxxxxxe has started
Log file is located at: C:\Users\xxx\.azcopy\054xxx.log
INFO: azcopy.exe: A newer version 10.19.0 is available to download
100.0 %, 1 Done, 0 Failed, 0 Pending, 0 Skipped, 1 Total, 2-sec Throughput (Mb/s): 0.0033

Job 054xxxxxx summary
Elapsed Time (Minutes): 0.0335
Number of File Transfers: 1
Number of Folder Property Transfers: 0
Total Number of Transfers: 1
Number of Transfers Completed: 1
Number of Transfers Failed: 0
Number of Transfers Skipped: 0
TotalBytesTransferred: 839
Final Job Status: Completed


参考号:

使用AzCopy v10将文件上载到Azure Blob存储|Microsoft Learn

相关问题