Solved

Append a string before an array index variable

Posted on 2013-05-24
5
424 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
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hypervisor 1 U 10 36
Getting output of invoke command into a combo box 1 13
powershell variable inside quotes 5 21
parsing xml using powershell 6 17
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
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 tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

828 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