Powershell-调用VMScript-如何将变量插入脚本变量?
我正在尝试使用invoke-vmscript在所有使用macaddress的服务器上修改IPV4参数Powershell-调用VMScript-如何将变量插入脚本变量?,powershell,invoke,Powershell,Invoke,我正在尝试使用invoke-vmscript在所有使用macaddress的服务器上修改IPV4参数 $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
$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
+&{=get netadapter | where对象MacAddress-如00-50-56-a1-33-61|
选择-。。。
+ ~
+CategoryInfo:ObjectNotFound:(=:String)[],CommandNotFoundEx
概念
+FullyQualifiedErrorId:CommandNotFoundException
Set-DnsClient:无法对参数进行参数转换
“InterfaceIndex”。无法将值“.InterfaceIndex”转换为类型
“System.UInt32[]”。错误:“无法将值“.InterfaceIndex”转换为类型
“System.UInt32”。错误:“输入字符串的格式不正确。”
第1行字符:134
+…InterfaceIndex.InterfaceIndex-注册为False时使用后缀
-注册表此连接。。。
+ ~~~~~~~~~~~~~~~
+CategoryInfo:InvalidData:(:)[Set-DnsClient],参数Bindi
ngArgumentTransformationException
+FullyQualifiedErrorId:参数ArgumentTransformationError,设置DnsClie
nt您的$dns
字符串(至少)有两个问题
首先:试试@Boxdog所说的,并在首字母$
符号前面加一个反勾号。第二:您必须围绕
-InterfaceIndex$($InterfaceIndex.InterfaceIndex)
使用paradensis
您的完整字符串应如下所示:
$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:
有时很难理解所有这些符号的用法:-)。在参数文档中,它显示了参数InterfaceIndex的“指定了接口的索引编号”。使用代码
$interfaceindex=get netadapter | where object MacAddress-比如$($mac)|选择object interfaceindex
变量中有值。使用类似的
($interfaceindex.interfaceindex[0])
(不要忘记“(”后面的反勾号)使用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
}
尝试在初始的
$
符号前面添加反勾号。我刚刚尝试过,但仍然无效。错误如下:Set-DnsClient:缺少参数“InterfaceIndex”的参数。请指定类型为“System.UInt32[]的参数'然后重试。在第1行字符:133+…set-dnsclient-InterfaceIndex-UseSuffixWhen-RegisterWhen-True-RegisterThisConn…++~~~~~~~~~~~~~~~~~~+CategoryInfo:InvalidArgument:(:)[set-dnsclient],ParameterB indingException+FullyQualifiedErrorId:MissingArgument,Set-DnsClient–您是否尝试将文本放入文件并将文件作为脚本文本传递?或者可能将$dns行从双引号更改为单引号。因为它正试图将变量转换为dns字符串输入的变量。@RobertCotterman我已经ied使用单引号,但在本例中my$mac为空。Set-DnsClient:缺少参数“InterfaceIndex”的参数。请指定类型为“System.UInt32[]的参数'然后重试。在第1行字符:133+…set-dnsclient-InterfaceIndex-UseSuffixWhen-RegisterWhen-True-RegisterThisConn…++~~~~~~~~~~~~~~~~~~+CategoryInfo:InvalidArgument:(:)[set-dnsclient],ParameterB indingException+FullyQualifiedErrorId:MissingArgument,Set-DNSClient我认为这样更好,但是interfaceindex仍然是个问题。我很难理解所有“,”,(,[在这样的上下文中!像这样-interfaceindex$(`$interfaceindex.interfaceindex[0])的用户?我尝试了使用和不使用backtick,但它一直说indexinterface没有价值。唯一不应该转义的美元(backtick)是$mac。所以interfaceindex和真变量都应该打勾。更好的测试方法是显示$dns并查看结果。唯一应该出现的是mac,因为它依赖于循环。否则它应该在远端创建。我在所有$mac上添加了一个打勾,除了$mac,它仍然不起作用。当我在第一个括号后回勾$($interfaceindex.interfaceindex)时,错误是术语$interfaceindex.interfaceindex未被识别为小程序命令。