简体   繁体   English

如何通过PowerShell为VPN连接指定DNS服务器?

[英]How to specify DNS server for a VPN Connection by PowerShell?

I am fairly new to PowerShell so please forgive me if the question looks stupid. 我对PowerShell相当陌生,因此如果问题看起来很愚蠢,请原谅我。

I am trying to configure VPN Connections using PowerShell. 我正在尝试使用PowerShell配置VPN连接。 With the help of PowerShell ISE, I have no trouble creating a VPN connection and configuring VPN specifics. 在PowerShell ISE的帮助下,我可以轻松创建VPN连接并配置VPN详细信息。 However I also need to set the DNS server of this VPN to 8.8.8.8, a task of which none of the cmdlets in VpnClient module is capable. 但是,我还需要将此VPN的DNS服务器设置为8.8.8.8, VpnClient模块中的所有cmdlet均无法执行此任务。 I figured that all the settings in "Networking" tab is not accessed by VpnClient module so I tried DnsClient , NetAdapter , NetTCPIP and NetConnection , but the commands in these module all need a parameter called either Name or InterfaceAlias . 我发现VpnClient模块无法访问“网络”选项卡中的所有设置,因此我尝试了DnsClientNetAdapterNetTCPIPNetConnection ,但是这些模块中的命令都需要一个名为NameInterfaceAlias的参数。

I successfully changed the DNS Configuration of my Ethernet Adapter by Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 8.8.8.8,8.8.4.4 . 我通过Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 8.8.8.8,8.8.4.4成功更改了我的以太网适配器的DNS配置。 However, if I give my vpn name to -InterfaceAlias option then PowerShell gives out an error, saying it cannot find such InterfaceAlias. 但是,如果我将我的vpn名称指定为-InterfaceAlias选项,则PowerShell会给出错误,表示找不到此类InterfaceAlias。

I listed all interfaces on my Windows but there's no sign of any vpn connection. 我列出了Windows上的所有接口,但没有任何VPN连接的迹象。 All helps are appreciated, thank you! 感谢所有帮助,谢谢!


Thanks to @DavidBrabant, it seems that it is a bug that Microsoft has not fixed since Windows Vista. 感谢@DavidBrabant,看来这是Microsoft自Windows Vista以来未修复的错误。 I then followed one of the solutions on that kb article: using .Net Class. 然后,我遵循了该kb文章中的一种解决方案:使用.Net类。 The result is still the same as before: no sign of any vpn connection. 结果仍然与以前相同:没有任何VPN连接的迹象。 My input and output look like this: 我的输入和输出看起来像这样:

