Elegent way to set variables in .ini files












1















I'm currently trying to script the installation of Microsoft Sql Server 2012, and I've run into a problem with letting the user set options for the installation process in my powershell script



Because sql server's options are set in an .ini file, I'm not sure what the best way to edit said .ini file is. I could see myself




  • Copying the whole .ini file in powershell and setting variables within the quotes to write it out later, or

  • saving the ini file separately and searching for each line I need to set variables as a string to edit separately.


Is there any more elegant way to work with .ini files than this?
Is there any "find and replace" powershell module for files that I could use?










share|improve this question























  • Probably best to re-write the entire .ini file from an "in-script" variable that contains your template because then you can guarantee that the content will be what you need it to be (plus the values the user sets)...

    – Kinnectus
    Jul 28 '17 at 15:09











  • "Is there any "find and replace" PowerShell module for files that I could use?" INI files are just text, any of PS's standard find and replace commands would work on one. Load the file into memory, find and replace, dump back to file.

    – Ƭᴇcʜιᴇ007
    Jul 28 '17 at 15:52
















1















I'm currently trying to script the installation of Microsoft Sql Server 2012, and I've run into a problem with letting the user set options for the installation process in my powershell script



Because sql server's options are set in an .ini file, I'm not sure what the best way to edit said .ini file is. I could see myself




  • Copying the whole .ini file in powershell and setting variables within the quotes to write it out later, or

  • saving the ini file separately and searching for each line I need to set variables as a string to edit separately.


Is there any more elegant way to work with .ini files than this?
Is there any "find and replace" powershell module for files that I could use?










share|improve this question























  • Probably best to re-write the entire .ini file from an "in-script" variable that contains your template because then you can guarantee that the content will be what you need it to be (plus the values the user sets)...

    – Kinnectus
    Jul 28 '17 at 15:09











  • "Is there any "find and replace" PowerShell module for files that I could use?" INI files are just text, any of PS's standard find and replace commands would work on one. Load the file into memory, find and replace, dump back to file.

    – Ƭᴇcʜιᴇ007
    Jul 28 '17 at 15:52














1












1








1








I'm currently trying to script the installation of Microsoft Sql Server 2012, and I've run into a problem with letting the user set options for the installation process in my powershell script



Because sql server's options are set in an .ini file, I'm not sure what the best way to edit said .ini file is. I could see myself




  • Copying the whole .ini file in powershell and setting variables within the quotes to write it out later, or

  • saving the ini file separately and searching for each line I need to set variables as a string to edit separately.


Is there any more elegant way to work with .ini files than this?
Is there any "find and replace" powershell module for files that I could use?










share|improve this question














I'm currently trying to script the installation of Microsoft Sql Server 2012, and I've run into a problem with letting the user set options for the installation process in my powershell script



Because sql server's options are set in an .ini file, I'm not sure what the best way to edit said .ini file is. I could see myself




  • Copying the whole .ini file in powershell and setting variables within the quotes to write it out later, or

  • saving the ini file separately and searching for each line I need to set variables as a string to edit separately.


Is there any more elegant way to work with .ini files than this?
Is there any "find and replace" powershell module for files that I could use?







powershell sql-server-2012






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 28 '17 at 15:01









comp.sci.interncomp.sci.intern

84




84













  • Probably best to re-write the entire .ini file from an "in-script" variable that contains your template because then you can guarantee that the content will be what you need it to be (plus the values the user sets)...

    – Kinnectus
    Jul 28 '17 at 15:09











  • "Is there any "find and replace" PowerShell module for files that I could use?" INI files are just text, any of PS's standard find and replace commands would work on one. Load the file into memory, find and replace, dump back to file.

    – Ƭᴇcʜιᴇ007
    Jul 28 '17 at 15:52



















  • Probably best to re-write the entire .ini file from an "in-script" variable that contains your template because then you can guarantee that the content will be what you need it to be (plus the values the user sets)...

    – Kinnectus
    Jul 28 '17 at 15:09











  • "Is there any "find and replace" PowerShell module for files that I could use?" INI files are just text, any of PS's standard find and replace commands would work on one. Load the file into memory, find and replace, dump back to file.

    – Ƭᴇcʜιᴇ007
    Jul 28 '17 at 15:52

















