locked
Basic Find Replace Script RRS feed

  • Question

  • I'm looking for some documentation that would pretty much tell me how to find a line in a file (hostname=server_1) and replace it with the actual host name of where the file resides.  I plan on running this against 5,000+ servers; we want to use the FQDN and not the short name.  Will any of the books point me in the correct direction and if so, which one's?  We have an environment of Windows 2003, 2008, and 2012 servers.

    Thanks everyone/anyone.

    Wednesday, August 19, 2015 8:16 PM

Answers

  • Hi,

    Here's something you can play with:

    (Get-Content .\config.txt).Replace('(hostname=server_1)',"(hostname={0})" -f "$($env:COMPUTERNAME).$($env:USERDNSDOMAIN)")|
        Set-Content .\config.txt
    
    
    config.txt before script is run:
    blah1
    blah2
    (hostname=server_1)
    blah3
    
    config.txt after script is run:
    blah1
    blah2
    (hostname=hostname.domain.com)
    blah3

    This only works if you run it locally.


    EDIT: Something else to try would be adjusting the way you're getting the FQDN and using a different method (like getting the information from WMI instead of using environment variables).

    • Edited by Mike Laughlin Wednesday, August 19, 2015 8:29 PM
    • Marked as answer by jerzdevs1 Wednesday, August 19, 2015 10:28 PM
    Wednesday, August 19, 2015 8:26 PM

All replies

  • Hi,

    Here's something you can play with:

    (Get-Content .\config.txt).Replace('(hostname=server_1)',"(hostname={0})" -f "$($env:COMPUTERNAME).$($env:USERDNSDOMAIN)")|
        Set-Content .\config.txt
    
    
    config.txt before script is run:
    blah1
    blah2
    (hostname=server_1)
    blah3
    
    config.txt after script is run:
    blah1
    blah2
    (hostname=hostname.domain.com)
    blah3

    This only works if you run it locally.


    EDIT: Something else to try would be adjusting the way you're getting the FQDN and using a different method (like getting the information from WMI instead of using environment variables).

    • Edited by Mike Laughlin Wednesday, August 19, 2015 8:29 PM
    • Marked as answer by jerzdevs1 Wednesday, August 19, 2015 10:28 PM
    Wednesday, August 19, 2015 8:26 PM
  • Thanks!!!  That's nice of you.  We'll mess around with this and I'll also need to find something with WMI because not all of our servers have Powershell.  Ughhhh...  Thanks again.
    Wednesday, August 19, 2015 10:28 PM