VMware Cloud Community
Sam1230
Enthusiast
Enthusiast
Jump to solution

Assign static IP Address of multiple Windows 2016/2019 Servers from CSV file

Hi,

Hope you are all well.

I have researched this question a lot but still haven't found a correct way to be able to have a csv file with ComputerName and IP address information to set it to Windows VM using Invoke-VMScript.

I have a CSV file

ComputerName,IPAddress,Gateway,Subnet,DNS1,DNS2

This contains multiple VMs and the static IP address that should be assigned.

I know I need to use PowerShell script something like

For IP:

New-NetIPAddress -InterfaceAlias "Ethernet0" -IPAddress $IPAddress -PrefixLength $Subnet -DefaultGateway $Gateway

For DNS:

Set-DnsClientServerAddress -InterfaceAlias "Ethernet0" -ServerAddresses $DNS1, $DNS2

But I am not understanding how I can pass these variables to the VM using Invoke-VMScript.

Would appreciate the help.

Thanks

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You might want to have a look at my dives named Here string and variable substitution and Here strings and the ExpandString method

Using those techniques you pass your variables into the script text you want to run on the target VM via Invoke-VMScript.


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

View solution in original post

5 Replies
Sam1230
Enthusiast
Enthusiast
Jump to solution

It has to be via InvokeVM-Script and I will not be able to use invoke-command. Thanks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You might want to have a look at my dives named Here string and variable substitution and Here strings and the ExpandString method

Using those techniques you pass your variables into the script text you want to run on the target VM via Invoke-VMScript.


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

Sam1230
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Thank you for this.

I gave this an attempt along with other resources and to my suprise I actually have a working script to change the IP addresses (leaving DNS out for now)

$VirtualMachinesCSV = Import-Csv 'C:\Build-info.csv'

Get-Module -Name VMware* -ListAvailable | Import-Module

$UserName = Read-Host -Prompt 'Enter username'

$Password = Read-Host -AsSecureString -Prompt 'Enter password'

foreach($VM in $VirtualMachinesCSV){

    $VMName = $VM.ComputerName

    $IPAddress = $VM.IPAddress

    $Subnet = $VM.Subnet

    $Gateway = $VM.Gateway

$IPScript = @'

New-NetIPAddress -InterfaceAlias "Ethernet0" -IPAddress "xIPAddress" -PrefixLength "xSubnet" -DefaultGateway "xGateway"

'@

$IPScript =

$IPScript.Replace('xIPAddress', $IPAddress).Replace('xSubnet', $Subnet).Replace('xGateway', $Gateway)

Invoke-VMScript -VM $VMName -ScriptText $IPScript -ScriptType Powershell -GuestUser $UserName -GuestPassword $Password

}

This successfully changes the IP addresses according to the CSV file - as I wanted to do.

I have a question though. PowerShell gives the following output after running this script:

Invoke-VMScript : 09/10/2020 20:12:20 Invoke-VMScript A general system error occurred: vix error codes = (1, 0).

At line:23 char:1

+ Invoke-VMScript -VM $VMName -ScriptText $IPScript -ScriptType Powersh ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Invoke-VMScript], SystemError

    + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_GetProcessOutputInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

I am guessing this is a good error? Like error code 0? Because it runs fine and changes the IP but gives the above "error".

Many thanks,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This might be because by changing the IP address of the target VM, the existing connection used by Invoke-VMScript is broken.


What I usually do is to schedule the script to change the IP address as a Scheduled Task that runs a bit later.

That way the Invoke-VMScript returns to the calling script before the connection is interrupted.
Then in that script, in a loop, monitors the VM till the IP address is changed.

If it isn't the script retrieves the log of the scheduled task.


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

0 Kudos
Sam1230
Enthusiast
Enthusiast
Jump to solution

That makes a lot of sense. Thank you for explaining.

I might give the scheduled task a go on Monday and see how it goes. Otherwise I'll stick to this method because as long as I see that error, I know it was more or less successful.

Thanks.

0 Kudos