Reputation: 69
There are some files stored in Azure blob storage. I want to search a string in the latest file and work with that string later on via Powershell. Problem is - I can't directly read or search string, I have to download it first through Get-AzStorageBlobContent. I want to avoid download file. I want directly to search a string.
$storageAccountName = "resaibdplogsa2prod"
$container_name = "devops-tasks"
$context = New-AzStorageContext -StorageAccountName $storageAccountName -UseConnectedAccount
$blobs = Get-AzStorageBlob -Container $container_name -Context $context | sort @{Expression = "LastModified";Descending=$true}
$latestBlob = $blobs[0]
$getString_terraform = Select-String -Path $latestBlob -Pattern '_tool/terraform.*'
Last line giving me a obvious error ' Cannot find path 'C:\Users\jubaiaral\Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob' because it does not exist. '
Upvotes: 1
Views: 1269
Reputation: 975
I'm not sure whether what you want to achieve would work using the Az PowerShell modules.
Based on @guiwhatsthat's note on using Select-String
, you could use the REST API though. Technically it is also downloading the blob (but not storing in a file) - here is an example.
Authentication Part
This uses applicationId/secret based authentication. The corresponding Service Principal would need to have the Storage Blob Data Reader
role assigned as a minimum.
$requestMethod = 'POST'
$requestHeaders = @{
'Content-Type' = 'application/x-www-form-urlencoded'
}
$requestUri = 'https://login.microsoftonline.com/'+$tenantId+'/oauth2/v2.0/token'
$requestBody = @{
client_id = $applicationId
grant_type = 'client_credentials'
client_secret = $applicationSecret
scope = $storageAccountBlobEndpoint+'.default'
}
$request = Invoke-RestMethod -Method $requestMethod `
-Headers $requestHeaders `
-Uri $requestUri `
-Body $requestBody
Read Blob and search for string
$blobRequestMethod = 'GET'
$blobRequestHeaders = @{
'Content-Type' = 'application/xml'
Authorization = $($request.token_type + ' ' + $request.access_token)
'x-ms-date' = $(Get-Date -AsUTC -Format 'ddd, dd MMM yyyy HH:mm:ss') + ' GMT'
'x-ms-version' = '2021-04-10'
}
$blobRequestUri = $storageAccountBlobEndpoint + $containerName + '/' + $blobName
$blobRequest = Invoke-RestMethod -Method $blobRequestMethod -Headers $blobRequestHeaders -Uri $blobRequestUri
$blobRequest | Select-String -Pattern $searchString
Obviously, you would need to populate some variables before running this (values below are random examples):
$tenantId = "597d8458-6a5d-447f-875d-922e448f681a"
$subscriptionId = "3338781b-426a-4b1c-8545-d9b88d78039d"
$storageAccountBlobEndpoint = "https://<some_name>.blob.core.windows.net/"
$applicationId = "4e625cb8-595b-4b46-9ba9-6a9d220f4208"
$applicationSecret = "<some_secret_value>"
$containerName = "container01"
$blobName = "yourblob.txt"
$searchString = "find this"
I know it is not without downloading data but at least it is not writing a file. :-)
Reference: https://learn.microsoft.com/en-us/rest/api/storageservices/get-blob
Upvotes: 1