Hmm. I’m not entirely sure a regex is completely the right approach. What you probably need to do is just go through the file line-by-line. When you see a line matching a header, your code would know what to do with the subsequent lines. Because INI file sections don’t have a closing delimiter for a section, you can’t easily grab just the contents of a section using a regex. I mean, each section but the last is “terminated” by the start of the following section… but I think you’re going to work a lot harder that way.
For example, there’s a C# class at An INI file handling class using C# - CodeProject you might use as a procedural example. You’ll notice that it uses some native-code DLLs from Windows to actually deal with the INI.
INI files are really a PITA, though. If you’ve got any control over how those are created, consider using something like XML instead. It’s a lot easier to parse. INI is pretty 1980’s tech.
I didnt think of using an xml file for the ini part.I just want it to be easy to edit so that whoever uses the script can open the file and enter the services they want the script to monitor and backup software being used. What is the easiest way to build an xml configuration file?
Or write two scripts. Better yet, write them as functions, as put them in a module. The first is the one that reads the XML and does whatever. The second accepts input on parameters, and produces an XML file. E.g.,
Thanks for another fast response. I am trying to work through this now after finding out I should use [xml](gc $File) instead of import-clixml, it is going quite well. Thanks for the help Don I have both of your learn windows powershell books and they are fantastic. Do you plan to do any more soon?
We’ll probably do updates for v4 on the existing books early next year. It’s a ton of work, and it doesn’t pay much, so I can’t afford to do many more than I do.
This may be more complicated than it’s worth but YAML is a great format since it’s machine readable like XML and easily human readable/editable. Great for config files.