VMware Cloud Community
tdubb123
Expert
Expert

change win guest IP address

hello

any idea why this isnt working?

Function Set-WinVMIP ($VM, $IP, $SNM, $GW, $DNSA, $DNSB){

$netsh = "netsh interface ip set address ""Local Area Connection"" static $IP $SNM $GW 1"

Invoke-VMScript -VM $VM -GuestCredential $GC -ScriptType bat -ScriptText $netsh

$netsh1 = "netsh interface ip set dns ""Local Area Connection"" static $DNSA primary"

Invoke-VMScript -VM $VM -GuestCredential $GC -ScriptType bat -ScriptText $netsh1

$netsh2 = "netsh interface ip add dns ""Local Area Connection"" addr=$DNSB index=2"

Invoke-VMScript -VM $VM -GuestCredential $GC -ScriptType bat -ScriptText $netsh2

}

$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "", "")

Import-Csv "C:\scripts\vms.csv" -UseCulture | %{

Set-WinVMIP $_.VM $_.IP $_.SNM $_.GW $_.DNSA $_.DNSB $GC

}

no errors but not working

Capture.PNG

0 Kudos
9 Replies
LucD
Leadership
Leadership

I assume that the GuestCredential is the same for all VMs, since you query that outside the ForEach loop?

Can you give the following version a try?

Perhaps the verbosity setting might give some clues what happens.

Function Set-WinVMIP{

    param(

        [string]$VM,

        [string]$IP,

        [string]$SNM,

        [string]$GW,

        [string]$DNSA,

        [string]$DNSB,

        [PSCredential]$GC

    )

    $code = @'

`$VerbosePreference = 'Continue'

netsh interface ip set address "Local Area Connection" static $IP $SNM $GW 1

netsh interface ip set dns "Local Area Connection" static $DNSA primary

netsh interface ip add dns "Local Area Connection" addr=$DNSB index=2

'@

    $ExecutionContext.InvokeCommand.ExpandString($code)

    Invoke-VMScript -VM $VM -GuestCredential $GC -ScriptType Powershell -ScriptText ($ExecutionContext.InvokeCommand.ExpandString($code))

}

$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "", "")

Import-Csv "C:\scripts\vms.csv" -UseCulture | %{

    Set-WinVMIP -VM $_.VM -IP $_.IP -SNM $_.SNM -GW $_.GW -DNSA $_.DNSA -DSNB $_.DNSB -GC $GC

}

On another note, do you have the NetTcpIP module installed in those guests?
That would make changing the IP address a lot easier.


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

0 Kudos
tdubb123
Expert
Expert

Hi lucd

got this error

Capture.PNG

0 Kudos
LucD
Leadership
Leadership

My mistake, there should have been a back-tick before $verbosePreference.

I corrected the code above.

Can you give it another try?


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

0 Kudos
tdubb123
Expert
Expert

hi Lucd

This is what I see now

Screen Shot 2018-09-13 at 5.43.21 AM.png

0 Kudos
LucD
Leadership
Leadership

Can you run it like this?

Function Set-WinVMIP{

    param(

        [string]$VM,

        [string]$IP,

        [string]$SNM,

        [string]$GW,

        [string]$DNSA,

        [string]$DNSB,

        [PSCredential]$GC

    )

    $code = @'

netsh interface ip show config

netsh interface ip set address "Local Area Connection" static $IP $SNM $GW 1

netsh interface ip set dns "Local Area Connection" static $DNSA primary

netsh interface ip add dns "Local Area Connection" addr=$DNSB index=2

'@

    Invoke-VMScript -VM $VM -GuestCredential $GC -ScriptType Powershell -ScriptText ($ExecutionContext.InvokeCommand.ExpandString($code))

}

$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "", "")

Import-Csv "C:\scripts\vms.csv" -UseCulture | %{

    Set-WinVMIP -VM $_.VM -IP $_.IP -SNM $_.SNM -GW $_.GW -DNSA $_.DNSA -DSNB $_.DNSB -GC $GC

}


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

0 Kudos
tdubb123
Expert
Expert

output changed but still got errors

Screen Shot 2018-09-13 at 7.26.30 AM.png

0 Kudos
LucD
Leadership
Leadership

That confirms what I suspected, the NIC is known as "Ethernet", not as "Local Area Connection".

Update the original netsh commands accordingly.


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

0 Kudos
tdubb123
Expert
Expert

yes it does work

DNs got changed but got an error

Screen Shot 2018-09-13 at 12.36.24 PM.png

0 Kudos
LucD
Leadership
Leadership

When you change the DNS server setting with netsh, there is a validation wizard that runs.

The error comes from that wizard.

It could mean that one of the DNS server is not configured correctly or not started.

Can you check?


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

0 Kudos