trying to change 2012 windows DNS settings with following
i was able to use
invoke-vmscript -VM $VM -scripttext {ipconfig /all}
this worked just fine. Also I checke the name of the ethernet name and it is correct "
I think the reason was some VM has ethernet interface labelled different
some as Ethernet0
some as Ethernet
some as PROD
any idea?
The following works for me
$DC = Read-Host "Enter Datacenter"
$GuestCred = $Host.UI.PromptForCredential("Please enter admin credentials", "Enter Guest credentials", "", "")
$PrimaryDNS = Read-Host "Primary DNS: "
$SecondaryDNS = Read-Host "Secondary DNS: "
$code1 = @"
netsh interface ip set dns Ethernet0 static $PrimaryDNS && netsh interface ip add dns Ethernet0 $SecondaryDNS
"@
$code2 = @'
ipconfig /all
'@
Get-Datacenter $DC | Get-VM |
Where-Object { $_.Name -notlike "*dc*" -and $_.powerstate -eq "PoweredOn" } |
ForEach-Object {
Invoke-VMScript -VM $_ -GuestCredential $GuestCred -ScriptType "bat" -ScriptText $code1
# Verify setting
Invoke-VMScript -VM $_ -GuestCredential $GuestCred -ScriptType "bat" -ScriptText $code2
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Then you would need to find the name of the interface first, and substitute that in the $code1 variable
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
is there way to get the variable from invoke-vmscript.?
Try like this
$code0 = @"
(Get-NetAdapter | ? {$_.Status -eq "up"}).name
"@
$nic = (Invoke-VMScript -VM $_ -GuestCredential $GuestCred -ScriptType "PowerShell" -ScriptText $code0).ScriptOutput
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
kept getting
ScriptOutput
---------------------------------------------------------------------------------------------------------------------------------------------------------
| & was unexpected at this time.
|
----------------------------------------
I didn't notice you had "bat" as the ScriptType, I corrected it to PowerShell.
Try again
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference