看了半天还是没太看懂,只能闹到这地步了,期待...'VBS脚本 Option Explicit Dim ICSSC_DEFAULT, CONNECTION_PUBLIC, CONNECTION_PRIVATE, CONNECTION_ALL Dim NetSharingManager 'HNetCfg.HnetShare.1 插件对象(估计是OCX插件) Dim PublicConnection, PrivateConnection Dim EveryConnectionCollectionDim objArgs Dim priv_con, publ_con Dim switchICSSC_DEFAULT = 0 CONNECTION_PUBLIC = 0 '公有连接 CONNECTION_PRIVATE = 1 '私有连接 CONNECTION_ALL = 2 '全部连接 '主方法入口 Sub Main() '获取脚本参数对象 Set objArgs = Wscript.Arguments If objArgs.Count = 3 Then priv_con = objArgs(0) publ_con = objArgs(1) switch = objArgs(2) If Initialize() = True Then '如果已经初始化过 NetSharingManager 对象 '那么,获取连接对象; GetConnectionObjects FirewallTestByName priv_con, publ_con End If Else Dim szMsg If Initialize() = True Then GetConnectionObjects FirewallTestByName "list", "list" End If 'szMsg 为字符串信息; szMsg = "To share your internet connection, please provide the name of the private and public connections as the argument." & vbCrLf & vbCrLf & _ "Usage:" & vbCrLf & _ " " & Wscript.scriptname & " " & Chr(34) & "Private Connection Name" & Chr(34) & " " & Chr(34) & "Public Connection Name" & Chr(34) Wscript.Echo (szMsg & vbCrLf & vbCrLf) End If End Sub '通过名称测试防火墙 Sub FirewallTestByName(con1, con2) On Error Resume Next
Dim Item Dim EveryConnection Dim objNCProps Dim szMsg Dim bFound1, bFound2 Wscript.Echo (vbCrLf & vbCrLf)
bFound1 = False bFound2 = False
'遍历所有的连接集合 For Each Item In EveryConnectionCollection 'EveryConnection 对象(网络共享配置接口,由网络连接对象中获得) Set EveryConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item) 'objNCProps 为网络连接属性对象; Set objNCProps = NetSharingManager.NetConnectionProps(Item) szMsg = "Name: " & objNCProps.Name & vbCrLf & _ "Guid: " & objNCProps.GUID & vbCrLf & _ "DeviceName: " & objNCProps.DeviceName & vbCrLf & _ "Status: " & objNCProps.Status & vbCrLf & _ "MediaType: " & objNCProps.MediaType 'EveryConnection 连接对象是否启用共享; If EveryConnection.SharingEnabled Then szMsg = szMsg & vbCrLf & _ "SharingEnabled" & vbCrLf & _ "SharingType: " & ConvertConnectionTypeToString(EveryConnection.SharingConnectionType) End If If objNCProps.Name = con1 Then bFound1 = True If EveryConnection.SharingEnabled = False And switch = "on" Then szMsg = szMsg & vbCrLf & "Not Shared... Enabling private connection share..." Wscript.Echo (szMsg) EveryConnection.EnableSharing CONNECTION_PRIVATE szMsg = " Shared!" ElseIf (switch = "off") Then szMsg = szMsg & vbCrLf & "Shared... DisEnabling private connection share..." Wscript.Echo (szMsg) EveryConnection.EnableSharing CONNECTION_ALL End If End If If objNCProps.Name = con2 Then bFound2 = True If EveryConnection.SharingEnabled = False And switch = "on" Then szMsg = szMsg & vbCrLf & "Not Shared... Enabling public connection share..." Wscript.Echo (szMsg) EveryConnection.EnableSharing CONNECTION_PUBLIC szMsg = " Shared!" ElseIf (switch = "off") Then szMsg = szMsg & vbCrLf & "Shared... DisEnabling public connection share..." Wscript.Echo (szMsg) EveryConnection.EnableSharing CONNECTION_ALL End If End If Wscript.Echo (szMsg & vbCrLf & vbCrLf) Next If (con1 <> "list") Then If (bFound1 = False) Then Wscript.Echo ("Connection " & Chr(34) & con1 & Chr(34) & " was not found") End If If (bFound2 = False) Then Wscript.Echo ("Connection " & Chr(34) & con2 & Chr(34) & " was not found") End If End If End Sub '初始化 Function Initialize() Dim bReturn bReturn = False '创建 HNetCfg.HNetShare.1 对象,估计该对象为 OCX 插件之类的东东; Set NetSharingManager = Wscript.CreateObject("HNetCfg.HNetShare.1") If (IsObject(NetSharingManager)) = False Then '检查类型是否为对象; Wscript.Echo ("Unable to get the HNetCfg.HnetShare.1 object") Else If (IsNull(NetSharingManager.SharingInstalled) = True) Then '检查 NetSharingManager 对象中是否存在 SharingInstalled 属性; Wscript.Echo ("Sharing isn't available on this platform.(不可在该平台共享。)") Else bReturn = True End If End If Initialize = bReturn End Function '获取连接对象(返回获取连接对象是否成功的标志) Function GetConnectionObjects() As Boolean Dim bReturn As Boolean bReturn = True If GetConnection(CONNECTION_PUBLIC) = False Then bReturn = False End If If GetConnection(CONNECTION_PRIVATE) = False Then bReturn = False End If If GetConnection(CONNECTION_ALL) = False Then bReturn = False End If GetConnectionObjects = bReturnEnd Function '获取连接(返回获取连接对象是否成功的标志) Function GetConnection(CONNECTION_TYPE) Dim bReturn Dim Connection Dim Item bReturn = True If (CONNECTION_PUBLIC = CONNECTION_TYPE) Then Set Connection = NetSharingManager.EnumPublicConnections(ICSSC_DEFAULT) If (Connection.Count > 0) And (Connection.Count < 2) Then For Each Item In Connection Set PublicConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item) Next Else bReturn = False End If ElseIf (CONNECTION_PRIVATE = CONNECTION_TYPE) Then Set Connection = NetSharingManager.EnumPrivateConnections(ICSSC_DEFAULT) If (Connection.Count > 0) And (Connection.Count < 2) Then For Each Item In Connection Set PrivateConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item) Next Else bReturn = False End If ElseIf (CONNECTION_ALL = CONNECTION_TYPE) Then Set Connection = NetSharingManager.EnumEveryConnection If (Connection.Count > 0) Then Set EveryConnectionCollection = Connection Else bReturn = False End If Else bReturn = False End If If (True = bReturn) Then If (Connection.Count = 0) Then Wscript.Echo ("No " + CStr(ConvertConnectionTypeToString(CONNECTION_TYPE)) + " connections exist (Connection.Count gave us 0)") bReturn = False 'valid to have more than 1 connection returned from EnumEveryConnection ElseIf (Connection.Count > 1) And (CONNECTION_ALL <> CONNECTION_TYPE) Then Wscript.Echo ("ERROR: There was more than one " + ConvertConnectionTypeToString(CONNECTION_TYPE) + " connection (" + CStr(Connection.Count) + ")") bReturn = False End If End If Wscript.Echo (CStr(Connection.Count) + " objects for connection type " + ConvertConnectionTypeToString(CONNECTION_TYPE)) GetConnection = bReturn End Function'转换连接类型为字符串型 Function ConvertConnectionTypeToString(ConnectionID) As String Dim ConnectionString As String If (ConnectionID = CONNECTION_PUBLIC) Then ConnectionString = "public" ElseIf (ConnectionID = CONNECTION_PRIVATE) Then ConnectionString = "private" ElseIf (ConnectionID = CONNECTION_ALL) Then ConnectionString = "all" Else ConnectionString = "Unknown: " + CStr(ConnectionID) End If ConvertConnectionTypeToString = ConnectionString End Function'用法: 'bat cscript /nologo ics.vbs "无线网络连接2" "ChinaNetSNWide" "on" 'netsh wlan start hostednetwork '
Option Explicit
Dim ICSSC_DEFAULT, CONNECTION_PUBLIC, CONNECTION_PRIVATE, CONNECTION_ALL
Dim NetSharingManager 'HNetCfg.HnetShare.1 插件对象(估计是OCX插件)
Dim PublicConnection, PrivateConnection
Dim EveryConnectionCollectionDim objArgs
Dim priv_con, publ_con
Dim switchICSSC_DEFAULT = 0
CONNECTION_PUBLIC = 0 '公有连接
CONNECTION_PRIVATE = 1 '私有连接
CONNECTION_ALL = 2 '全部连接
'主方法入口
Sub Main()
'获取脚本参数对象
Set objArgs = Wscript.Arguments If objArgs.Count = 3 Then
priv_con = objArgs(0)
publ_con = objArgs(1)
switch = objArgs(2) If Initialize() = True Then
'如果已经初始化过 NetSharingManager 对象
'那么,获取连接对象;
GetConnectionObjects
FirewallTestByName priv_con, publ_con
End If
Else
Dim szMsg
If Initialize() = True Then
GetConnectionObjects
FirewallTestByName "list", "list"
End If
'szMsg 为字符串信息;
szMsg = "To share your internet connection, please provide the name of the private and public connections as the argument." & vbCrLf & vbCrLf & _
"Usage:" & vbCrLf & _
" " & Wscript.scriptname & " " & Chr(34) & "Private Connection Name" & Chr(34) & " " & Chr(34) & "Public Connection Name" & Chr(34)
Wscript.Echo (szMsg & vbCrLf & vbCrLf)
End If
End Sub
'通过名称测试防火墙
Sub FirewallTestByName(con1, con2)
On Error Resume Next
Dim Item
Dim EveryConnection
Dim objNCProps
Dim szMsg
Dim bFound1, bFound2 Wscript.Echo (vbCrLf & vbCrLf)
bFound1 = False
bFound2 = False
'遍历所有的连接集合
For Each Item In EveryConnectionCollection
'EveryConnection 对象(网络共享配置接口,由网络连接对象中获得)
Set EveryConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
'objNCProps 为网络连接属性对象;
Set objNCProps = NetSharingManager.NetConnectionProps(Item)
szMsg = "Name: " & objNCProps.Name & vbCrLf & _
"Guid: " & objNCProps.GUID & vbCrLf & _
"DeviceName: " & objNCProps.DeviceName & vbCrLf & _
"Status: " & objNCProps.Status & vbCrLf & _
"MediaType: " & objNCProps.MediaType
'EveryConnection 连接对象是否启用共享;
If EveryConnection.SharingEnabled Then
szMsg = szMsg & vbCrLf & _
"SharingEnabled" & vbCrLf & _
"SharingType: " & ConvertConnectionTypeToString(EveryConnection.SharingConnectionType)
End If If objNCProps.Name = con1 Then
bFound1 = True
If EveryConnection.SharingEnabled = False And switch = "on" Then
szMsg = szMsg & vbCrLf & "Not Shared... Enabling private connection share..."
Wscript.Echo (szMsg)
EveryConnection.EnableSharing CONNECTION_PRIVATE
szMsg = " Shared!"
ElseIf (switch = "off") Then
szMsg = szMsg & vbCrLf & "Shared... DisEnabling private connection share..."
Wscript.Echo (szMsg)
EveryConnection.EnableSharing CONNECTION_ALL End If
End If If objNCProps.Name = con2 Then
bFound2 = True
If EveryConnection.SharingEnabled = False And switch = "on" Then
szMsg = szMsg & vbCrLf & "Not Shared... Enabling public connection share..."
Wscript.Echo (szMsg)
EveryConnection.EnableSharing CONNECTION_PUBLIC
szMsg = " Shared!"
ElseIf (switch = "off") Then
szMsg = szMsg & vbCrLf & "Shared... DisEnabling public connection share..."
Wscript.Echo (szMsg)
EveryConnection.EnableSharing CONNECTION_ALL
End If
End If
Wscript.Echo (szMsg & vbCrLf & vbCrLf)
Next If (con1 <> "list") Then
If (bFound1 = False) Then
Wscript.Echo ("Connection " & Chr(34) & con1 & Chr(34) & " was not found")
End If
If (bFound2 = False) Then
Wscript.Echo ("Connection " & Chr(34) & con2 & Chr(34) & " was not found")
End If
End If
End Sub
'初始化
Function Initialize()
Dim bReturn
bReturn = False '创建 HNetCfg.HNetShare.1 对象,估计该对象为 OCX 插件之类的东东;
Set NetSharingManager = Wscript.CreateObject("HNetCfg.HNetShare.1")
If (IsObject(NetSharingManager)) = False Then
'检查类型是否为对象;
Wscript.Echo ("Unable to get the HNetCfg.HnetShare.1 object")
Else
If (IsNull(NetSharingManager.SharingInstalled) = True) Then
'检查 NetSharingManager 对象中是否存在 SharingInstalled 属性;
Wscript.Echo ("Sharing isn't available on this platform.(不可在该平台共享。)")
Else
bReturn = True
End If
End If
Initialize = bReturn
End Function
'获取连接对象(返回获取连接对象是否成功的标志)
Function GetConnectionObjects() As Boolean
Dim bReturn As Boolean bReturn = True If GetConnection(CONNECTION_PUBLIC) = False Then
bReturn = False
End If If GetConnection(CONNECTION_PRIVATE) = False Then
bReturn = False
End If If GetConnection(CONNECTION_ALL) = False Then
bReturn = False
End If GetConnectionObjects = bReturnEnd Function
'获取连接(返回获取连接对象是否成功的标志)
Function GetConnection(CONNECTION_TYPE)
Dim bReturn
Dim Connection
Dim Item
bReturn = True If (CONNECTION_PUBLIC = CONNECTION_TYPE) Then
Set Connection = NetSharingManager.EnumPublicConnections(ICSSC_DEFAULT)
If (Connection.Count > 0) And (Connection.Count < 2) Then
For Each Item In Connection
Set PublicConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
Next
Else
bReturn = False
End If
ElseIf (CONNECTION_PRIVATE = CONNECTION_TYPE) Then
Set Connection = NetSharingManager.EnumPrivateConnections(ICSSC_DEFAULT)
If (Connection.Count > 0) And (Connection.Count < 2) Then
For Each Item In Connection
Set PrivateConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
Next
Else
bReturn = False
End If
ElseIf (CONNECTION_ALL = CONNECTION_TYPE) Then
Set Connection = NetSharingManager.EnumEveryConnection
If (Connection.Count > 0) Then
Set EveryConnectionCollection = Connection
Else
bReturn = False
End If
Else
bReturn = False
End If If (True = bReturn) Then
If (Connection.Count = 0) Then
Wscript.Echo ("No " + CStr(ConvertConnectionTypeToString(CONNECTION_TYPE)) + " connections exist (Connection.Count gave us 0)")
bReturn = False
'valid to have more than 1 connection returned from EnumEveryConnection
ElseIf (Connection.Count > 1) And (CONNECTION_ALL <> CONNECTION_TYPE) Then
Wscript.Echo ("ERROR: There was more than one " + ConvertConnectionTypeToString(CONNECTION_TYPE) + " connection (" + CStr(Connection.Count) + ")")
bReturn = False
End If
End If
Wscript.Echo (CStr(Connection.Count) + " objects for connection type " + ConvertConnectionTypeToString(CONNECTION_TYPE)) GetConnection = bReturn
End Function'转换连接类型为字符串型
Function ConvertConnectionTypeToString(ConnectionID) As String
Dim ConnectionString As String If (ConnectionID = CONNECTION_PUBLIC) Then
ConnectionString = "public"
ElseIf (ConnectionID = CONNECTION_PRIVATE) Then
ConnectionString = "private"
ElseIf (ConnectionID = CONNECTION_ALL) Then
ConnectionString = "all"
Else
ConnectionString = "Unknown: " + CStr(ConnectionID)
End If ConvertConnectionTypeToString = ConnectionString
End Function'用法:
'bat cscript /nologo ics.vbs "无线网络连接2" "ChinaNetSNWide" "on"
'netsh wlan start hostednetwork
'
你说的是哪个ID和TYPE啊?
你说的是哪个ID和TYPE啊?在最后那个自定义函数ConvertConnectionTypeToString里面的参数ConnectionID,他是怎么确定呢?还有另外一个自定义函数GetConnection里的参数CONNECTION_TYPE也是无法确定,搞不懂
函数ConvertConnectionTypeToString中的参数ConnectionID是由调用这个函数的那行代码来传入的啊。
如上面 207 行代码:Wscript.Echo ("No " + CStr(ConvertConnectionTypeToString(CONNECTION_TYPE))
这里就有调用ConvertConnectionTypeToString函数。而调用函数ConvertConnectionTypeToString时所使用的参数值恰恰是由调用函数GetConnection时传入CONNECTION_TYPE的。