从VBS中的列表中设置IP地址

时间:2013-10-09 13:38:21

标签: networking file-io vbscript setup-deployment static-ip-address

好的,所以我正在使用Fog成像一堆PC。我的老板是手动设置所有东西的坚持者,他甚至不会让我使用打印服务器来管理打印机......

无论如何,总而言之,我已经通过编写一堆打印机安装脚本和其他一些小型监控和其他脚本取得了很大进展。

所以它现在归结为设置IP地址,通常必须设置为静态而没有正常的AD \ DHCP ezmode。

所以我希望得到一些帮助,这个新剧本我弗兰克斯特在一起。

此脚本“应该”

  1. 读取它运行的主机名(工作!)
  2. 在网络共享上打开Computerlist.csv(主要工作)
  3. 解析ComputerList.csv,寻找主机名(通常是作品,但情况敏感)
  4. 获取为主机名列出的信息,并设置变量(拉动时为Looses。)
  5. 使用这些变量配置网络连接。 (由于上述#4有问题)
  6. 我真的很惊讶我无法谷歌搜索已经构建过的脚本。

    到目前为止,这是我拼凑在一起的东西,它看起来非常接近,但我错过了一些错误,我无法解决这个问题。

    option explicit
    
    Dim WshShell
    Dim ObjShell
    Dim objSysInfo
    Dim strComputerName
    Dim strFile
    
    Set WshShell = WScript.CreateObject("WScript.Shell")
    If WScript.Arguments.length = 0 Then
    Set ObjShell = CreateObject("Shell.Application")
    ObjShell.ShellExecute "wscript.exe", """" & _
    WScript.ScriptFullName & """" &_
    " RunAsAdministrator", , "runas", 1
    Else
    end if 
    
    '* Pulls Computer name and sets it to variable
    Set objSysInfo = CreateObject( "WinNTSystemInfo" )
    strComputerName = objSysInfo.ComputerName
    
    '* Loop through CSV file, read entries, store them in an array
    dim CONNECTION : set CONNECTION = CreateObject("ADODB.CONNECTION")
    dim RECORDSET : set RECORDSET = CreateObject("ADODB.RECORDSET")
    
    CONNECTION.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\carmichaels\e\PCSetup\IPchanger\;Extended Properties=""text;HDR=YES;FMT=Delimited"""
    
    RECORDSET.Open "SELECT * FROM ComputerList.csv WHERE ComputerName = '" & strComputerName & "'", CONNECTION, 3, 3
    
    ' //// For testing \\\\
    WScript.Echo RECORDSET.Source
    ' \\\\ For testing ////
    
    if RECORDSET.EOF then
        WScript.Echo "Record not found"
        WScript.Quit
    else
        dim strIPAddress : strIPAddress = RECORDSET("IPAddress") & ""
        dim strSubnetMask : strSubnetMask = RECORDSET("SubnetMask") & ""
        dim strGateway : strGateway = RECORDSET("Gateway") & ""
        dim intGatewayMetric : intGatewayMetric = 1
        dim strDns1 : strDns1 = RECORDSET("Dns1") & ""
        dim strDns2 : strDns2 = RECORDSET("Dns2") & ""
        dim strDns3 : strDns3 = RECORDSET("Dns3") & ""
        WScript.Echo strIPAddress
    end if
    
    '* Set IP address information stored in variables
    Set objShell = WScript.CreateObject("Wscript.Shell")
    objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
    objShell.Run "netsh interface ip set dns name=""Local Area Connection"" static "& strDns1, 0, True
    objShell.Run "netsh interface ip add dns name=""Local Area Connection"" addr="& strDns2, 0, True
    objShell.Run "netsh interface ip add dns name=""Local Area Connection"" addr="& strDns3, 0, True
    Set objShell = Nothing
    

    我的问题是当我运行这个脚本时,它声称第28行chr 1无法打开文件(在32位机器上)。

    在64位计算机上,(我在.bat [%windir%\ SysWoW64 \ cscript \ server \ share \ folder \ folder \ IPchanger.vbs]中运行以下内容)它贯穿但IP地址缺少点。恩。 10.1.0.57在我的测试窗口中显示为10.1057,并且无法再次运行声称文件已打开或锁定。

    这是CSV文件

    ComputerName,IPAddress,SubnetMask,Gateway,Dns1,Dns2,Dns3
    CLONE2,10.1.0.57,255.255.255.0,10.1.0.1,10.1.0.18,10.1.0.13,10.1.0.12
    

1 个答案:

答案 0 :(得分:0)

不知道你的文件打开错误,对我来说第28行是

Set objShell = WScript.CreateObject("Wscript.Shell")

但是为了避免点的问题,使用Jet OLEDB文本过滤器,您需要为csv定义Schema.ini文件。见http://msdn.microsoft.com/en-us/library/windows/desktop/ms709353%28v=vs.85%29.aspx

我认为Jet会将您的IP设为十进制数字,而不是文本。