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.
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
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
Can you show what you have in Rename.ps1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Can't see an attachment I'm afraid
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Please check now
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
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
Great help..Thanks Mr. LuCD
It works perfectly on windows server 2008R2.
Regards,
Srini T