【问题标题】:Powershell - Invoke-VMScript - How to insert a variable into the script variable?Powershell - Invoke-VMScript - 如何将变量插入脚本变量?
【发布时间】:2018-10-23 23:05:22
【问题描述】:

我正在尝试使用 invoke-vmscript 来修改我们所有服务器上的 IPV4 参数,使用它们的 MAC 地址。

$vms = import-csv C:\temp\adapter_vm.csv -Delimiter ";"

foreach($vm in $vms){ 

 $serveur = $vm.name
 $mac = $vm.macaddress
 $dns = "$interfaceindex = get-netadapter | where-object MacAddress -Like $($mac) | select-object InterfaceIndex; set-dnsclient -InterfaceIndex $interfaceindex.InterfaceIndex -UseSuffixWhenRegistering $true -RegisterThisConnectionsAddress $true -ConnectionSpecificSuffix test"

 Invoke-VMScript -VM $serveur -ScriptType Powershell -ScriptText $dns -GuestUser secret -GuestPassword secret

}    

我的问题是远程计算机无法识别 $interfaceindex。

set-dnsclient 需要 interfaceindex,这就是我使用 $interfaceindex 的原因。

你能帮我吗?

也许还有其他方法?

谢谢!

----------------------------------------------------------------------------------------

= : 术语“=”未被识别为 cmdlet、函数、脚本的名称 文件或可运行的程序。检查名称的拼写,或者路径是否 包括,验证路径是否正确,然后重试。 在行:1 字符:5 + & { = 获取网络适配器 | where-object MacAddress -Like 00-50-56-a1-33-61 | 选择- ... + ~ + CategoryInfo : ObjectNotFound: (=:String) [], CommandNotFoundEx 感觉 + FullyQualifiedErrorId : CommandNotFoundException

Set-DnsClient : 无法处理参数的参数转换 '接口索引'。无法将值“.InterfaceIndex”转换为类型 “System.UInt32[]”。错误:“无法将值“.InterfaceIndex”转换为类型 “System.UInt32”。错误:“输入字符串的格式不正确。”” 在行:1 字符:134 + ... InterfaceIndex .InterfaceIndex -UseSuffixWhenRegistering False -RegisterThisConn ... +~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Set-DnsClient], ParameterBindi ngArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-DnsClie nt

【问题讨论】:

  • 尝试在最初的$ 符号前添加一个反引号。
  • 我刚试过,还是不行。错误如下:Set-DnsClient:缺少参数“InterfaceIndex”的参数。指定“System.UInt32[]”类型的参数,然后重试。在 line:1 char:133 + ... set-dnsclient -InterfaceIndex -UseSuffixWhenRegistering True -RegisterThisConn ... + ~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [设置-DnsClient], ParameterB indingException + FullyQualifiedErrorId : MissingArgument,Set-DnsClient –
  • 您是否尝试将文本放入文件并将文件作为脚本文本传递?或者可能将 $dns 行从双引号更改为单引号。因为它正在尝试将变量转换为 DNS 字符串输入的变量。
  • @RobertCotterman 我已经尝试使用单引号,但在这种情况下我的 $mac 是空的。

标签: powershell invoke


【解决方案1】:

使用 wmiobject 实际上更好,因为它适用于 Windows 2008 和 2012。

$vms = import-csv C:\temp\adapter_vm.csv -Delimiter ";"

foreach($vm in $vms){ 

 $serveur = $vm.name
 $mac = $vm.macaddress
 $dns = "(get-wmiobject -class Win32_NetworkAdapterConfiguration | where-object {`$_.MacAddress -Like ""$($mac)""}).SetDynamicDNSRegistration(`$TRUE, `$TRUE)"

 Invoke-VMScript -VM $serveur -ScriptType Powershell -ScriptText $dns -hostcredential $domaincred

}

【讨论】:

    【解决方案2】:

    您的$dnsstring 有(至少)两个问题..

    首先:试试@Boxdog 所说的,并在初始$sign 前添加一个反引号。
    第二:您必须在-InterfaceIndex $($interfaceindex.InterfaceIndex) 周围使用括号。

    你的完整字符串应该是这样的:

    $dns = "`$interfaceindex = get-netadapter | where-object MacAddress -Like $($mac) | select-object InterfaceIndex; set-dnsclient -InterfaceIndex $($interfaceindex.InterfaceIndex) -UseSuffixWhenRegistering $true -RegisterThisConnectionsAddress $true -ConnectionSpecificSuffix test"
    

    试一试。

    编辑:也许您还需要在第二个$interfaceIndex之前添加一个反引号.. (`$interfaceindex.InterfaceIndex)

    编辑2: 有时很难理解所有这些标志的使用:-)。在 set-dnsClient 参数的文档中,它显示对于参数 InterfaceIndex 它“指定接口的索引号。”
    使用您的代码 $interfaceindex = get-netadapter | where-object MacAddress -Like $($mac) | select-object InterfaceIndex,您的变量中有
    尝试使用类似 ($interfaceindex.InterfaceIndex[0]) 的代码(不要忘记“(”括号后的反引号)

    【讨论】:

    • Set-DnsClient :缺少参数“InterfaceIndex”的参数。指定“System.UInt32[]”类型的参数,然后重试。在 line:1 char:133 + ... set-dnsclient -InterfaceIndex -UseSuffixWhenRegistering True -RegisterThisConn ... + ~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [设置-DnsClient], ParameterB indingException + FullyQualifiedErrorId : MissingArgument,Set-DnsClient
    • 我觉得更好但是interfaceindex还是有问题。我很难理解所有“,”,(,[在这种情况下的用户!
    • 像这样 -InterfaceIndex $(`$interfaceindex.InterfaceIndex[0]) ?我试过带和不带反引号,它一直说 indexinterface 没有价值。
    • 唯一不应该转义(反引号)的 $ 是 $mac。所以 interfaceindex 和 true 变量都应该反引号。更好的测试方法是显示 $dns 并查看结果。唯一应该出现的是mac,因为这取决于循环。否则应该全部在远端创建。
    • 我在除 $mac 之外的所有 $ 上添加了一个反引号,但它仍然不起作用。当我在第一个括号后有 $($interfaceindex.interfaceindex) 的反引号时,错误是术语 $interfaceindex.interfaceindex 未被识别为小程序命令。
    猜你喜欢
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多