Author Posts

April 6, 2015 at 7:18 am

I wanted to find out if you can help me with a script that will edit .ini files, and update the server name. We have about 1700 ini files. That need to have new print server names since the old server is are going to be decommissioned soon.

The filed that needs to be updated is the Host value. Ie if the Host value is Host = “KBFILE01” it should be update to Host = “vKBprt01”

Before Script:

Printer=SMB2 Local Name="Windows Printer | LB-28-11" Host="KBFILE01" Name="LB-28-11" PrinterID="HP LaserJet 4100 Series PCL6" Class="PCL5" Enabled=yes

I need it to be like :

Printer=SMB2 Local Name="Windows Printer | LB-28-11" Host="vKBprt01" Name="LB-28-11" PrinterID="HP LaserJet 4100 Series PCL6" Class="PCL5" Enabled=yes

Thanks

Quick Reply

April 6, 2015 at 7:21 am

You'll have to rename the file to have a TXT filename extension if you want to upload it.

But...

Get-Content old.ini |
ForEach { $_ -replace 'Host="KBFIE01"','Host="vKBprt01"' } |
Out-Bile new.ini

Is basically what you're after, right?

April 6, 2015 at 8:52 am

Thanks for the quick response. Can I put conditions using this script. i.e if 'Host="KBFIE01"then replace with 'Host="vKBprt01' or if Host="LBFIE01" then replace with Host="vLBprt01"

April 6, 2015 at 8:56 am

Well, you don't really need to.

Get-Content old.ini |
ForEach { 
  $_ -replace 'Host="KBFIE01"','Host="vKBprt01"' 
  $_ -replace 'Host="LBFIE01"','Host="vLBprt01"' 
} |
Out-Bile new.ini

You don't need "conditions." It's not going to replace KBFIE01 with vLPprt01 under any circumstances. It'll only replace what's there.

April 6, 2015 at 10:00 am

Hey Mikhael,

If it's just the printer server name that's changing in the INI file, is there an option for you to update the DNS entry in AD for each old printer server name, and point them to the new server instead?

April 6, 2015 at 10:13 am

Don,

Thanks four reply that should work with what I am trying to accomplish.

April 6, 2015 at 10:15 am

Tim,

Thanks for your response. We are trying not edit or update the DNS entries at this point.

April 6, 2015 at 10:58 am

Don,

I ran the script that you provided. It is making duplicate entris to the file instead of just replacing the value. I have uploaded both the orignial file (tests.ini) and also the new.file (new.ini)

April 6, 2015 at 11:02 am

Don,

Sorry here we go. I have renamed the files to .txt

I ran the script that you provided. It is making duplicate entris to the file instead of just replacing the value. I have uploaded both the orignial file (tests.ini) and also the new.file (new.ini)

April 6, 2015 at 1:29 pm

Hi All,

I was finally able to get the replace function to work using the script below.

(get-content \\vfs04\Users$\kaendm\Desktop\test\tests.ini) | foreach-object {$_ -replace "QMFILE01", "vKBprt01"} | set-content \\vfs04\Users$\kaendm\Desktop\test\tests.ini

**But I am having problems when I try to run this script on two or more ini files. (I have 1500 ini files that i am running this script against) Using the script below. It is appending everyitning to into one file.

(get-content \\vfs04\Users$\kaendm\Desktop\test\*.ini) | foreach-object {$_ -replace "QMFILE01", "vKBprt01"} | set-content \\vfs04\Users$\kaendm\Desktop\test\*.ini

April 6, 2015 at 2:04 pm

The original command I posted was designed to handle one file; you never mentioned more than one. It's logic won't accommodate that – you didn't mention that you had 1500 files. Yes, it's appending everything into one file because that's exactly what it's supposed to do.

I don't want to just do your job for you – I want to help you understand the technology. You see how you've changed Get-Content to get MULTIPLE files? It gets them one line at a time – so every line, from every file, is being funneled into the -replace loop. The results are going to a single file – there's no point where the input filename is being preserved and used to create a separate output. I want to make sure you can see that, so that we can work through the problem toward a solution.

April 6, 2015 at 4:42 pm

Thank you very much for the update Don. I'm very sorry If I didn't make myself clear. I thought I had mentioned i in the original question above. I am new to Powershell and I really appreciate your help. I want to be able to run the script against multiple .ini files.If you can point in the right direction. I will really appreciate it.

Thanks

April 7, 2015 at 3:47 am

It's probably easier to switch more to a script approach for clarity. Anytime you have multiples of something, you can expect to have to enumerate them using a ForEach. In your case, you have multiple FILES, which each contain multiple LINES. This is something I cover pretty well in [i]Learn PowerShell in a Month of Lunches[/i], and it would be a good place for you to learn this more formally.

$files = Get-ChildItem -Path c:\myfiles\*.ini
foreach ($file in $files) {
  $newfilename = $file.fullname -replace ".ini","-2.ini"
  Get-Content $file.fullname |
  ForEach { 
    $_ -replace 'Host="KBFIE01"','Host="vKBprt01"' 
    $_ -replace 'Host="LBFIE01"','Host="vLBprt01"' 
  } |
  Out-File $newfilename
}

Make sure you understand what that's doing before you use it – I'm not in your environment, so I can't test this, and it's going to be difficult for me to troubleshoot it for you. So it's important that you understand what each piece is doing (and ask, if you don't) before you use this.

April 7, 2015 at 6:16 am

Don, Thank you very much for the update and for the information. I will do more testing as you have suggested before using it. Once again thank you.

April 7, 2015 at 6:56 am

An update on the script. The script is working fine on multiple files.The only thing that i have noticed is that when when I have two [b]replace lines [/b]it as below it is doubling up everything in the ini file. Instead of replacing the host value the script is creating another entry see below.

$_ -replace 'Host="KBFIE01"','Host="vKBprt01"'
$_ -replace 'Host="LBFIE01"','Host="vLBprt01"'
|

Instead of replacing 'Host="KBFIE01"' with 'Host="vKBprt01"'

It is adding the two entries below:

Host="vKBprt01"
Host="kbfile01"

April 7, 2015 at 7:07 am

Oh, yeah. That's gonna happen with the way the logic works now. It's basically outputting each line of the file twice into the destination.

Fragment:

  Get-Content $file.fullname |
  ForEach { 
    $_.Replace('Host="KBFIE01"','Host="vKBprt01"').Replace('Host="LBFIE01"','Host="vLBprt01"')
  } |
  Out-File $newfilename

That should generate only one output string. Now, if your list of stuff to replace is huge, that's going to get massively awkward really quickly. But if it's only these two values, maybe a couple more, then it shouldn't be a huge problem. Annoyingly, .NET doesn't have a PHP-like array-based string replacement, or you could make that a lot more elegant.