none
Having trouble with -replace for multiple if statements RRS feed

  • Question

  • I know this should be super easy, I cannot for the life of me get this to work!

    $path = "C:\new folder\New folder\server.ini"
    $replace1 = "`nWiaAutoconnect=1`r`nHideTwainScanners=1`r`ntransfermode=1"
    $replace2 = "myiplocal=0`nWiaAutoconnect=1`r`nHideTwainScanners=1`r`ntransfermode=1"
    
    $gfile = get-content $path
    
    Foreach($line in $gfile) {
    
    if($line -match "transfermode=0"){
    $line = $line -replace "transfermode=0","$Replace1"
    }
    
    elseif($line -match "transfermode=1"){
    $line = $line -replace "transfermode=1","$Replace1"
    }
    
    elseif($line -notmatch "transfermode=0" -or "transfermode=1"){
    $line = $line -replace "myiplocal=0","$Replace2"
    }
     
    else {write-host "fail"}
    
    
     }
    EDIT: When I run this no changes are made and when I output $line it returns the last line of the INI. 
    EDIT: It would probably help if I used set-content as well.



    • Edited by MSmith7295 Wednesday, November 4, 2015 1:27 PM
    Wednesday, November 4, 2015 12:51 PM

Answers

  • This is one way to replace lines in a file.  Once you understand then it will be easier.

    $lines=Get-Content  "C:\new folder\New folder\server.ini"

    $new=$lines -replace'transfermode=1','transfermode=2'
    $new | Out-File newini.ini




    \_(ツ)_/


    • Edited by jrv Wednesday, November 4, 2015 4:35 PM
    • Marked as answer by MSmith7295 Friday, November 6, 2015 3:57 PM
    Wednesday, November 4, 2015 3:13 PM

All replies

  • This is one way to replace lines in a file.  Once you understand then it will be easier.

    $lines=Get-Content  "C:\new folder\New folder\server.ini"

    $new=$lines -replace'transfermode=1','transfermode=2'
    $new | Out-File newini.ini




    \_(ツ)_/


    • Edited by jrv Wednesday, November 4, 2015 4:35 PM
    • Marked as answer by MSmith7295 Friday, November 6, 2015 3:57 PM
    Wednesday, November 4, 2015 3:13 PM
  • It would be easier to use the INI file editor API:

    https://gallery.technet.microsoft.com/Edit-old-fashioned-INI-f8fbc067

    WritePrivateProfileString 'section1,'transfermode1','2','c:\system.ini'

    This will either add or replace directly without manipulating the file.


    \_(ツ)_/

    Wednesday, November 4, 2015 3:18 PM
  • Typo jrv ;)

    Cheers,

    Ruud
    Twitter:    Blog: www.ruudborst.nl  LinkedIn:    
    Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Wednesday, November 4, 2015 3:19 PM
  • Thanks.  That is what I get for compositing of screen copies.


    \_(ツ)_/

    Wednesday, November 4, 2015 4:35 PM
  • I am having trouble with running the file against multiple conditions to see if what it should input. Based on the script above, is there a reason why it isn't changing values?
    Wednesday, November 4, 2015 5:58 PM
  • Just use the profile write as it is much easier and more reliable.

    To do multiple replaces:

    $lines=Get-Content  "C:\new folder\New folder\server.ini"

    $new=$lines -replace'transfermode=1','transfermode=2'
    $new=$new-replace'transfermode=1','transfermode=2'
    $new=$new-replace'transfermode=1','transfermode=2'
    $new | Out-File newini.ini



    \_(ツ)_/

    Wednesday, November 4, 2015 7:51 PM
  • Sorry if I am not being clear. Let me explain my dilemma further. I have an INI that is different on every users machine. There are some points of commonality. For the most part the transfermode usually equals 0. Sometimes it does already equal 1. Also, it can not exist. That's why I have the three if statements. 

    If transfermode = 0 replace with transfermode = 1 + additional configurations
    If transfermode = 1 replace with transfermode = 1 + additional configurations
    If transfermode does not exist go to myiplocal=0 and replace with myiplocal=0 + transfermode = 1 + additional configurations. 

    I don't mind using the tool as long as I can use those conditions. 

    Wednesday, November 4, 2015 7:59 PM
  • Sorry if I am not being clear. Let me explain my dilemma further. I have an INI that is different on every users machine. There are some points of commonality. For the most part the transfermode usually equals 0. Sometimes it does already equal 1. Also, it can not exist. That's why I have the three if statements. 

    If transfermode = 0 replace with transfermode = 1 + additional configurations
    If transfermode = 1 replace with transfermode = 1 + additional configurations
    If transfermode does not exist go to myiplocal=0 and replace with myiplocal=0 + transfermode = 1 + additional configurations. 

    I don't mind using the tool as long as I can use those conditions. 


    Which is why we use profile commands.  The writePrivate can replace or create an entry.  Just use it an it will simplify this. YOu cannot rely on string parsing in an ini file.

    \_(ツ)_/

    Wednesday, November 4, 2015 8:14 PM
  • This worked for me; thanks jrv. I would try the other method. I am unfamiliar with how to use the tool. 
    Friday, November 6, 2015 3:57 PM