0

静的 IP の入力を使用して、IP 設定を静的に変更するファイルを作成する必要があります。これを行うファイルを作成することはそれほど難しくありませんが (BATCH または VBS を使用)、問題は接続の名前です。標準のウィンドウはローカル エリア接続ですが、i(for例) 接続の名前を test に変更します。また、2 つ以上の接続を持っている人もいますが、標準のものだけを変更し、他のすべてを無効にする必要があります (WIFI、ハマチなど)。これは LAN パーティーで使用され、手動の作業 (200 人以上の場合はかなりの時間がかかります) の代わりに、全員の IP アドレスを指定されたアドレスにすばやく変更します (なんらかの入力が必要です)。

ヒントや例を教えてください。

前もってありがとう、バート

4

1 に答える 1

0

私はこれを少し前に同様の目的で書きました。

少し面倒ですが、基本的には、どのネットワーク接続を変更するかをユーザーに尋ね、次に DHCP をオンにするか、手動で IP アドレスを入力するかを尋ねます。これを変更するには、ログインしたユーザーに管理者権限が必要だと思います

Option Explicit

Const SCRIPT_NAME = "Set IP"
Const SUBNET_MASK = "255.255.255.0"

Dim objWMI
Dim arrNANames
Dim colNa, objNa
Dim colNAConfig, objNAConfig
Dim strIP
Dim intIPRet
Dim intCount, strSelectString, intSelected

Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colNA = objWMI.ExecQuery("select * from Win32_NetworkAdapter")

ReDim arrNANames(colNA.Count)
intCount = 0
strSelectString = "Select a network adapter to modify:" & vbCrLf
For Each objNa In colNa
    arrNANames(intCount) = objNA.Name
    strSelectString = strSelectString & intCount & ")  " & arrNANames(intCount) & vbCrLf
    intCount = intCount + 1
Next

Do
    intSelected = inputbox(strSelectString, SCRIPT_NAME)
    If intSelected = "" Or Not IsNumeric(intSelected) Then
        quitScript
    End If
Loop Until CInt(intSelected) < UBound(arrNANames) And CInt(intSelected) > -1

Set colNA = objWMI.ExecQuery("select * from Win32_NetworkAdapter where Name='" & arrNANames(intSelected) & "'")

For Each objNA In colNA
    Set colNAConfig = objWMI.ExecQuery("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & objNA.DeviceID & "'} WHERE resultClass = win32_NetworkAdapterConfiguration ")
    For Each objNAConfig In colNAConfig
        If MsgBox("Do you want to enable automatic IP (DHCP/APIPA) for device " & chr(34) & objNa.Name & chr(34), vbQuestion+vbYesNo, SCRIPT_NAME) = vbYes Then
            intIPRet = objNAConfig.EnableDHCP
            Select Case intIPRet
                Case 0      MsgBox "DHCP enabled successfully", vbInformation, SCRIPT_NAME
                Case 1      MsgBox "DHCP enabled successfully" & vbCrLf & "Please reboot for changes to take effect", vbInformation, SCRIPT_NAME
                Case Else   MsgBox "Could not enable DHCP", vbCritical, SCRIPT_NAME
            End Select
        Else
            Do
                strIP = inputbox("Type an IP for network adapter: " & objNA.Name, SCRIPT_NAME)
                If strIP = "" Then
                    quitScript
                End If
            Loop Until isValidIP(strIP)
            intIPRet = objNAConfig.EnableStatic(Array(strIP),Array(SUBNET_MASK))
            Select Case intIPRet
                Case 0      MsgBox "IP changed to " & strIP, vbInformation, SCRIPT_NAME
                Case 1      MsgBox "IP changed to " & strIP & vbCrLf & "Please reboot for changes to take effect", vbInformation, SCRIPT_NAME
                Case Else   MsgBox "Could not change IP", vbCritical, SCRIPT_NAME
            End Select
        End If
    Next
Next

quitScript

'returns true if the parameter is a valid IP address
Function isValidIP(ip)
    Dim arrNums, intNum

    arrNums = Split(ip, ".")
    If UBound(arrNums) <> 3 Then
        isValidIP = False
        Exit Function
    End If
    For Each intNum In arrNums
        If Not IsNumeric(intNum) Then
            isValidIP = False
            Exit Function
        End If
        If intNum < 0 Or intNum > 255 Then
            isValidIP = False
            Exit Function
        End If
        If Len(intNum) > 1 And Left(intNum,1) = "0" Then
            isValidIP = False
            Exit Function
        End If
    Next
    isValidIP = True
End Function

Sub quitScript
    Set objWMI = Nothing
    Set colNa = Nothing
    WScript.Quit
End Sub
于 2013-11-11T11:34:33.363 に答える