?
Solved

Append a string before an array index variable

Posted on 2013-05-24
5
Medium Priority
?
451 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 1600 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 1600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Teach the user how to rename, unmount, delete and upgrade VMFS datastores. Open vSphere Web Client: Rename VMFS and NFS datastores: Upgrade VMFS-3 volume to VMFS-5: Unmount VMFS datastore: Delete a VMFS datastore:
This video shows you how to use a vSphere client to connect to your ESX host as the root user. Demonstrates the basic connection of bypassing certification set up. Demonstrates how to access the traditional view to begin managing your virtual mac…
Suggested Courses

770 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