PS C:\WINDOWS\system32> [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces().NetworkInterfaceType
Wireless80211
Wireless80211
Ethernet
Loopback
Tunnel
Tunnel

I haven't tried the second resolution which is " Use the GetAdaptersAddresses API " because I don't know how. 我没有尝试过第二种解决方案,即“ Use GetAdaptersAddresses API ”,因为我不知道如何做。 Anyone knows? 有谁知道? Or did I miss something from the .Net framework? 还是我错过了.Net框架中的某些功能?

Since I can't comment and/or +1 anything because of switching to new account. 由于切换到新帐户,我无法发表评论和/或+1。 I want to mention that above Antonio's solution works just fine. 我想提一下,安东尼奥的解决方案很好用。 You have to use common sense and understand things a little more. 您必须使用常识并多了解一些东西。 I will elaborate on Antonio's message. 我将详细介绍安东尼奥的信息。 Understand that the people who are answering you are either coding professionals or IT professionals. 了解正在回答您的人员是编码专业人员还是IT专业人员。 So they answer in assumption you are one as well. 因此,他们假设您也是如此。 Please do not -1 anything if you haven't tried it and don't understand it. 如果您没有尝试过并且不理解,请不要-1。 ASK they will help you and explain more if needed. 询问他们将为您提供帮助,并在需要时提供更多解释。

Set-DnsClientServerAddress -InterfaceAlias Corporate -ServerAddresses 8.8.8.8 

Let's break this down. 让我们分解一下。 So what is this saying? 那是什么意思呢?

Set-DnsClientServerAddress

This is assuming you're already connected to the VPN. 假设您已经连接到VPN。 as Antonio actually mentioned. 正如安东尼奥所说。

you have to connect to the VPN first 您必须先连接到VPN

Ok now that we have the understanding that this command through powershell assumes we're connected to the VPN (Only 1 by the way not more than 1). 好的,现在我们已经了解到通过powershell发出的此命令假定我们已连接到VPN(仅1个,最多不超过1个)。

Let's tackle the rest of the command. 让我们处理其余的命令。

-InterfaceAlias Corporate

Ok so those who code undestand that -InterfaceAlias is a Parameter to the Set-DnsClientServerAddress command. 好的,这样,那些代码编写者就不会理解-InterfaceAlias是Set-DnsClientServerAddress命令的参数。 Then following that parameter you have attributes. 然后,在该参数之后,您具有属性。 Now that attribute can either be TEXT (aka a String) or a boolean value (aka $True or $False). 现在,该属性可以是TEXT(aka字符串)或布尔值(aka $ True或$ False)。 So in this case he is saying that his vpn is called Corporate. 因此,在这种情况下,他说他的vpn称为Corporate。 Now with most Windows stuff. 现在有了大多数Windows东西。 Windows will accept strings un quoted if it's all 1 word. 如果全为1个字,Windows将接受未加引号的字符串。 However if your VPN Name has a space in it then it will not accept it. 但是,如果您的VPN名称中有空格,则它将不接受它。 you would have to encapsulate your string with quotes. 您将必须用引号将字符串封装起来。 for example. 例如。

-InterfaceAlias "My VPN"

So in this case you need to know what your VPN Name is. 因此,在这种情况下,您需要知道您的VPN名称是什么。 If you don't know what your VPN Name is, please go to the following location. 如果您不知道您的VPN名称是什么,请转到以下位置。

Control Panel -> Netwok and Sharing Center -> Change Adapter Settings (left hand side) -> net find your VPN Adapter. 控制面板-> Netwok和共享中心->更改适配器设置(左侧)-> net找到您的VPN适配器。

Alright so the last bit. 好吧,最后一点。

-ServerAddresses 8.8.8.8

So this ServerAddresses property is already strictly data typed to an IP address or multiple IP addreseses. 因此,此ServerAddresses属性已经严格地将数据键入到一个IP地址或多个IP地址。 So in this case you would need to know the DNS Server you wish to request DNS resolution from and replace it (8.8.8.8 = Google outside DNS server). 因此,在这种情况下,您将需要知道要向其请求DNS解析的DNS服务器并进行替换(8.8.8.8 = Google以外的DNS服务器)。 So for example 所以举个例子

-ServerAddresses 10.1.10.254

Now this will set the DNS server to be 10.1.10.254 for this VPN Adapter. 现在,此VPN适配器会将DNS服务器设置为10.1.10.254。 Kill the VPN and then start it up again. 终止VPN,然后重新启动。 Open a command prompt and type the following 打开命令提示符并键入以下内容

ipconfig/all 

This will show you all your current adapters. 这将显示所有当前适配器。 For your VPN adapter it should show your updated DNS. 对于您的VPN适配器,它应该显示更新的DNS。 If you still can't resolve DNS names it possibly has to do with your Type of DNS server and you will have to probably specify a DNS suffix manually as well. 如果您仍然无法解析DNS名称,则可能与您的DNS服务器类型有关,并且可能还必须手动指定DNS后缀。 See the following article for more details. 有关更多详细信息,请参见以下文章。

https://superuser.com/questions/966832/windows-10-dns-resolution-via-vpn-connection-not-working https://superuser.com/questions/966832/windows-10-dns-resolution-via-vpn-connection-not-working

Hope this helped. 希望这会有所帮助。 Sometimes us Senior IT guys just don't have the time to type it all out for business owners and/or new to the trade IT personnel. 有时,我们的高级IT人员只是没有时间为企业主和/或新手提供技术信息。

-Grafix -Grafix

To be able to change the interface DNS of a windows VPN you have to connect to the VPN first then use the PS command 为了能够更改Windows VPN的接口DNS,您必须先连接到VPN,然后使用PS命令

Set-DnsClientServerAddress -InterfaceAlias Corporate -ServerAddresses 8.8.8.8 

I did it and after that I was able to join the windows 10 to the domain; 我这样做了,之后我就可以将Windows 10加入域中了。 also first you have to create the VPN using the PS command 同样首先您必须使用PS命令创建VPN

Add-VpnConnection -Name "My VPN" -ServerAddress "x.x.x.x" -TunnelType Pptp -EncryptionLevel Required -AuthenticationMethod MSChapv2 -AllUserConnection  -RememberCredential -PassThru

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM