Solved

Append a string before an array index variable

Posted on 2013-05-24
5
442 Views
Last Modified: 2013-05-24
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

0
Comment
Question by:Mazdajai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 400 total points
ID: 39195342
Try
"10.1.1.$($ips[$i])"

Open in new window

0
 
LVL 21

Author Comment

by:Mazdajai
ID: 39195357
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

0
 
LVL 40

Accepted Solution

by:
Subsun earned 400 total points
ID: 39195796
You can also use 10.1.1.$($ips[$i]). Because these characters are not recognized as a valid expression and PowerShell consider it as an expandable string and pass it to Write-host.

When you use "10.1.1.$($ips[$i])”, again PowerShell consider it as a single expandable string since both the 10.1.1. and var is inside the double quotes.

When you use "10.1.1."$ips[$i], it become two valid expressions. So PowerShell interpret it as two separate expressions before passing them to Write-Host and Write-Host separates them with a separator.
0
 
LVL 21

Author Closing Comment

by:Mazdajai
ID: 39195800
Thanks! Sometimes I found explanation is more important than solution.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39195811
Definitely.. That’s how we learn.. :-)
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Teach the user how to configure vSphere Replication and how to protect and recover VMs Open vSphere Web Client: Verify vsphere Replication is enabled: Enable vSphere Replication for a virtual machine: Verify replicated VM is created: Recover replica…
This Micro Tutorial walks you through using a remote console to access a server and install ESXi 5.1. This example is showing remote access and installation using a Dell server. The hypervisor is the very first component of your virtual infrastructu…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question