Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Netdom Help 2

Status
Not open for further replies.

sotghalz

Technical User
Apr 11, 2007
57
US
Good Afternoon,

Is there a way to read from two separate text files (or one) and have them populate "sOldCname" and "sNewCname". Then run WSHShell.Run in a "For Each In"?

Currently, I have a InputBox for "sOldCname" and "sNewCname" but, I don’t want to input for over 800 workstations.

Any help would be appreciated?

Code:
Dim sNewCname
Dim sOldCname
Dim dUserName

Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")

[COLOR=red]sOldCname = InputBox("Enter computers current name","Windows 2k3 Netdom Rename Script")
sNewCname = InputBox("Enter computers new name","Windows 2k3 Netdom Rename Script")[/color]

dUserName = "username"
Call WSHShell.Run("cmd.exe /K netdom renamecomputer " & sOldCname & " /newname:" & sNewCname & _
	" /userd:swab-n\" & dUserName &" /passwordd:* /usero:swab-n\" & dUserName &" /passwordo:* /force /reboot:10")
 
Better to have 1 file with comma separated values (csv) that you can whip up in excel really quick. Then, just use the "split" method to break up the components.

Code:
Do While Not oTSInput.AtEndOfStream
	[red]aNames = Split(oTSInput.ReadLine, ",")[/red]
	iResult = oShell.Run("<path>\netdom renamecomputer " & [red]aNames(0)[/red] & _
		" /newname:" & [red]aNames(1)[/red] & " /userd:swab-n\" & sUser & _
		" /passwordd:" & sPwd & " /usero:swab-n\" & dUser & _
		" /passwordo:" & sPwd & " /force /reboot:10", , True)
Loop


Also... see this FAQ on popping up a password box so that you don't have to save your credentials in the script or retype them 800 times... faq329-6910

PSC
[&mdash;] CCNP[sub][blue]x3[/blue][/sub] (Security/R&S/Wireless) [&bull;] MCITP: Enterprise Admin [&bull;] MCSE [&mdash;]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --from "Hackers
 
Good Morning PSC,

If I understand this correctly...

I can add the code and a csv file with the old and new computer names in one cell separated by a comma:
Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
Set oTSInput = oFSO.OpenTextFile("computer.csv")
RemotePC = Split(oTSInput.ReadAll, vbNewLine)
oTSInput.Close

Do While Not oTSInput.AtEndOfStream
    RemotePC = Split(oTSInput.ReadLine, ",")
    [COLOR=red]iResult[/color] = oShell.Run("cmd.exe /K netdom renamecomputer " & RemotePC(0) & _
        " /newname:" & RemotePC(1) & " /userd:swab-n\" & sUserName & _
        " /passwordd:" & sPassword & " /usero:swab-n\" & dUser & _
        " /passwordo:" & sPassword & " /force /reboot:10", , True)
Loop
at the end of the code you provided for the password. It should work?

Also, is the iResult supposed to be for a output file?
 
One correction for you...

Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
Set oTSInput = oFSO.OpenTextFile("computer.csv")
[green]' Line not needed.  Loop is going to read each line one by one.[/green]
[s]RemotePC = Split(oTSInput.ReadAll, vbNewLine)[/s]
oTSInput.Close

Do While Not oTSInput.AtEndOfStream
    RemotePC = Split(oTSInput.ReadLine, ",")
    iResult = oShell.Run("cmd.exe /K netdom renamecomputer " & RemotePC(0) & _
        " /newname:" & RemotePC(1) & " /userd:swab-n\" & sUserName & _
        " /passwordd:" & sPassword & " /usero:swab-n\" & dUser & _
        " /passwordo:" & sPassword & " /force /reboot:10", , True)
Loop

I suggest Excel, because you can put your old computer names in one column and the new computer names in a second column. This gives you the chance to look at the data side by side and do a sanity check. When you save, you choose to save the file in CSV format, which gives you the data you need in a format that is convenient for scripting.

This script assumes that your input file has only 2 comma separated fields per line in the form of oldcomputername,newcomputername. When you do a split, you get an array of strings. So under the previously listed assupmtion, you should get oldcomputername in array element 0 and newcomputername in array element 1. This is useful equivalency in terms of what you are trying to accomplish here.

The "Run" method of the Shell object returns the exit code of the application you are running. So iResult is that exit code. Doing it this way is simply my preference. iResult can sometimes be useful when debugging the script.

The key, I suppose, is that scripting isn't the only tool in your toolbox. Normalizing data isn't a pretty business and you have to pay close attention. Excel is an excellent tool to know. You can use it to scrutinize data that could impact 1000s of machines (You're already planning to hit 800). One bad screw up with a script could be an RGE (resume generating event).

And if I can impart no other wisdom, heed this: TEST, TEST, TEST. Try on a handful of machines first before going crazy.

PSC
[&mdash;] CCNP[sub][blue]x3[/blue][/sub] (Security/R&S/Wireless) [&bull;] MCITP: Enterprise Admin [&bull;] MCSE [&mdash;]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --from "Hackers
 
Good Morning PSC,

I finally got the script to run. There was a little tweaking that I had to perform but it works great.

I removed
Code:
oTSInput.Close
it made the next line fail. (oTSInput not Set)

I also removed
Code:
set WSHShell = wscript.createObject("wscript.shell")
because it was already used in your password script.

Note: When I run the script it opens the netdom command in its own cmd window and lets me know that it has completed but, I have to type in “exit” for it to continue to the next computer in the csv file.

Second note: The HTML prompt does not come to the foreground.

Thanks for your help.

Here is what I have:
Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTSInput = oFSO.OpenTextFile("<path>\computer.csv")

Do While Not oTSInput.AtEndOfStream
    RemotePC = Split(oTSInput.ReadLine, ",")
    iResult = oShell.Run("cmd.exe /K netdom renamecomputer " & RemotePC(0) & " /newname:" & RemotePC(1) & _
    	" /userd:swab-n\" & sUserName &" /passwordd:" & sPassword &" /usero:swab-n\" & sUserName &" /passwordo:" & sPassword &" /force /reboot:10", , True)
Loop
 
>I have to type in "exit" for it to continue to the next computer in the csv file.
If you want it to close automatically, use switch /c
[tt] iResult = oShell.Run("cmd.exe /[highlight]c[/highlight] ...[/tt]
 
1) instead of running cmd.exe, just run netdom directly. You don't need a separate CMD process. (BTW... If any of your target PCs are XP, you will need to put netdom on the network somewhere and refer to it's UNC path.)
2) I've had the same problem with window activation. It seems to be hit or miss. I'm not sure what to tell you on that.

PSC
[&mdash;] CCNP[sub][blue]x3[/blue][/sub] (Security/R&S/Wireless) [&bull;] MCITP: Enterprise Admin [&bull;] MCSE [&mdash;]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --from "Hackers
 
tsuji,

Thanks that worked.

PSC,

Thanks for the heads up. Fortunately, we have all our XP machines in one OU.

Do you know how to output the results to a text file? Errors and successes.
 
output the results to a text file
A starting point:
iResult = oShell.Run("cmd.exe /[!]C[/!] netdom renamecomputer " & RemotePC(0) & " /newname:" & RemotePC(1) & _
" /userd:swab-n\" & sUserName &" /passwordd:" & sPassword &" /usero:swab-n\" & sUserName &" /passwordo:" & sPassword &" /force /reboot:10 [!]>>\path\to\textfile.txt[/!]", , True)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,

Thanks for the reminder. I was still thinking vbscript and not DOS. I should have figured that out because I have used it before. Thanks again for everybody’s help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top