1

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. '

1
  • You are using the command Select-String wrong. You can not use -Path for a string. In your case the code needs to look like that: "Value | Select-String -Pattern "Pattern". Additionaly you should specify what property you want to use of the of $lastblob. Otherwise it will always use the datatype (Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob) of the object and not the value. Commented Sep 29, 2022 at 6:14

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.