1

像许多人一样,我们有 Azure VM,我们希望在不使用时销毁这些 VM,这样我们就不必为它们的核心使用付费。所有有问题的虚拟机都在同一个域中,并且 DC/DNS 服务器永远不会被破坏/重新创建,并且具有静态 IP。然而,在成功使用 Export/Remove/Import-AzureVM 的组合后,网络适配器的所有 IP 设置(DNS 是我主要关心的)都消失了,因为每次我使用 Import-重建 VM 时都会创建一个新的网络适配器 -蔚蓝虚拟机。

我最初尝试使用 NETSH 在启动时设置我的 DNS 条目,但这取决于知道适配器的名称并且适配器名称每天都会更改(因为我们要在晚上关闭机器并在早上重新创建它们)。我的下一个不太好的想法是包含一个 VBScript,它在启动时将适配器重命名为相同的名称,这样 NETSH 将始终使用相同的适配器名称来处理。然而,就在那时,我发现所有旧适配器仍然存在,但只是被隐藏且未使用,这使我的计划变得毫无意义。

以下是我尝试使用的测试 NETSH 命令和 VBScript,仅供参考:

'this script was modified from one i got from the Scripting Guys
Const NETWORK_CONNECTIONS = &H31&

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(NETWORK_CONNECTIONS)

Set colItems = objFolder.Items
For Each objItem in colItems  
'only one adapter is ever returned by this query, but it didn't seem like a bad idea to leave the loop alone just in case      
        objItem.Name = "testlan"
    wscript.echo objItem.Name
Next

NETSH

netsh interface ip add dns name="testlan" 10.0.0.4 

我知道我不是唯一一个试图解决这个问题的人,但我一直无法通过大量的谷歌搜索和反复试验找到解决方案。非常感谢!

4

2 回答 2

0

尝试这个...

Set-ExecutionPolicy 不受限制


$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"


$wmi.SetDNSServerSearchOrder("10.0.2.6")

于 2013-10-21T19:25:24.293 回答
0

@Nathan 的评论不正确。当虚拟机“停止”时,它仍在计费。但是,如果它是“已停止(解除分配)”,则计费将停止。来自Azure 的定价详细信息常见问题解答

为确保不向您收费,请从管理门户停止 VM。您还可以通过 Powershell 停止 VM,方法是调用 ShutdowRoleOperation,且“PostShutdownAction”等于“StoppedDeallocated”。但是,如果您从内部关闭虚拟机(例如使用 Windows 中的电源选项)或通过 PowerShell 通过调用 ShutdownRoleOperation 且“PostShutdownAction”等于“已停止”,您将继续被收费。

您可以使用 azure 控制面板进入解除分配状态,而不是销毁 VM,或者使用 Azure Cmdlet 强制停止 VM。这将解除分配,您不会遇到网络问题。不幸的是,目前这不能用 REST Api 完成。

我在应用程序中使用以下内容来停止服务:

RunPowerShellScript(@"Stop-AzureVM -ServiceName " + cloudServiceName + " -Name " + vmName + " -Force");

在按钮上使用该行,或使用 REST api 查询您的云服务,然后使用以下函数运行您的 powershell。请务必首先运行入门指南。

private string RunPowerShellScript(string scriptText)
        {
            // create Powershell runspace
            Runspace runspace = RunspaceFactory.CreateRunspace();
            // open it
            runspace.Open();
            // create a pipeline and feed it the script text
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(scriptText);
            // add an extra command to transform the script
            // output objects into nicely formatted strings
            // remove this line to get the actual objects
            // that the script returns. For example, the script
            // "Get-Process" returns a collection
            // of System.Diagnostics.Process instances.
            pipeline.Commands.Add("Out-String");
            // execute the script
            Collection<PSObject> results = pipeline.Invoke();
            // close the runspace
            runspace.Close();
            // convert the script result into a single string

            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject obj in results)
            {
                stringBuilder.AppendLine(obj.ToString());
            }
            return stringBuilder.ToString();
        }
于 2013-08-13T15:57:15.000 回答