寻找使用csv文件和.vbs脚本重命名计算机 [英] Looking to Rename Computers with csv file and .vbs script

查看:396
本文介绍了寻找使用csv文件和.vbs脚本重命名计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Powershell做这个,但这不是一个选择。我有一堆电脑,我正在寻找重命名,我试图尽可能自动化的过程。我想有一个csv文件设置与两列(oldname,newname),并能够将该信息到一个vbs脚本自动重命名计算机。

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:root\cimv2")

    '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

我不想在更改名称后强制重新启动计算机。将重新启动将完成,但我不能包括一个在重命名。我不知道足够的编码,去从csv文件中提取信息,但我会感谢任何帮助或反馈。

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.

推荐答案

打开CSV作为数据库可能是最优雅的方式:

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

filename = "C:\path\to\your.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

这篇关于寻找使用csv文件和.vbs脚本重命名计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