Automation

 View Only
Expand all | Collapse all

Invoke-VMscript - Change VM Guest IP without network connectivity

  • 1.  Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 12:14 PM

    Hello,

    I'm trying to build a script that need to change IP of some VM before we connect it to the network (because it's a clone on the same network).

    But  a simple test is not working : Invoke-VMscript General system error occured: vix error codes = (1, 0)

    $script = 'Get-NetIpAddress'

    $Param = @{

         vm =$vm

         GuestCredential = $Credential["domain"]

         ScriptType = 'Powershell'

    }

    $invoke = Invoke-VMScript @Param

    Test Get-NetIpAddress on the guest with the credential is working

    Guest OS is Windows 2012R2 64bits

    Is someone can help me?

    Thanks.



  • 2.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 12:18 PM

    That is a rather generic error message, often it indicates an error with the guest credentials.
    How did create the object that you pass on the GuestCredential parameter?



  • 3.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 12:31 PM

    I'm importing a json and convert in secure password

    $import = Get-Content $script:exec_dir\conf\Fichier2_TestPSI.json | ConvertFrom-Json

    $objPwd = New-Object psobject

    foreach ($Credential in $importJSON)

    {

      $objPwd = "" | Select-Object Type, Login, Password

      $objPwd.Type = $Credential.Type

      $objPwd.Login = $Credential.Login

      $objPwd.Password = $Credential.Password | Convertto-SecureString -Key $script:key -ErrorAction stop

      $SecurePasswords += $objPwd

      }

    Foreach ($myCredential in $SecurePasswords)

    {

        $myCredentials = New-Object System.Management.Automation.PSCredential($myCredential.Login,$myCredential.Password)

        $script:AllCredentials.add($myCredential.Type,$myCredentials)

    }

    This is working for all other credentials

    (can we speak in french?)



  • 4.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 12:48 PM

    I assume the discrepancy between $import (1st line) and $importJSON (5th line) is a typo?

    I don't think you can create a PSCredential object the way you are doing it.
    I normally use something like this

    $cred = New-Object PSCredential -ArgumentList ('user',(ConvertTo-SecureString -String 'lala' -AsPlainText -Force))

    And I pass the $cred variable on the GuestCredential parameter.



  • 5.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 12:57 PM

    Yes it's a Typo :smileyhappy:

    same result.



  • 6.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 01:06 PM

    As an alternative, can you try to do the same thing with my Invoke-VMScriptPlus function.
    With the Verbose switch, it might offer a bit more feedback.



  • 7.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 02:15 PM

    invoke-vmscriptplus send me hundred of error page with code of my script inside the error. i'm probably do something wrong.



  • 8.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 02:17 PM

    That seems to happen from time to time, no clue why.
    Restarting the PS sessions most of the time helps.



  • 9.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 30, 2020 02:02 PM

    Hello Luc,

    Restarting th PS Session worked fine.

    Here's the result :



  • 10.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 30, 2020 03:30 PM

    It looks as if there might be a PowerShell issue on the machine where you get the errors with Invoke-VMScriptPlus.

    There is at least a module, PSReadLine, missing.

    That could indeed be the cause of the issue.

    And no, the VM does not need network access to be able to use Invoke-VMScript.



  • 11.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Oct 01, 2020 07:58 AM

    Hi Lucd,

    About

    And no, the VM does not need network access to be able to use Invoke-VMScript.

    $vm = get-vm -name prod1 (network connected)

    $vm1 = get-vm -name prod1_clone (network disconnected)

    $script = 'get-netipaddress'

    Without the network, i think invoke-vmscript cannot contact the AD to authentify user or something like that.



  • 12.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Oct 01, 2020 08:43 AM

    If you are of course using credentials that need the network to authenticate, then yes, you will need network access.
    If you use a local account, you will not need network access.

    But that is all related to the guest credentials you are using.

    My remark was that to use Invoke-VMScript, you do not need network access on the target VM.



  • 13.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 12:43 PM

    if i try to remove the guestcredential param :

    invoke-vmscript -scripttext 'get-netipaddress' -vm $vm

    the result here : (the user used is the good one)



  • 14.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 12:55 PM

    Which PowerCLI version are you using?
    And which version of VMware Tools do you have running in that VM?



  • 15.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 01:04 PM

    PowerCLI Version : 11.5

    PowerShell : 5.1.14

    VMware Tools : 11269



  • 16.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 01:07 PM

    As an alternative, you might be experiencing the same issue as described in Invoke-VMScript failed when guestcredential is ... |VMware Communities



  • 17.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 02:05 PM

    i'll try to update the ESXi and then the vmware tools.

    thanks for your help i'll come back after.



  • 18.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 02:58 PM

    anyway, assuming the invoke-vmscript is working i have no idea how to fix my changing ip issue.

    I have a table with the source IPs and the destination IPs.

    I imagine the correct method is to do a foreach loop of my VMs to modify and apply the modification to each one with something like that?

    iptables.json :

    [{"ipsource":"10.0.0.1","ipdest":"10.20.0.1},

    {"ipsource2":"10.0.0.2","ipdest2":"10.20.0.2}]

    $importJSON = Get-Content c:\iptables.json | ConvertFrom-Json

    $IPtables = @{}

    foreach ($thisIP in $importJSON)

    {

         $IPtables.add($ThisIP.ipsource,$ThisIP.ipdest)

    }

    $testVMs = get-vm -Location $folderofVMs

    foreach ($thisVM in $testVMs)

    {

         foreach ($thisIP in $importJson.ipsource)

         {

              $script = '(get-netipaddress | where-object {$_.IPaddress -match "' + $thisIP +  '"-and $_.AddressFamily -eq "IPv4"}).IPAddress'

              $sourceIP = invoke-vmscript -scripttext $script -vm $thisVM

              if ($sourceIP -eq $thisIP)

                   {

                   $script2 = $changingIP = '%WIWNDIR%\system32\netsh.exe interface ipv4 set address name = "Eth0" source=static address=' + $IPTables[$sourceIP] + 'mask=255.255.255.0 gate

                   }

         }

    }

        



  • 19.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 29, 2020 03:31 PM

    If the VMware Tools are running on those VM, you don't have to call the 1st Invoke-VMScript.
    The IP address available through the Guest property of the VM



  • 20.  RE: Invoke-VMscript - Change VM Guest IP without network connectivity

    Posted Sep 30, 2020 02:12 PM

    Luc,

    I'm trying to do the invoke-vmscript on the production VM and it works.

    The only difference between the 2 VM is the network access.

    VM1 production =>  have access to the AD

    VM2 Clone =>  have no access to the AD

    So there's no way to change the IP of a VM without network connectivity?