?
Solved

Append a string before an array index variable

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

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In this article will go through how to backup a vPostgres DB from a broken vCenter Appliance and restore to a new vCenter Appliance.
Teach the user how to use configure the vCenter Server storage filters Open vSphere Web Client:  Navigate to vCenter Server Advanced Settings: Add the four vCenter Server storage filters: Review the advanced settings: Modify the values of the four v…
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…

839 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