在 Azure 自动化中编译包含 Get-NetAdapter 的 DSC 配置

Compiling DSC Config that contains Get-NetAdapter in Azure Automation

many Azure Quick Start examples 中所示,通常使用 Get-NetAdapter 获取网络接口名称以进行 DNS 配置等操作。这是一个例子:

configuration MyConfig
{

    $Interface=Get-NetAdapter|Where Name -Like "Ethernet*"|Select-Object -First 1
    $InterfaceAlias=$($Interface.Name)

    Node localhost
    {
        xDnsServerAddress DnsServerAddress
        {
            Address        = $DNSServer
            InterfaceAlias = $InterfaceAlias
            AddressFamily  = 'IPv4'
        }
   }
}

如果命令 Get-NetAdapter 在我的配置中并且配置是由 Azure Automation 编译的,我会收到以下错误:

Cannot connect to CIM server. The specified service does not exist as an installed service.

有解决办法吗?

试试这个:

    xDnsServerAddress DnsServerAddress
    {
        Address        = $DNSServer
        InterfaceAlias = (Get-NetAdapter | ? name -Like "Ethernet*" | select -First 1).Name
        AddressFamily  = 'IPv4'
    }

Get-NetAdapter 在内部使用 WMI 获取在 Azure 自动化中不起作用的信息。但是,您可以使用 get-netipinterface cmdlet 获取有关适配器的信息。

答案是——不可能。配置在 Azure 自动化服务器上​​编译,而不是在目标节点上编译。即使我想办法在配置中获取网络适配器名称,它也会获取 DSC 拉取服务器上的适配器名称,而不是目标节点。

如果每个节点使用 1 个配置并且在目标节点上预编译然后将其上传到 Azure 自动化,问题中的代码将起作用。