Automation

 View Only
  • 1.  Passing local variable to remote script

    Posted Sep 20, 2017 09:45 AM

    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.



  • 2.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 11:11 AM

    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!"



  • 3.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 11:49 AM

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



  • 4.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 12:26 PM

    Can you show what you have in Rename.ps1



  • 5.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 12:38 PM

    Here the attachment.



  • 6.  RE: Passing local variable to remote script
    Best Answer

    Posted Sep 20, 2017 01:05 PM

    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!"



  • 7.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 01:48 PM

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

    VM OS : Windows 7



  • 8.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 01:50 PM

    Can't see an attachment I'm afraid



  • 9.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 01:51 PM

    Please check now



  • 10.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 02:02 PM

    Got it.

    Strange, seems to be working on a Win2012R2 server.

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



  • 11.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 02:07 PM

    Thanks for your response.

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



  • 12.  RE: Passing local variable to remote script

    Posted Sep 20, 2017 09:24 PM

    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)



  • 13.  RE: Passing local variable to remote script

    Posted Sep 21, 2017 06:08 AM

    Great help..Thanks Mr. LuCD

    It works perfectly on windows server 2008R2.

    Regards,

    Srini T