0

I'm trying to use an invoke-vmscript to modify IPV4 parameters on all our servers using their macaddress.

$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

}    

My problem is that the $interfaceindex is not recognized by the remote computer.

set-dnsclient needs the interfaceindex, that is the reason I'm using $interfaceindex.

Could you help me ?

Maybe there is another way ?

Thank you !

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

= : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:5 + & { = get-netadapter | where-object MacAddress -Like 00-50-56-a1-33-61 | select- ... + ~ + CategoryInfo : ObjectNotFound: (=:String) [], CommandNotFoundEx ception + FullyQualifiedErrorId : CommandNotFoundException

Set-DnsClient : Cannot process argument transformation on parameter 'InterfaceIndex'. Cannot convert value ".InterfaceIndex" to type "System.UInt32[]". Error: "Cannot convert value ".InterfaceIndex" to type "System.UInt32". Error: "Input string was not in a correct format."" At line:1 char:134 + ... InterfaceIndex .InterfaceIndex -UseSuffixWhenRegistering False -RegisterThisConn ... + ~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Set-DnsClient], ParameterBindi ngArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-DnsClie nt

4
  • Try adding a backtick in front of the initial $ sign. Commented Oct 18, 2018 at 12:00
  • I just tried and it still does not work. The error is the following : Set-DnsClient : Missing an argument for parameter 'InterfaceIndex'. Specify a parameter of type 'System.UInt32[]' and try again. At line:1 char:133 + ... set-dnsclient -InterfaceIndex -UseSuffixWhenRegistering True -RegisterThisConn ... + ~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-DnsClient], ParameterB indingException + FullyQualifiedErrorId : MissingArgument,Set-DnsClient – Commented Oct 18, 2018 at 12:58
  • Did you try putting the text into a file and passing the file as your script text? Or possibly changing $dns line from double quotes to single quotes. As it is trying to convert the variables into variables for the DNS string input. Commented Oct 19, 2018 at 3:19
  • @RobertCotterman I already tried to use single quotes but in this case my $mac is empty. Commented Oct 22, 2018 at 9:57

2 Answers 2

1

Using a wmiobject is actually better since it works on both Windows 2008 and 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

}
Sign up to request clarification or add additional context in comments.

Comments

0

There are (at least) two problems with your $dnsstring..

First: Try what @Boxdog said and add a backtick in front of the initial $sign.
Second: You have to use paranthesis around the -InterfaceIndex $($interfaceindex.InterfaceIndex).

Your full string should like like that:

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

Give it a try.

EDIT: Maybe you need to add a backtick before the second $interfaceIndexas well.. (`$interfaceindex.InterfaceIndex)

EDIT2: It is sometimes difficult to understand the use of all those signs :-) . In the documentation for set-dnsClient parameter, it shows that for the parameter InterfaceIndex it "Specifies the index number of the interface."
With your code $interfaceindex = get-netadapter | where-object MacAddress -Like $($mac) | select-object InterfaceIndex you have values in your variable..
Try it with something like this ($interfaceindex.InterfaceIndex[0]) (don't forget the backtick after the "(" paranthesis)

7 Comments

Set-DnsClient : Missing an argument for parameter 'InterfaceIndex'. Specify a parameter of type 'System.UInt32[]' and try again. At line:1 char:133 + ... set-dnsclient -InterfaceIndex -UseSuffixWhenRegistering True -RegisterThisConn ... + ~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-DnsClient], ParameterB indingException + FullyQualifiedErrorId : MissingArgument,Set-DnsClient
I think it's better but the interfaceindex is still a problem. I have difficulties to understand the user of all the ", ', (,[ in such a context !
like this -InterfaceIndex $(`$interfaceindex.InterfaceIndex[0]) ? I tried with and without the backtick and it keeps saying that indexinterface has no value.
The only $ that shouldn't be escaped (backticked) is the $mac. So the interfaceindex and the true variables should all be backticked. A better way to test this is display $dns and see what comes out. Only thing that should come out is the mac because that's dependent upon the loop. Otherwise it should all be created on the far end.
I added a backtick on all $ except $mac and it stil does not work. When I had the backtick to $($interfaceindex.interfaceindex) after the first parenthesis, the error is that the term $interfaceindex.interfaceindex is not recognized as an applet command.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.