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
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
Hi lucd
got this error
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
hi Lucd
This is what I see now
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
output changed but still got errors
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
yes it does work
DNs got changed but got an error
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