Search code examples
jsonazurepowershellcloud

Parse file and and get the value of a specific line using Powershell


I have a script that run on a list of azure VMs within a subscription and outputs a file for each VM, here is an example of its content:

Value[0]        : 
  Code          : ComponentStatus/StdOut/succeeded
  Level         : Info
  DisplayStatus : Provisioning succeeded
  Message       :     Directory: C:\variasuit_windows\HARMOR\windows_server\package


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
d-----       12/23/2023   5:10 AM                HARMOR                                                                
"Server Name","GPO_HARMOR","BIOMEDIC_AGENT","EDWARD_STATUS","SENSOR_STATUS","SENTINEL_STATUS"
"AZ-TSTAPP1D","OK","OK","OK","OK","KO"


Value[1]        : 
  Code          : ComponentStatus/StdErr/succeeded
  Level         : Info
  DisplayStatus : Provisioning succeeded
  Message       : 
Status          : Succeeded
Capacity        : 0
Count           : 0

I would like to parse the file using powershell to get the this line:

"AZ-TSTAPP1D","OK","OK","OK","OK","KO"

Then gathering this value from each file to another txt file to have something like this

"AZ-TSTAPP1D","OK","OK","OK","OK","KO"
"AZ-TSTAPP2D","OK","KO","KO","KO","KO"
"AZ-TSTAPP3D","OK","OK","OK","OK","KO"

I tried to get the content of this specific line but I found out that line number can change from a file to another
I tried this:

        $scriptResult = Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -CommandId 'RunPowerShellScript' -ScriptPath $scriptPath

        $scriptResult | Out-File $tempFile
        $contentFromFile = Get-Content -Path $tempFile
        $line12 = $contentFromFile | Select-Object -Index 11
        $line12 | Out-File $outputFile -Append
        Remove-Item $tempFile

Any help please?
Thanks


Solution

  • On a general note:

    • I don't know if Invoke-AzVMRunCommand is capable of relaying (serialized) objects, but if, so, you'd be better off operating on objects rather than on their for-display string representations.

    As for your code:

    • You don't need a temporary file and can use Out-String with the -Stream parameter to get the for-display formatted representations line-by-line; if Invoke-AzVMRunCommand only emits text anyway, you don't even need that.

    • You can use the -match operator with a regex to filter out the line of interest.

    • You can use the >> redirection operator as a shortcut to piping to Out-File -Append.

    @($scriptResult | Out-String -Stream) -match '^"AZ-TSTAPP' >> $outputFile
    

    The above regex turned out not to be specific enough; per your comment, this is the regex you came up with that worked for you:

    @($scriptResult | Out-String -Stream) -match '^".*AZ-.*",".*",".*",".*"$' >> $outputFile