Script to change ip address on windows

前端 未结 4 1941
名媛妹妹
名媛妹妹 2020-12-02 23:56

I use my computer to communicate with a piece of hardware via ethernet. To communicate with this device I set my ip to 192 168 0 11, subnet mask to 255 255 255 0, and defaul

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 00:40

    You can use vbscript to change IP Address,

    Dim strIPAddress, strSubnetMask,strGateway, intGatewayMetric, strDns1, strDns2, objWMIService, colItems, stradaptername, objFSO
    Const ForReading = 1 
    Const ForAppending = 8
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set OutPutFile = objFSO.CreateTextFile("C:\ProgramData\test.txt" ,2 , True)
    Set InterfaceName = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionStatus >= 0")
    If objFSO.FileExists("C:\ProgramData\test.txt") Then
    Set OutPutFile = objFSO.CreateTextFile("C:\ProgramData\test1.txt" ,2 , True)
    End If
    For Each objItem in InterfaceName
    If objFSO.FileExists("C:\ProgramData\test.txt") Then
    arrInterfaces = objItem.NetConnectionID
    'wscript.echo "test: " &arrInterfaces
    Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\ProgramData\test.txt",8,true)
    ObjFileToWrite.WriteLine(arrInterfaces)
    objFileToWrite.Close
    Set objFileToWrite = Nothing
    End If
    next
    If objFSO.FileExists("C:\ProgramData\test.txt") Then
    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\ProgramData\test.txt",1)
    strFileText = objFileToRead.ReadAll()
    objFileToRead.Close
    Set objFileToRead = Nothing
    'wscript.echo "obtained" &strFileText
    End If
    Result = inputbox("Enter the AdapterName: " &vbCrLf &strFileText)
    If Result = "" then
    'wscript.echo "user selected cancel"
    Else
    strIPAddress=InputBox("Enter Static IP Adrress: ")
    strSubnetMask =InputBox("Enter SubnetMask: " )
    strGateway=InputBox("Enter Default Gateway: ")
    strDns1=InputBox("Enter Preferred DNS: ")
    strDns2=InputBox("Enter Alternate DNS: ")
    Set objShell = WScript.CreateObject("Wscript.Shell")
    objShell.CurrentDirectory = "C:\Windows\System32"
    objShell.Run "netsh interface ip set address name=""" & Result & """ static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
    objShell.Run "netsh interface ip set dns name=" & Result & " static "& strDns1, 0, True
    objShell.Run "netsh interface ip add dns name=" & Result & " addr="& strDns2, 0, True
    Set objShell = Nothing: Set obj=Nothing
    End If
    WScript.Quit
    

提交回复
热议问题