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.
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..
@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:
NVIT
@slightwv... Thanks for that. I was looking on my Android mobile, which shows just VB Script. On desktop its fine. Weird.
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"
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.
Open in new window