Solved

Append a string before an array index variable

Posted on 2013-05-24
5
396 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
  • 3
  • 2
5 Comments
 
LVL 40

Assisted Solution

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

Open in new window

0
 
LVL 21

Author Comment

by:Mazdajai
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks! Sometimes I found explanation is more important than solution.
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Definitely.. That’s how we learn.. :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
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:
Teach the user how to use vSphere Update Manager to update the VMware Tools and virtual machine hardware version Open vSphere Client: Review manual processes for updating VMware Tools and virtual hardware versions: Create a new baseline group in vSp…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now