Hi,
I am unable to change the IP address. Subnet, Gateway and DNS on multiple VMs using below as I am getting error
$WPassword = "Password@123"
$pass = ConvertTo-SecureString -AsPlainText $WPassword -Force
$GC = New-Object System.Management.Automation.PSCredential (".\Admin", $pass)
# Defines Variables
$VMName = "App02"
$IP = "192.168.2.52"
$SNM = "255.255.254.0"
$GW = "192.168.2.1"
$DNS1 = "192.168.2.5"
$DNS2 = "192.168.2.6"
$code = @'
$wmi={Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"}
New-NetIPAddress -InterfaceIndex $wmi.Index - IPAddress $IP -PrefixLength $SNM -DefaultGateway $GW
'@
# Starts the setup of the IP, Subnetmask, Gateway and DNS
$vm = Get-VM -Name $vmName
$sInvoke = @{
VM = $vm
GuestCredential = $GC
ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)
ScriptType = 'powershell'
}
Write-Host "Setting IP address for $VMname" -ForegroundColor Yellow
Invoke-VMScript @sInvoke
Write-Host "Setting IP address completed." -ForegroundColor Green
Get-VM $VMname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName pg3568 -Confirm:$false
Output error
ScriptOutput
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| = : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
| spelling of the name, or if a path was included, verify that the path is correct and try again.
| At line:1 char:4
| + & {={Get-WmiObject win32_networkadapterconfiguration -filter "ipenabl ...
| + ~
| + CategoryInfo : ObjectNotFound: (=:String) [], CommandNotFoundException
| + FullyQualifiedErrorId : CommandNotFoundException
|
| New-NetIPAddress : Cannot process argument transformation on parameter 'InterfaceIndex'. Cannot convert value ".Index"
| to type "System.UInt32". Error: "Input string was not in a correct format."
| At line:2 char:34
| + New-NetIPAddress -InterfaceIndex .Index -IPAddress 192.168.2.52 -Pref ...
| + ~~~~~~
| + CategoryInfo : InvalidData: (:) [New-NetIPAddress], ParameterBindingArgumentTransformationException
| + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-NetIPAddress
|
|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------