VMware Cloud Community
srinivasanthia
Contributor
Contributor
Jump to solution

Passing local variable to remote script

Hi All,

I have created a script to rename the network adapter in windows virtual machine based its mac address. Below is my script.

$vmname = "w12-core"

$vmacaddress =  "00:50:56:9C:63:8A"

$script =

{

$wmi = Get-wmiobject -Class win32_Networkadapter | where { $_.Macaddress -eq  $vmacaddress }

$wmi.NetconnectionID = 'Newcard'

$wmi.put()

}

Invoke-Vmscript - VM $vmname -ScriptText = $script -Guestuser="Administrator" -GuestPassword = "VMware1!"

Am getting some error while passing macaddress via variable $vmacaddress. Script is getting executed If i mentioned the macaddress directly. ($wmi = Get-wmiobject -Class win32_Networkadapter | where { $_.Macaddress -eq "00:50:56:9C:63:8A" }

Need a help in passing local variable to remote.

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It's a quote and substitution issue.

Try like this

$vmname = "w12-core"

$vmacaddress =  "00:50:56:9C:63:8A"

$script = @"

`$wmi = Get-wmiobject -Class win32_Networkadapter -Filter "MACaddress = '$vmacaddress'"

`$wmi.NetconnectionID = 'Newcard'

`$wmi.put()

"@

Invoke-Vmscript -VM $vmname -ScriptText $script -Guestuser "Administrator" -GuestPassword "VMware1!"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Try with a here-string, like this

$vmname = "w12-core"

$vmacaddress =  "00:50:56:9C:63:8A"

$script = @"

$wmi = Get-wmiobject -Class win32_Networkadapter | where { $_.Macaddress -eq  $vmacaddress }

$wmi.NetconnectionID = 'Newcard'

$wmi.put()

"@

Invoke-Vmscript - VM $vmname -ScriptText $script -Guestuser "Administrator" -GuestPassword "VMware1!"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
srinivasanthia
Contributor
Contributor
Jump to solution

Am getting an error while running the script as mentioned. Attached the output.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you show what you have in Rename.ps1


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
srinivasanthia
Contributor
Contributor
Jump to solution

Here the attachment.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It's a quote and substitution issue.

Try like this

$vmname = "w12-core"

$vmacaddress =  "00:50:56:9C:63:8A"

$script = @"

`$wmi = Get-wmiobject -Class win32_Networkadapter -Filter "MACaddress = '$vmacaddress'"

`$wmi.NetconnectionID = 'Newcard'

`$wmi.put()

"@

Invoke-Vmscript -VM $vmname -ScriptText $script -Guestuser "Administrator" -GuestPassword "VMware1!"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
srinivasanthia
Contributor
Contributor
Jump to solution

Am getting another error now...Here the attachment of both output and script.

VM OS : Windows 7

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can't see an attachment I'm afraid


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
srinivasanthia
Contributor
Contributor
Jump to solution

Please check now

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Got it.

Strange, seems to be working on a Win2012R2 server.

I'll try to get a Win7 box to test further


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
srinivasanthia
Contributor
Contributor
Jump to solution

Thanks for your response.

An additional information. I ran the script directly on the windows 7. Its works perfectly.Here the attachment.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like this Put() issue has been reported several times, but there is no real solution I could find.

Any chance you could upgrade the PowerShell version on those VMs?

Otherwise you could try with the netsh command (see for example Rename a Network Interface from the Command Line)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
srinivasanthia
Contributor
Contributor
Jump to solution

Great help..Thanks Mr. LuCD

It works perfectly on windows server 2008R2.

Regards,

Srini T

Reply
0 Kudos