返回列表 发帖

[问题求助] vbs设置internet连接共享提示“没有权限”如何解决

关于网络共享给网卡的一个ics.vbs,这代码怎么弄求助
此代码是用来开启双网卡共享的,如果不行还求指导

代码如下,在XP下运行时可以的,但是在win10上就会报错如下,请问该如何解决
'用法 cscript  /nologo ics.vbs "内网适配器名称" "外网网络连接名称" "off/on"
OPTION EXPLICIT
DIM ICSSC_DEFAULT, CONNECTION_PUBLIC, CONNECTION_PRIVATE, CONNECTION_ALL
DIM NetSharingManager
DIM PublicConnection, PrivateConnection
DIM EveryConnectionCollection
DIM objArgs
DIM priv_con, publ_con
DIM switch
ICSSC_DEFAULT       = 0
CONNECTION_PUBLIC   = 0
CONNECTION_PRIVATE  = 1
CONNECTION_ALL      = 2
Main()
sub Main( )
    Set objArgs = WScript.Arguments
    if  objArgs.Count = 3 then
priv_con      = objArgs(0)   '内网适配器名称
publ_con      = objArgs(1)   '外网网络连接名称
switch        = objArgs(2)   '状态切换开关 on 为打开 off 相反
        if Initialize() = TRUE then
            GetConnectionObjects()
            FirewallTestByDeviceName priv_con,publ_con
        end if
    else
        if Initialize() = TRUE then
            GetConnectionObjects()
            FirewallTestByDeviceName "list","list"
        end if
    end if
end sub
sub FirewallTestByDeviceName(con1,con2)
on error resume next
    DIM Item
    DIM EveryConnection
    DIM objNCProps
    DIM szMsg
    DIM bFound1,bFound2
    WScript.echo(vbCRLF)
    bFound1 = false
    bFound2 = false
    for each Item in EveryConnectionCollection
        set EveryConnection = NetSharingManager.INetSharingConfigurationForINetConnection(Item)
        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
        if EveryConnection.SharingEnabled then
            szMsg = szMsg            & vbCRLF & _
                    "SharingEnabled" & vbCRLF & _
                    "SharingType:  " & ConvertConnectionTypeToString(EveryConnection.SharingConnectionType)
        end if
        if objNCProps.DeviceName = con1 then
            bFound1 = true
            if EveryConnection.SharingEnabled = False and switch="on" then
                szMsg = szMsg & vbCRLF & "Not Shared... Enabling private connection share..."
                EveryConnection.EnableSharing CONNECTION_PRIVATE
            end if
if switch="off" then
szMsg = szMsg & vbCRLF & "Shared... DisEnabling private connection share..."
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..."
                EveryConnection.EnableSharing CONNECTION_PUBLIC
            end if
if switch="off" then
szMsg = szMsg & vbCRLF & "Shared... DisEnabling public connection share..."
EveryConnection.EnableSharing CONNECTION_ALL
end if
        end if
        WScript.Echo(szMsg & 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
    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
            Wscript.Echo("Sharing isn't available on this platform.")
        else
            bReturn = TRUE
        end if
    end if
    Initialize = bReturn
end function
function GetConnectionObjects()
    DIM bReturn
    DIM Item
    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 = bReturn
end 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)
    DIM ConnectionString
    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 functionCOPY
1

评分人数

    • pcl_test: 勿发笼统无意义的标题PB -2

还请指出问题或者方向,谢谢了

TOP

初到贵坛并不熟悉,看到DiamondbacK 反馈的帖子才觉得,发这篇帖子挺是对不起论坛的,我承认我是新手和伸手党也有点无耻,和老鸟的境界没得比,甚至最简单的代码都搞不定,小弟以前逛得论坛都没有bathome这么有技术性,此次也是接触了这个ICS共享脚本问题才来的,作为一个新手不敢要求太多,只求贵坛和气不想伤了大神们的心,其实我的问题已经不重要了,一天解决不了两天,总有解决的时候,论坛的长久才是关键,DiamondbacK大神的话在理,在下叨扰了
  PS:我也在百度搜罗了N多相关信息,弄得焦头烂额不得已才请教求助,有时候发帖求助只是想尽快解决问题吧,生活中接触代码的机会太少了,养成了伸手的习惯,惭愧惭愧
                                                                           ——不想删帖是想留句话,起码还有新手有自知之明

TOP

我相信我的问题会解决的,哪怕只有我自己自学自通,祝福贵坛!

TOP

管理员权限试试看

TOP

回复 3# iK-


    淡定,别去搭理那些整天叽叽歪歪的人。

TOP

回复 5# lxzzr


    右键没有管理员运行的选项啊

TOP

xp?
[url=][/url]

TOP

回复 8# 523066680


    拷贝到XP测试可以使用,但小弟的win10里用不了会报错

TOP

回复 9# iK-


    建立批处理.bat  内容为 start xxx.vbs (就是你要运行的vbs

批处理的右键菜单应该有 用管理员身份运行

Win7是这样的
[url=][/url]

TOP

回复 10# 523066680


    谢谢啊,好像可以了,非常感谢

TOP

返回列表