Quản lý các mạng Windows dùng script - Phần 2: Hoàn chỉnh script

9 2 0
Quản lý các mạng Windows dùng script - Phần 2: Hoàn chỉnh script

Đang tải... (xem toàn văn)

Thông tin tài liệu

Quản lý các mạng Windows dùng script - Phần 2: Hoàn chỉnh script Ở phần trước chúng ta đã biết đến một số khái niệm cơ bản về kỹ thuật scripting như đối tượng (object), phương thức (method), thuộc tính (property) và viết ra một scritp đơn giản thay đổi địa chỉ IP gán cho bộ điều hợp mạng. Sau đó, chúng ta đã sử dụng bốn script đầu tiên, được gọi là ChangeIPAddress.vbs: strComputer = . arrIPAddress = Array(172.16.11.99) arrSubnetMask = Array(255.255.255.0) Set objWMIService = GetObject(winmgmts:\ & strComputer & ootcimv2) Set colNetAdapters = objWMIService.ExecQuery(Select * from Win32_NetworkAdapterConfiguration) For Each...

Quản lý mạng Windows dùng script - Phần 2: Hoàn chỉnh script Ở phần trước biết đến số khái niệm kỹ thuật scripting đối tượng (object), phương thức (method), thuộc tính (property) viết scritp đơn giản thay đổi địa IP gán cho điều hợp mạng Sau đó, sử dụng bốn script đầu tiên, gọi ChangeIPAddress.vbs: strComputer = "." arrIPAddress = Array("172.16.11.99") arrSubnetMask = Array("255.255.255.0") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration") For Each objNetAdapter in colNetAdapters errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Next Khi chạy script server Windows, thay đổi thành cơng địa IP máy từ 45 sang 99 (Kiểm tra lệnh ipconfig trước sau chạy script) Kết hoàn toàn tốt Nhưng script xây dựng lại mức đơn giản Còn thiếu nhiều yếu tố quan trọng khác định nghĩa biến, điều khiển lỗi, dùng liệu đầu vào xác nhận liệu đầu cần phải bổ sung vào để có script tương đối hồn chỉnh Chúng ta thực điều phần hai Các định nghĩa biến Việc cần làm để xếp gọn gàng script định nghĩa biến dùng VBScript cho phép định nghĩa ngầm biến đơn giản cách dùng câu lệnh, tốt bạn khai báo tường minh chúng bắt đầu script Khai báo biến nói cho VBScript biết tồn để cấp phát nhớ lưu trữ Vì khai báo biến tường minh lại hay hơn? Ví dụ script dài, bạn thường phạm phải hay số lỗi gõ phím nhầm Và gõ nhầm tên biến, script bạn không chạy Nếu khai báo biến tường đầu script biến khai báo ngầm sau script (có thể nguyên nhân gây lỗi gõ nhầm) tạo lỗi runtime Các thơng báo lỗi giúp bạn xác định vị trí nhầm lẫn gỡ lỗi cho script bạn Để VBScript biết bạn khai báo tường minh tất biến script, thêm lệnh sau vào đầu script: Option Explicit Nếu thêm lệnh vào đầu script ChangeIPAddress.vbs chạy từ Command Prompt, kết là: C:\Documents and Settings\Administrator.DC-1\Desktop>ChangeIPAddress.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved C:\Documents and Settings\Administrator.DC-1\Desktop\ChangeIPAddress.vbs(2, 1) Microsoft VBScript runtime error: Variable is undefined: 'strComputer' Điều mà VBScript (hay chế script đăng ký Windows Script Host để chạy script VBScript) muốn nói có lỗi dịng script: strComputer = "." Vì lại xuất lỗi này? Đó gán giá trị cho biến xâu (strComputer) chưa khai báo Vì thế, cần thêm khai báo cho biến dùng script: Option Explicit Dim objWMIService Dim objNetAdapter Dim strComputer Dim arrIPAddress Dim arrSubnetMask Dim colNetAdapters Dim errEnableStatic strComputer = "." arrIPAddress = Array("172.16.11.93") arrSubnetMask = Array("255.255.255.0") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") For Each objNetAdapter in colNetAdapters errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Next Chú ý dùng Option Explicit (tuỳ chọn khai báo tường minh), bạn phải khai báo tất biến script mình, gồm đối tượng, xâu, mảng, tập hợp, biến cho đoạn mã lỗi… Nghe rắc rối phức tạp thực tế cần chạy số trang dài, chạy chương trình gỡ lỗi rungning time, bạn biết hữu ích Cũng ý khơng cần khai báo biến theo trình tự, cần nhớ khai báo cho biến trước dùng Thông thường người ta đặt tất khai báo biến vào phần riêng đầu script làm Điều khiển lỗi Bây giờ, loại bỏ lỗi gõ nhầm chạy script soát lại, script hoạt động Nhưng khơng hoạt động sao? Ví dụ, chuyện xảy thay đổi script chút để chạy máy từ xa thay máy cục mà máy từ xa khơng nằm mạng? Một lần lỗi thời gian chạy (tức lỗi xảy script thực thi, ngược lại với lỗi cú pháp mà VBScript nhận biên dịch script trước chạy nó) xuất script bị ngừng lại, hiển thị thông báo lỗi tương tự thông báo thấy Chuyện xảy viết script để thực số thao tác? Trong trường hợp tất nhiên không muốn có lỗi thời gian chạy khiến script phải dừng lại chừng mà script phải thực tất thao tác khác xây dựng Một ví dụ điển hình script giám sát thiết lập số máy tính mà khơng thay đổi thiết lập Trong trường hợp bạn cần xây dựng script chạy liên tục cho dù hay số máy bị hư hỏng khơng chạy Cách đơn giản để kiểm sốt lỗi thời gian chạy lờ chúng chúng xuất Bạn nói với VBScript thực điều cách thêm lệnh sau vào gần nơi bắt đầu script, chẳng hạn sau Option Explicit: On Error Resume Next Tất nhiên, bạn muốn thực thêm nhiều thứ khác kiểm soát lỗi Ví dụ kiểm tra tồn điều kiện lỗi thời gian chạy số điểm script (như sau kết nối tới dịch vụ WMI máy từ xa) để xác định xem liệu hoạt động script quy định để thực có thành cơng hay khơng Sau đó, dựa kết kiểm tra điều kiện lỗi, bạn định script hoạt động script Ví dụ, lỗi xuất hiện, bạn nhận thơng báo nói rằng: “Computer X not found” (khơng tìm thấy máy tính X) sau script tiếp tục chạy Chúng ta tìm hiểu sâu kiểm sốt lỗi số khác, bạn cần thêm lệnh vào để bỏ qua lỗi thời gian chạy xuất Dữ liệu đầu vào người dùng Sẽ phải làm muốn mô tả địa IP cho máy chạy script thay viết mã cho vào script dạng 172.16.11.99? Trong trường hợp cần chỉnh sửa script phép cung cấp liệu vào người dùng chạy Thực cách thêm vào tham số chạy script từ dịng lệnh Ví dụ gõ ChangeIPAddress.vbs 172.16.11.188 thay đổi địa IP điều hợp mạng thành 172.16.11.188… Chúng ta thực sau: Option Explicit On Error Resume Next Dim objWMIService Dim objNetAdapter Dim strComputer Dim strAddress Dim arrIPAddress Dim arrSubnetMask Dim colNetAdapters Dim errEnableStatic If WScript.Arguments.Count = Then Wscript.Echo "Usage: ChangeIPAddress.vbs new_IP_address" WScript.Quit End If strComputer = "." strAddress = Wscript.Arguments.Item(0) arrIPAddress = Array(strAddress) arrSubnetMask = Array("255.255.255.0") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") For Each objNetAdapter in colNetAdapters errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Next Chúng ta phân tích phần Đầu tiên khai báo biến mới: Dim strAddress Đây biến xâu (string) chứa tham số (địa IP) mô tả chạy script Tiếp theo thêm dòng bên vào sau phần khai báo: If WScript.Arguments.Count = Then Wscript.Echo "Usage: ChangeIPAddress.vbs new_IP_address" WScript.Quit End If Các dòng thực điều gì? Thuộc tính Arguments đối tượng WScript trả tập hợp tham số mô tả chạy script Phương thức Count trả số lượng tham số nhập vào mục đích phần đoạn mã kiểm tra xem liệu có quên nhập tham số (số tham số 0) khơng Nếu có, báo hiệu (hoặc hiển thị) thơng báo nói cho bạn biết cách sử dụng script phù hợp chương trình chạy script bị ngừng lại hồn tồn Cuối cùng, dòng cũ: arrIPAddress = Array("172.16.11.93") nơi viết mã cho địa IP theo kiểu gán mảng thay hai dòng bên dưới: strAddress = Wscript.Arguments.Item(0) arrIPAddress = Array(strAddress) Dòng lấy phần tử (phần tử 0) tập hợp WScript.Arguments gán trở thành biến xâu strAddress Dịng thứ hai sau lấy biến xâu strAddress gán trở thành phần tử mảng arrIPAddress Hãy xem chuyện xảy chạy script này, không mô tả tham số, sau chạy với tham số: C:\Documents and Settings\Administrator.DC-1\Desktop>ipconfig Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix : IP Address : 172.16.11.31 Subnet Mask : 255.255.255.0 Default Gateway : 172.16.11.1 C:\Documents and Settings\Administrator.DC-1\Desktop>ChangeIPAddress.vbs Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved Usage: ChangeIPAddress.vbs new_IP_address C:\Documents and Settings\Administrator.DC-1\Desktop>ChangeIPAddress.vbs 172.16.11.188 Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved C:\Documents and Settings\Administrator.DC-1\Desktop>ipconfig Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix : IP Address : 172.16.11.188 Subnet Mask : 255.255.255.0 Default Gateway : 172.16.11.1 Chương trình chạy tuyệt! Kiểm chứng đầu Nếu bạn thấy chán phải gõ lệnh ipconfig sau chạy script để kiểm tra kết quả, cách khác giúp bạn: sử dụng dòng bên dưới: For Each objNetAdapter in colNetAdapters errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Next Mục đích đoạn mã thay đổi địa IP gán cho điều hợp mạng, dùng phương thức objNetAdapter.EnableStatic Nhưng bạn nên ý cần phải có biến err- (ở errEnableStatic) dùng làm nơi lưu trữ đoạn mã lỗi trả chạy phương thức Danh sách mã lỗi trả từ phương thức EnableStatic lớp Win32_NetworkAdapterConfiguration bạn tham khảo MSDN Và từ danh sách thấy kết trả nghĩa thao tác script thực thành cơng (ví dụ địa IP điều hợp thay đổi thành công) Cách đơn giản để kiểm tra thêm dòng bên vào cuối script: Wscript.Echo errEnableStatic Chạy script lại lần nữa: C:\Documents and Settings\Administrator.DC-1\Desktop>ChangeIPAddress.vbs 172.16.11.237 Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved C:\Documents and Settings\Administrator.DC-1\Desktop>ipconfig Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix : IP Address : 172.16.11.237 Subnet Mask : 255.255.255.0 Default Gateway : 172.16.11.1 Chắc chắn đủ Kết trả cho thấy địa IP thay đổi thành công Một phương thức hay hiển thị thông báo cách thay lệnh báo hiệu lại lệnh sau: If errEnableStatic=0 Then Wscript.Echo "Adapter's IP address has been successfully changed to " & strAddress Else Wscript.Echo "Changing the adapter's address was not successful Error code " & errEnableStatic End If Thêm lệnh sau vào cuối script chạy lại hai lần, với địa IP xác với địa IP tuỳ ý: C:\Documents and Settings\Administrator.DC-1\Desktop>ChangeIPAddress.vbs 172.16.11.173 Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved Adapter's IP address has been successfully changed to 172.16.11.173 C:\Documents and Settings\Administrator.DC-1\Desktop>ChangeIPAddress.vbs 172.16.11.1492567 Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved Changing the adapter's address was not successful Error code 70 C:\Documents and Settings\Administrator.DC-1\Desktop> Kết luận Việc cuối cần làm thêm vài thích vào script để diễn giải cho script Đây ý kiến hay năm sau đọc lại muốn thay đổi điểm script, bạn dễ dàng tìm cần Đây script hoàn chỉnh cuối để thay đổi địa IP điều hợp mạng: ========================= ' NAME: ChangeIPAddress.vbs ' 'AUTHOR: Mitch Tulloch 'DATE: October 2006 ' ARGUMENTS: '1 new_IP_address '=========================Option Explicit On Error Resume Next Dim objWMIService Dim objNetAdapter Dim strComputer ' Can specify IP address or hostname or FQDN Dim strAddress 'Contains the new IP address Dim arrIPAddress Dim arrSubnetMask Dim colNetAdapters Dim errEnableStatic 'Check for missing arguments If WScript.Arguments.Count = Then Wscript.Echo "Usage: ChangeIPAddress.vbs new_IP_address" WScript.Quit End If strComputer = "." strAddress = Wscript.Arguments.Item(0) arrIPAddress = Array(strAddress) arrSubnetMask = Array("255.255.255.0") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") For Each objNetAdapter in colNetAdapters errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Next 'Display result or error code If errEnableStatic=0 Then Wscript.Echo "Adapter's IP address has been successfully changed to " & strAddress Else Wscript.Echo "Changing the adapter's address was not successful Error code " & errEnableStatic End If ... Microsoft VBScript runtime error: Variable is undefined: 'strComputer' Điều mà VBScript (hay chế script đăng ký Windows Script Host để chạy script VBScript) muốn nói có lỗi dịng script: strComputer... người dùng Sẽ phải làm muốn mô tả địa IP cho máy chạy script thay viết mã cho vào script dạng 172.16.11.99? Trong trường hợp cần chỉnh sửa script phép cung cấp liệu vào người dùng chạy Thực cách... SettingsAdministrator.DC-1Desktop> Kết luận Việc cuối cần làm thêm vài thích vào script để diễn giải cho script Đây ý kiến hay năm sau đọc lại muốn thay đổi điểm script, bạn dễ dàng tìm cần Đây script hoàn chỉnh

Ngày đăng: 11/05/2021, 03:20

Tài liệu cùng người dùng

Tài liệu liên quan