Probably best to re-write the entire .ini file from an "in-script" variable that contains your template because then you can guarantee that the content will be what you need it to be (plus the values the user sets)...

– Kinnectus
Jul 28 '17 at 15:09





Probably best to re-write the entire .ini file from an "in-script" variable that contains your template because then you can guarantee that the content will be what you need it to be (plus the values the user sets)...

– Kinnectus
Jul 28 '17 at 15:09













"Is there any "find and replace" PowerShell module for files that I could use?" INI files are just text, any of PS's standard find and replace commands would work on one. Load the file into memory, find and replace, dump back to file.

– Ƭᴇcʜιᴇ007
Jul 28 '17 at 15:52





"Is there any "find and replace" PowerShell module for files that I could use?" INI files are just text, any of PS's standard find and replace commands would work on one. Load the file into memory, find and replace, dump back to file.

– Ƭᴇcʜιᴇ007
Jul 28 '17 at 15:52










2 Answers
2






active

oldest

votes


















1














Treat the .ini as a text file. Say we have the following .ini:



[section1]
var1=foo1
[section2]
var2=foo2
var3=foo3


To change the value assigned to "var2", we can do:



(get-content .test.ini).Replace('foo2','bar2') | Set-Content .test.ini


Where "bar2" is the user defined value. To incorporate the user defined value, you could do:



$ini = ".test.ini"
$userInput = Read-Host -Prompt "Enter a new value for var2"
(get-content $ini).Replace('foo2',$userInput) | Set-Content $ini


The way you choose to design how you handle the replace will rely on the data in your particular file.






share|improve this answer


























  • This is a bit too simple, what if there are multiple entries with =foo2, you need to specify the value-name as well. What if there are duplicate value-names in different sections?

    – Peter Hahndorf
    Aug 3 '17 at 9:03



















0














from https://github.com/brandoncomputer/vds



function inifile ($a,$b,$c,$d){
switch ($a){
open {
$global:inifile = $b
}
write {
$Items = New-Object System.Collections.Generic.List[System.Object]
$content = get-content $global:inifile
if ($content)
{
$Items.AddRange($content)
}
if ($Items.indexof("[$b]") -eq -1)
{
$Items.add("")
$Items.add("[$b]")
$Items.add("$c=$d")
$Items | Out-File $global:inifile
}
else
{
For ($i=$Items.indexof("[$b]")+1; $i -lt $Items.count; $i++)
{
if ($Items[$i].length -gt $c.length)
{
if ($Items[$i].substring(0,$c.length) -eq $c -and ($tgate -ne $true))
{
$Items[$i] = "$c=$d"
$tgate = $true
}
}
if ($Items[$i].length -gt 0)
{
if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
{
$i--
$Items.insert(($i),"$c=$d")
$tgate = $true
$i++
}
}
}
if ($Items.indexof("$c=$d") -eq -1)
{
$Items.add("$c=$d")
}

$Items | Out-File $global:inifile -enc ascii
}
}
}}


Usage




  1. inifile open c:tempmyini.ini

  2. inifile write header footer value


$a = $(iniread header footer)



function iniread($a,$b) {
$Items = New-Object System.Collections.Generic.List[System.Object]
$content = get-content $global:inifile
if ($content)
{
$Items.AddRange($content)
}
if ($Items.indexof("[$a]") -eq -1)
{
$return = ""
}
else
{
$return = ""
For ($i=$Items.indexof("[$a]")+1; $i -lt $Items.count; $i++)
{
if ($Items[$i].length -gt $b.length)
{
if ($Items[$i].substring(0,$b.length) -eq $b -and $gate -ne $true)
{
$return = $Items[$i].split("=")[1]
$gate = $true
}
}
if ($Items[$i].length -gt 0)
{
if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
{$gate = $true}
}
}
}return $return}





