Link to home
Create AccountLog in
Avatar of Mazdajai
MazdajaiFlag for United States of America

asked on

Append a string before an array index variable

Is there a way to correctly append a string before an array index? For some reason there is space added before $ips on the output.

$ips=50..61;$vms=10..21;$i=0;$vms |%{
write-host New-VMHostNetworkAdapter -VMHost bsesx0"$vm"v.abc.com -VirtualSwitch vswitch0 -PortGroup "vmk1" -IP "10.1.1."$ips[$i] -SubnetMask 255.255.255.0 -VMotionEnabled:'$true';$i++
}

Open in new window


Output -
New-VMHostNetworkAdapter -VMHost bsesx021v.bbh.com -VirtualSwitch vswitch0 -PortGroup vmk1 -IP 10.1.1. 61 -SubnetMask 255.255.255.0 -VMotionEnabled: $true

Open in new window

SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Mazdajai

ASKER

It works but why there is a space between the string and variable if I use the following?

Is $() going to work on any variable?

"10.1.1."$ips[$i]

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks! Sometimes I found explanation is more important than solution.
Definitely.. That’s how we learn.. :-)