TransWikia.com

Powershell - Is is possible to extract values from several thousand yaml files and export them to a text file

Stack Overflow Asked by Micim987 on January 27, 2021

We are trying to process several thousand user data file for a server that we run and we would like to extract everyone’s home location. The YAML is structured like this. File names are *UUID*.yml and there are well over 2000 files

lastAccountName: *name*
...
homes:
  home:
    world: *world*
    x: *x*
    y: *y*
    z: *z*
    yaw: *yaw*
    pitch: *pitch*
  home2:
    world: *world*
    x: *x*
    y: *y*
    z: *z*
    yaw: *yaw*
    pitch: *pitch*
...

If possible I would like to export these values in the most human readable format possible but I am unsure how to go about doing that and google wasn’t too helpful. I assume this would also be possible in python but I’ve never used python and an unsure how to write a python script. Sorry if this is a little vague but I’m not too familiar with Powershell and parsing YAML files

Edit: I forgot to mention the homes can have any name so it could be home or it could be notHome. It’s not limited to just home_ where _is any number. There can also be more than 1 but no more than 20

One Answer

There are a few modules that may be helpful. For the purpose of this answer I briefly tested "powershell-yaml" from the PowerShell Gallery:

It provides 2 commands

  1. ConvertFrom-Yaml
  2. ConvertTo-Yaml

You can run Get-Content on a YAML file and pipe it to ConvertFrom-Yaml:

gc C:tempSampleYaml.yml.txt | ConvertFrom-Yaml

Similar to ConvertFrom-Json I think these will convert to PSCustomObjects that you can filter manipulate normally in PowerShell. Though the only Yaml file I had on hand was converting to hash tables, that might be a side effect of the file itself.

Another module FXPSYaml seems to have similar commands. You can find both modules in the gallery.

Install-Module FSPSYaml

Or

Install-Module powershell-Yaml

Answered by Steven on January 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP