VMware Cloud Community
VirtuaChris
Contributor
Contributor

Invoke-VMscript - Change VM Guest IP without network connectivity

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.

Tags (1)
0 Kudos
19 Replies
LucD
Leadership
Leadership

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?


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

0 Kudos
VirtuaChris
Contributor
Contributor

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?)

0 Kudos
VirtuaChris
Contributor
Contributor

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)

pastedImage_3.png

0 Kudos
LucD
Leadership
Leadership

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.


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

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
VirtuaChris
Contributor
Contributor

Yes it's a Typo Smiley Happy

same result.

pastedImage_1.png

0 Kudos
VirtuaChris
Contributor
Contributor

PowerCLI Version : 11.5

PowerShell : 5.1.14

VMware Tools : 11269

0 Kudos
LucD
Leadership
Leadership

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.


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

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
VirtuaChris
Contributor
Contributor

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

thanks for your help i'll come back after.

0 Kudos
VirtuaChris
Contributor
Contributor

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

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
VirtuaChris
Contributor
Contributor

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

               }

     }

}

    

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
VirtuaChris
Contributor
Contributor

Hello Luc,

Restarting th PS Session worked fine.

Here's the result :

pastedImage_0.png

0 Kudos
VirtuaChris
Contributor
Contributor

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?

0 Kudos
LucD
Leadership
Leadership

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.


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

0 Kudos
VirtuaChris
Contributor
Contributor

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.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

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.


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

0 Kudos