Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

vbscript - Looking to Rename Computers with csv file and .vbs script

I would love to do this in Powershell but that isn't an option. I have a bunch of computers I'm looking to rename and am trying to automate the process as much as I can. I'd like to have a csv file setup with two columns (oldname,newname) and be able to pull that info into a vbs script to rename the computers automatically.

The code to rename an individual computer is:

Name = "wantedcomputername"
Password = "localadminpassword"
Username = "localadminusername"

Set objWMIService = GetObject("Winmgmts:rootcimv2")

'Call always gets only one Win32_ComputerSystem object.
For Each objComputer In objWMIService.InstancesOf("Win32_ComputerSystem")

    Return = objComputer.rename(Name,Password,Username)
    If Return <> 0 Then
        WScript.Echo "Rename failed. Error = " & Err.Number
    Else
        WScript.Echo "Rename succeeded." &
            "Reboot for new name to have effect"
    End If
Next

I don't want to force a restart of the machine after the name is changed either. A restart will be done but I can't include one in the rename. I don't know enough about coding to go about pulling info from a csv file but I'd appreciate any help or feedback.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Opening the CSV as a database is arguably the most elegant way of going about this:

filename = "C:path	oyour.csv"

Set csv = CreateObject("Scripting.FileSystemObject").GetFile(filename)

Set conn = CreateObject("ADODB.Connection")
Set rs   = CreateObject("ADODB.Recordset")

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & csv.ParentFolder.Path & ";" & _
          "Extended Properties=""text;HDR=YES;FMT=Delimited"""

rs.Open "SELECT * FROM [" & csv.Name & "]", conn

Do Until rs.EOF
  WScript.Echo rs.Fields("ComputerName").Value
  rs.MoveNext
Loop

rs.Close
conn.Close

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...