Avatar of jskfan
jskfan
Flag for Cyprus asked on

replace a string on a text file on a remote computer

replace a string on a text file on a remote computer

Currently, I have to type in \\RemotecomputerName\c$ I will be prompted to type  in RemotecomputerName\administrator  and password.
then open a text file (it has extension of CMD). the file has few lines on it , but the string  I want to replace is called username and comes after -username (notice the dash before username).

example:
aaaaaa bbbbbbbb ccccccccc dddddddddd -username username
then Save the file.

I need this operation to be done by a script

Thank you
VB ScriptWindows BatchProgramming Languages-OtherScripting LanguagesPowershell

Avatar of undefined
Last Comment
jskfan

8/22/2022 - Mon
NVIT

Does this help?
set origstr=aaaaaa bbbbbbbb ccccccccc dddddddddd -username olduser
set rmvstr=%origstr:*-username=%
call set rslt=%origstr:%rmvstr%=%
echo %rslt% newuser

Open in new window

jskfan

ASKER
Can you explain you script please. ?

I have a list of computers. for instance Excel spreadsheet or text file:

computername      newusername
Comp1                  UserA
Comp2                  UserB
...
...

Open in new window


the script should access comp1  , It s access this way:  comp1\administrator then type in Password
then on text file name Mytext.com  there is a line that literally contains -username username
as you can notice the first username has dash.
the script should replace the second username with UserA
then it closes the text file and save it

it will jump to second computer Comp2 and do the same thing..
slightwv (䄆 Netminder)

Should be pretty easy in Powershell.

Replacing text in a file is straight forward:
https://mcpmag.com/articles/2018/08/08/replace-text-with-powershell.aspx

I don't have a network where I can test anything on remote computers but scripts seem to exist:
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Replace-String-58fbfa85
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
NVIT

@jskfan... My solution would be a CMD script. I see you've changed your topic tags. I thought it originally included Windows Batch/Script. Now, it just shows VB Script.

If you would still like a CMD solution, let me know. Otherwise, please wait for someone to come in with a VB Script solution.
slightwv (䄆 Netminder)

I see Batch is still there:
Untitled.jpg
NVIT

@slightwv... Thanks for that. I was looking on my Android mobile, which shows just VB Script. On desktop its fine. Weird.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
slightwv (䄆 Netminder)

Don't use Mobile but see if there is a "+" looking icon next to VB Script to expand the topics.
NVIT

No + icon next to VB Script on my android. Shucks! Thanks again.
jskfan

ASKER
NVIT

Any Script that bring a solutions is welcomed
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
slightwv (䄆 Netminder)

Personally, I would stay away from old BAT scripting.  IMO, it should have gone away in the 90's early 00's.  Much better options out there these days that are much easier to work with.

Did you look at the Powershell link I posted?
NVIT

@jsk... Please clarify your objective and steps. It's not clear.

Your original question says: but the string  I want to replace is called username and comes after -username (notice the dash before username).
aaaaaa bbbbbbbb ccccccccc dddddddddd -username username

Your next post makes things more confusing. Sorry, it's probably just me but I'm having trouble getting the overall goal and steps you need.

Need clarification, please
slightwv (䄆 Netminder)

>>Need clarification, please

Seems straight forward to me.

Parse a file getting the computer name and new password:
Get Comp1 then open a file looking for "some string -username xxx" then replace "xxx" with  "UserA"
next line in file, go to Comp2  replace string after -username "www" with "UserB"
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jskfan

ASKER
oBdA


can you please explain (by commenting the lines of the script), before I test it ?

Thank you.

By the way this Powershell command worked, but just on one local computer:
 ((Get-Content -path C:\ReplaceDemo.txt -Raw) -replace 'brown','white') | Set-Content -Path C:\ReplaceDemo.txt
oBdA

PowerShell is usually pretty self-explanatory, so what exactly is unclear?
The script reads a csv with computernames and new usernames, creates a network drive to the remote computer using the credentials you entered, reads the text file, replaces the string found, writes the text file, and removes the network drive again.
The RegEx pattern (-username\s+)\S+ matches a literal "-username", followed by any number of whitespaces (\s+), followed by any number of non-whitespace (\S+) (the actual username to replace).
$content -replace $pattern, "`$1$($newUser)"
The literal -username and its following spaces will be in match group 1, and be put back in -replace with the $1 ($x a special token in a RegEx that refers to matching group x.
jskfan

ASKER
I will come back to this topic later..

Thank you Guys!!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck