Search-String Inside a Object/Variable - Powershell

Search-String Inside a Object/Variable - Powershell

I have an object called $latestBlob_content, this contains logs from azure build pipeline. I want to find a specific string from this object. But when I call - $latestBlob_content | Select-String -Pattern $pattern . This returns the whole logs. Is the 3rd line right way of reading a blob file!?

$blobs = Get-AzStorageBlob -Container $container_name -Context $context | sort @{Expression = "LastModified";Descending=$true}
$latestBlob = $blobs[0]
$latestBlob_content = $latestBlob.ICloudBlob.DownloadText()
$pattern = "Terraform_modules.azure_data_lake.*"
$latestBlob_content | Select-String -Pattern $pattern

Thanks in advance

Code those are not working - $latestBlob_content.ToString() | Select-String -Pattern "Terraform_modules.azure_data_lake.*"

7

1 Answer

Most likely, your $latestBlob_content is one string instead of an array of strings.You could check the type using $latestBlob_content.GetType().

If that is the case, you are essentially passing one string to Select-String and ask it if the string contains your pattern. It does so it returns you back the complete string.

You could try following to split up your one string into an array of strings and select only those who match your pattern

$latestBlob_content -split "`r?`n" | Select-String -Pattern $pattern

Your Answer

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

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.