share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1235444%2felegent-way-to-set-variables-in-ini-files%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Treat the .ini as a text file. Say we have the following .ini:



    [section1]
    var1=foo1
    [section2]
    var2=foo2
    var3=foo3


    To change the value assigned to "var2", we can do:



    (get-content .test.ini).Replace('foo2','bar2') | Set-Content .test.ini


    Where "bar2" is the user defined value. To incorporate the user defined value, you could do:



    $ini = ".test.ini"
    $userInput = Read-Host -Prompt "Enter a new value for var2"
    (get-content $ini).Replace('foo2',$userInput) | Set-Content $ini


    The way you choose to design how you handle the replace will rely on the data in your particular file.






    share|improve this answer


























    • This is a bit too simple, what if there are multiple entries with =foo2, you need to specify the value-name as well. What if there are duplicate value-names in different sections?

      – Peter Hahndorf
      Aug 3 '17 at 9:03
















    1














    Treat the .ini as a text file. Say we have the following .ini:



    [section1]
    var1=foo1
    [section2]
    var2=foo2
    var3=foo3


    To change the value assigned to "var2", we can do:



    (get-content .test.ini).Replace('foo2','bar2') | Set-Content .test.ini


    Where "bar2" is the user defined value. To incorporate the user defined value, you could do:



    $ini = ".test.ini"
    $userInput = Read-Host -Prompt "Enter a new value for var2"
    (get-content $ini).Replace('foo2',$userInput) | Set-Content $ini


    The way you choose to design how you handle the replace will rely on the data in your particular file.






    share|improve this answer


























    • This is a bit too simple, what if there are multiple entries with =foo2, you need to specify the value-name as well. What if there are duplicate value-names in different sections?

      – Peter Hahndorf
      Aug 3 '17 at 9:03














    1












    1








    1







    Treat the .ini as a text file. Say we have the following .ini:



    [section1]
    var1=foo1
    [section2]
    var2=foo2
    var3=foo3


    To change the value assigned to "var2", we can do:



    (get-content .test.ini).Replace('foo2','bar2') | Set-Content .test.ini


    Where "bar2" is the user defined value. To incorporate the user defined value, you could do:



    $ini = ".test.ini"
    $userInput = Read-Host -Prompt "Enter a new value for var2"
    (get-content $ini).Replace('foo2',$userInput) | Set-Content $ini


    The way you choose to design how you handle the replace will rely on the data in your particular file.






    share|improve this answer















    Treat the .ini as a text file. Say we have the following .ini:



    [section1]
    var1=foo1
    [section2]
    var2=foo2
    var3=foo3


    To change the value assigned to "var2", we can do:



    (get-content .test.ini).Replace('foo2','bar2') | Set-Content .test.ini


    Where "bar2" is the user defined value. To incorporate the user defined value, you could do:



    $ini = ".test.ini"
    $userInput = Read-Host -Prompt "Enter a new value for var2"
    (get-content $ini).Replace('foo2',$userInput) | Set-Content $ini


    The way you choose to design how you handle the replace will rely on the data in your particular file.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 28 '17 at 15:58

























    answered Jul 28 '17 at 15:53









    rootroot

    2,36751535




    2,36751535













    • This is a bit too simple, what if there are multiple entries with =foo2, you need to specify the value-name as well. What if there are duplicate value-names in different sections?

      – Peter Hahndorf
      Aug 3 '17 at 9:03



















    • This is a bit too simple, what if there are multiple entries with =foo2, you need to specify the value-name as well. What if there are duplicate value-names in different sections?

      – Peter Hahndorf
      Aug 3 '17 at 9:03

















    This is a bit too simple, what if there are multiple entries with =foo2, you need to specify the value-name as well. What if there are duplicate value-names in different sections?

    – Peter Hahndorf
    Aug 3 '17 at 9:03





    This is a bit too simple, what if there are multiple entries with =foo2, you need to specify the value-name as well. What if there are duplicate value-names in different sections?

    – Peter Hahndorf
    Aug 3 '17 at 9:03













    0














    from https://github.com/brandoncomputer/vds



    function inifile ($a,$b,$c,$d){
    switch ($a){
    open {
    $global:inifile = $b
    }
    write {
    $Items = New-Object System.Collections.Generic.List[System.Object]
    $content = get-content $global:inifile
    if ($content)
    {
    $Items.AddRange($content)
    }
    if ($Items.indexof("[$b]") -eq -1)
    {
    $Items.add("")
    $Items.add("[$b]")
    $Items.add("$c=$d")
    $Items | Out-File $global:inifile
    }
    else
    {
    For ($i=$Items.indexof("[$b]")+1; $i -lt $Items.count; $i++)
    {
    if ($Items[$i].length -gt $c.length)
    {
    if ($Items[$i].substring(0,$c.length) -eq $c -and ($tgate -ne $true))
    {
    $Items[$i] = "$c=$d"
    $tgate = $true
    }
    }
    if ($Items[$i].length -gt 0)
    {
    if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
    {
    $i--
    $Items.insert(($i),"$c=$d")
    $tgate = $true
    $i++
    }
    }
    }
    if ($Items.indexof("$c=$d") -eq -1)
    {
    $Items.add("$c=$d")
    }

    $Items | Out-File $global:inifile -enc ascii
    }
    }
    }}


    Usage




    1. inifile open c:tempmyini.ini

    2. inifile write header footer value


    $a = $(iniread header footer)



    function iniread($a,$b) {
    $Items = New-Object System.Collections.Generic.List[System.Object]
    $content = get-content $global:inifile
    if ($content)
    {
    $Items.AddRange($content)
    }
    if ($Items.indexof("[$a]") -eq -1)
    {
    $return = ""
    }
    else
    {
    $return = ""
    For ($i=$Items.indexof("[$a]")+1; $i -lt $Items.count; $i++)
    {
    if ($Items[$i].length -gt $b.length)
    {
    if ($Items[$i].substring(0,$b.length) -eq $b -and $gate -ne $true)
    {
    $return = $Items[$i].split("=")[1]
    $gate = $true
    }
    }
    if ($Items[$i].length -gt 0)
    {
    if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
    {$gate = $true}
    }
    }
    }return $return}





    share|improve this answer






























      0














      from https://github.com/brandoncomputer/vds



      function inifile ($a,$b,$c,$d){
      switch ($a){
      open {
      $global:inifile = $b
      }
      write {
      $Items = New-Object System.Collections.Generic.List[System.Object]
      $content = get-content $global:inifile
      if ($content)
      {
      $Items.AddRange($content)
      }
      if ($Items.indexof("[$b]") -eq -1)
      {
      $Items.add("")
      $Items.add("[$b]")
      $Items.add("$c=$d")
      $Items | Out-File $global:inifile
      }
      else
      {
      For ($i=$Items.indexof("[$b]")+1; $i -lt $Items.count; $i++)
      {
      if ($Items[$i].length -gt $c.length)
      {
      if ($Items[$i].substring(0,$c.length) -eq $c -and ($tgate -ne $true))
      {
      $Items[$i] = "$c=$d"
      $tgate = $true
      }
      }
      if ($Items[$i].length -gt 0)
      {
      if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
      {
      $i--
      $Items.insert(($i),"$c=$d")
      $tgate = $true
      $i++
      }
      }
      }
      if ($Items.indexof("$c=$d") -eq -1)
      {
      $Items.add("$c=$d")
      }

      $Items | Out-File $global:inifile -enc ascii
      }
      }
      }}


      Usage




      1. inifile open c:tempmyini.ini

      2. inifile write header footer value


      $a = $(iniread header footer)



      function iniread($a,$b) {
      $Items = New-Object System.Collections.Generic.List[System.Object]
      $content = get-content $global:inifile
      if ($content)
      {
      $Items.AddRange($content)
      }
      if ($Items.indexof("[$a]") -eq -1)
      {
      $return = ""
      }
      else
      {
      $return = ""
      For ($i=$Items.indexof("[$a]")+1; $i -lt $Items.count; $i++)
      {
      if ($Items[$i].length -gt $b.length)
      {
      if ($Items[$i].substring(0,$b.length) -eq $b -and $gate -ne $true)
      {
      $return = $Items[$i].split("=")[1]
      $gate = $true
      }
      }
      if ($Items[$i].length -gt 0)
      {
      if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
      {$gate = $true}
      }
      }
      }return $return}





      share|improve this answer




























        0












        0








        0







        from https://github.com/brandoncomputer/vds



        function inifile ($a,$b,$c,$d){
        switch ($a){
        open {
        $global:inifile = $b
        }
        write {
        $Items = New-Object System.Collections.Generic.List[System.Object]
        $content = get-content $global:inifile
        if ($content)
        {
        $Items.AddRange($content)
        }
        if ($Items.indexof("[$b]") -eq -1)
        {
        $Items.add("")
        $Items.add("[$b]")
        $Items.add("$c=$d")
        $Items | Out-File $global:inifile
        }
        else
        {
        For ($i=$Items.indexof("[$b]")+1; $i -lt $Items.count; $i++)
        {
        if ($Items[$i].length -gt $c.length)
        {
        if ($Items[$i].substring(0,$c.length) -eq $c -and ($tgate -ne $true))
        {
        $Items[$i] = "$c=$d"
        $tgate = $true
        }
        }
        if ($Items[$i].length -gt 0)
        {
        if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
        {
        $i--
        $Items.insert(($i),"$c=$d")
        $tgate = $true
        $i++
        }
        }
        }
        if ($Items.indexof("$c=$d") -eq -1)
        {
        $Items.add("$c=$d")
        }

        $Items | Out-File $global:inifile -enc ascii
        }
        }
        }}


        Usage




        1. inifile open c:tempmyini.ini

        2. inifile write header footer value


        $a = $(iniread header footer)



        function iniread($a,$b) {
        $Items = New-Object System.Collections.Generic.List[System.Object]
        $content = get-content $global:inifile
        if ($content)
        {
        $Items.AddRange($content)
        }
        if ($Items.indexof("[$a]") -eq -1)
        {
        $return = ""
        }
        else
        {
        $return = ""
        For ($i=$Items.indexof("[$a]")+1; $i -lt $Items.count; $i++)
        {
        if ($Items[$i].length -gt $b.length)
        {
        if ($Items[$i].substring(0,$b.length) -eq $b -and $gate -ne $true)
        {
        $return = $Items[$i].split("=")[1]
        $gate = $true
        }
        }
        if ($Items[$i].length -gt 0)
        {
        if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
        {$gate = $true}
        }
        }
        }return $return}





        share|improve this answer















        from https://github.com/brandoncomputer/vds



        function inifile ($a,$b,$c,$d){
        switch ($a){
        open {
        $global:inifile = $b
        }
        write {
        $Items = New-Object System.Collections.Generic.List[System.Object]
        $content = get-content $global:inifile
        if ($content)
        {
        $Items.AddRange($content)
        }
        if ($Items.indexof("[$b]") -eq -1)
        {
        $Items.add("")
        $Items.add("[$b]")
        $Items.add("$c=$d")
        $Items | Out-File $global:inifile
        }
        else
        {
        For ($i=$Items.indexof("[$b]")+1; $i -lt $Items.count; $i++)
        {
        if ($Items[$i].length -gt $c.length)
        {
        if ($Items[$i].substring(0,$c.length) -eq $c -and ($tgate -ne $true))
        {
        $Items[$i] = "$c=$d"
        $tgate = $true
        }
        }
        if ($Items[$i].length -gt 0)
        {
        if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
        {
        $i--
        $Items.insert(($i),"$c=$d")
        $tgate = $true
        $i++
        }
        }
        }
        if ($Items.indexof("$c=$d") -eq -1)
        {
        $Items.add("$c=$d")
        }

        $Items | Out-File $global:inifile -enc ascii
        }
        }
        }}


        Usage




        1. inifile open c:tempmyini.ini

        2. inifile write header footer value


        $a = $(iniread header footer)



        function iniread($a,$b) {
        $Items = New-Object System.Collections.Generic.List[System.Object]
        $content = get-content $global:inifile
        if ($content)
        {
        $Items.AddRange($content)
        }
        if ($Items.indexof("[$a]") -eq -1)
        {
        $return = ""
        }
        else
        {
        $return = ""
        For ($i=$Items.indexof("[$a]")+1; $i -lt $Items.count; $i++)
        {
        if ($Items[$i].length -gt $b.length)
        {
        if ($Items[$i].substring(0,$b.length) -eq $b -and $gate -ne $true)
        {
        $return = $Items[$i].split("=")[1]
        $gate = $true
        }
        }
        if ($Items[$i].length -gt 0)
        {
        if (($Items[$i].substring(0,1) -eq "[") -and ($tgate -ne $true))
        {$gate = $true}
        }
        }
        }return $return}






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 14 at 22:43

























        answered Feb 14 at 1:58









        Brandon CunninghamBrandon Cunningham

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1235444%2felegent-way-to-set-variables-in-ini-files%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How do I know what Microsoft account the skydrive app is syncing to?

            When does type information flow backwards in C++?

            Grease: Live!