uta*_*ngo 40 windows-7 powershell internet-connection
我可以通过右键单击网络连接,打开共享选项卡并单击“允许其他网络用户通过此计算机的 Internet 连接进行连接”复选框来手动执行此操作。
现在我需要自动化这个任务。是否有命令行工具或 Powershell cmdlet 来完成此操作?
uta*_*ngo 24
这是一个纯 PowerShell 解决方案(应以管理权限运行):
# Register the HNetCfg library (once)
regsvr32 hnetcfg.dll
# Create a NetSharingManager object
$m = New-Object -ComObject HNetCfg.HNetShare
# List connections
$m.EnumEveryConnection |% { $m.NetConnectionProps.Invoke($_) }
# Find connection
$c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Name -eq "Ethernet" }
# Get sharing configuration
$config = $m.INetSharingConfigurationForINetConnection.Invoke($c)
# See if sharing is enabled
Write-Output $config.SharingEnabled
# See the role of connection in sharing
# 0 - public, 1 - private
# Only meaningful if SharingEnabled is True
Write-Output $config.SharingType
# Enable sharing (0 - public, 1 - private)
$config.EnableSharing(0)
# Disable sharing
$config.DisableSharing()
Run Code Online (Sandbox Code Playgroud)
另请参阅social.msdn.microsoft.com 上的此问题:
您必须在要连接的适配器上启用公共接口,并在您希望能够用于网络的适配器的专用接口上启用共享。
我为此创建了一个简单的命令行工具。
下载并解压或git clone git@github.com:utapyngo/icsmanager.git
通过运行构建 build.cmd
注册HNetCfgCOM 库:(regsvr32 hnetcfg.dll它是位于 的标准库%WINDIR%\System32)
以管理员身份打开命令行提示符
cd到icsmanager目录(或者icsmanager-master如果您下载了 zip)。
类型 icsmanager
这应该显示可用的网络连接。注意 GUID 属性。要使用此工具,您至少需要有两个连接。
类型 icsmanager enable {GUID-OF-CONNECTION-TO-SHARE} {GUID-OF-HOME-CONNECTION}
这应该启用 ICS。
导入模块:
导入模块 IcsManager.dll
列出网络连接:
获取网络连接
启动 Internet 连接共享:
启用-ICS“连接共享”“家庭连接”
停止 Internet 连接共享:
禁用-ICS
免责声明:我还没有测试该工具。需要您自担风险使用它。如果某些东西不起作用,请随时在 GitHub 上打开一个问题。也欢迎拉取请求。
小智 1
以下应该有效
netsh routing ip autodhcp install
netsh routing ip autodhcp set interface name="Local Area Connection(or whereever your internet connection is from)" mode=enable
netsh routing ip autodhcp set global 192.168.0.1 255.255.255.0 11520
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79363 次 |
| 最近记录: |