Replace text in TSM option file

Hi, I have been trying to run the following script but it wont accept the text I want to replace:

EXCLUDE.DIR “C:\REMOVED”

(Get-Content C:\testext.txt) |
Foreach-Object {$_ -replace “EXCLUDE.DIR “C:\REMOVED””, “REMOVED”} |
Set-Content C:\testext2.txt

 

It works fine on most other text in the file if it’s like the following:

Foreach-Object {$_ -replace “OLD”, “NEW”} |

 

Any ideas how I stop it from thinking the 2 quotation marks in the find field are the end of the script?

Any help greatley appreciated.

Joe

It’s a regular expression search/replace, so you need to make sure you escape the right characters to get the results you want.

'Exclude.Dir "C:\Removed"' -replace 'Exclude\.dir \"C:\\Removed\"','Removed'

In the above example, I escaped the dot in the filename, the quotes, and the backslash itself (which is an escape character).

Thank you, you are a star.

Ive tried escaping the text before & failed, but I spotted you swapped the " for a ’ and it worked first time.

Thanks again :slight_smile:

I’m glad that resolved the issue for you Joe. :slight_smile:

Oh, one more thing. Â Turns out I didn’t need to escape the double quotes. Â I should have shown you this method:

$stringToReplace = 'Exclude.dir "C:\Removed"'
'Exclude.dir "C:\Removed"' -replace ([regex]::Escape($stringToReplace)), 'Removed'

 

This uses the power of the regex Escape static method to do the escaping for you.

Thanks for that, although I cant seem to get the regex method to work, it replaces the entire text of the document with Removed.

As you can probably guess im kinda new to Powershell at the moment.

Im having a nightmare at the moment with this.

So far, I have managed to get all the info out of all the options files on the servers listed in tsmlist.txt

But I now need to do stage to, which is modify the TXM option file on the servers as listed in the txt file & then restart the TSM Client Scheduler (to reload the option file)

 

This is the script I have bodged together, unsurprisingly it’s not working.

 

$servers = get-content c:\tsmlist.txt
foreach ($server in $servers){
$file = get-content "\$server\c$\testext.txt) |
Foreach-Object {$_ -replace ‘EXCLUDE.DIR “C:\Documents and Settings”’, “”} |
Set-Content C:\testext2.txt}
Restart-Service TSM Client Scheduler

 

Any idea where im going wrong?

Thanks

Joe

Sorry for the delayed reply…it was a long weekend here in Canada.

You should probably do some of this work step by step either using a debugger (PowerShell comes with one) or interactively in the console so that you can see what is going on in each line of your script. For example, assuming your first file was \foo\c$\testext.txt, you could do this:
$contents = Get-Content \foo\c$\testext.txt`
Once you have the contents read in, verify the type and the contents meet your expectations.
$contents.GetType()
$contents

Now that you see the contents, try to replace your text, see if that also meets your expectations, like this:
# Note, you can use replace on an array, and you don’t need to specify the replacement text if you are just removing it.
$newContents = $contents -replace ‘EXCLUDE.DIR “C:\Documents and Settings”’
$newContents

If that is what you want written to the new file, then see if you can do that next:
Out-File -FilePath C:\testext2.txt -InputObject $newContents
notepad C:\testext2.txt

Now, in writing this I changed a few things in your logic. First, I set it up to read the entire file and store it in an array. Then I applied replace to the array to remove the unwanted string (you can replace on an array and it will work on every item in the array). Then I added logic to write the new contents to a new file.

Give some of these things a try and let me know how it works out.

The Restart-Service command you’re executing is running against the local computer, not the one you’re modifying.

Try the following to run the command on the remote server(s)
<pre class=“lang:ps decode:true”>Restart-Service -InputObject $(Get-Service -ComputerName $server -Name “TSM Client Scheduler”)
 

Thanks for the replys to this, I went away on Honeymoon just before the latest replys and forgot about this until today when my boss asked if it was done yet.

Time to get back on it, thanks again for all the help.

It works!

This is what I have eneded up with and it seems to work perfect:

 

$servers = get-content c:\joe\tsmlist.txt
foreach ($server in $servers){
$file = get-content “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt” |
foreach-object {$_ -replace ‘EXCLUDE.DIR “C:\Documents and Settings”’, “”} |
Set-Content “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt”
Restart-Service -InputObject $(get-service -ComputerName $server -Name “TSM Client Scheduler”)
}

Excellent! Thanks for letting us know, Joe.

No problem, thanks for all the help.

Ive actually updated my script as it wouldnt overwrite the dsm.opt file, I also added $server so I could see which server it is on if it fails & added a copy-item line so I have a copy of the original file incase it goes wrong (boss asked for that bit)

As soon as I get a change request authorised I will be running this on about 300 servers, dont think I will run it on them all at once, maybe in chunks for peace of mind.

$servers = get-content c:\joe\tsmlist.txt
foreach ($server in $servers){
$server
$file = copy-item “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt” “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm-OLD.opt”
$file = get-content “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm-OLD.opt” |
foreach-object {$_ -replace ‘EXCLUDE.DIR “C:\Documents and Settings”’, “”} |
set-content “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt”
Restart-Service -InputObject $(get-service -ComputerName $server -Name “TSM Client Scheduler”)
}

Another upate, I realised that if I ran the script more than once I would loose the backuo copy I created.
This isnt great beacuse I have to go back and put it all right again if anything fails, so ive added the date & time to the copy.
However that created another problem, I cant tell power shell to open a file that I dont have the name for so this is what I ended up with:

$servers = get-content c:\joe\tsmlist.txt
$Date = (Get-Date -format “dd-MM-yyyy_HH-mm-ss”)
foreach ($server in $servers){
$server
$file = copy-item “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt” “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm-$Date.opt”
$file = copy-item “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt” “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm-OLD.opt”
$file = get-content “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm-OLD.opt” |
foreach-object {$_ -replace ‘EXCLUDE.DIR “C:\Documents and Settings”’, “”} |
set-content “\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt”
Restart-Service -InputObject $(get-service -ComputerName $server -Name “TSM Client Scheduler”)
}

Hi Joe,

You shouldn’t need that extra “old” file. Here’s your script with a minor tweak to remove the “middle-man”:

$servers = get-content c:\joe\tsmlist.txt

$Date = (Get-Date -format “dd-MM-yyyy_HH-mm-ss”)

foreach ($server in $servers){

    $server

    $file = copy-item “\\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt” “\\$server\c$\Program Files\Tivoli\TSM\baclient\dsm-$Date.opt” -PassThru

    get-content $file.FullName |
foreach-object {$_ -replace 'EXCLUDE\.DIR “C:\\Documents and Settings”', ''} |
set-content “\\$server\c$\Program Files\Tivoli\TSM\baclient\dsm.opt”

    Restart-Service -InputObject $(get-service -ComputerName $server -Name “TSM Client Scheduler”)

}