简体   繁体   中英

Powershell - Invoke-VMScript - How to insert a variable into the script variable?

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

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

}

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

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 $interfaceIndex as 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 $interfaceindex = get-netadapter | where-object MacAddress -Like $($mac) | select-object InterfaceIndex $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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM