Read Hostname for the last character when single digit(1 to 9) increment it to read 2 character when it reaches double digits(10) RRS feed

  • Question

  • Hi, I am writing a script to find a replace a particular line in a properties file. Have been able to get to a point where i can get the last 2 character replaced, however my issue is the hostname from where i am reading starts with 1 and can go upto 30; so i need only the last character to be considered when the loop goes through 1-9 but should read 2 character when it reaches the 10 hostname

    Here's where i am:

    $lastTwoCharsOfComputerName = $ENV:COMPUTERNAME.Substring($ENV:COMPUTERNAME.Length - 2)
    Write-Host $lastTwoCharsOfComputerName

    select-string "C:\Users\<UserName>\Desktop\CounterID\storefront.properties" -pattern "^CounterID=" | foreach {$_.Line} | out-file -filepath C:\Users\<UserName>\Desktop\CounterID\out.txt

    Wednesday, March 21, 2018 11:35 AM

All replies

  •  Get-Content C:\Users\<UserName>\Desktop\CounterID\storefront.properties | 
         ForEach{$_ -replace '^CounterID=.*','<replacement text>' |
         out-file -c:\Users\<UserName>\Desktop\CounterID\out.txt


    Wednesday, March 21, 2018 11:43 AM
  • Thank you for your response. However the <replacement text> is not taking any variable values. The output comes with the text itself. Also the logic of incrementing the CounterID when it reaches 10 to 2 characters from the hostname is also not clear to me. Thanks for your help.
    Wednesday, March 21, 2018 12:21 PM
  • No idea what you are asking.  What counter.  I see no counters in your code.  You asked how to replace text when a line contains "CounterID" .  I showed you how to do that.  The replacement text is up to you.  Replace it with anything you want.


    Wednesday, March 21, 2018 12:45 PM