Reputation: 1243
I have VM with multiple IP/MAC addresses
I'm using this code to get multiple IP/MAC addresses:
$vms=Get-VM | Where { $_.State –eq ‘Running’ } | Select-Object -ExpandProperty Name
foreach($vm in $vms) {
$out=Get-VMNetworkAdapter -vmname $vm | select VMName, MacAddress, IPAddresses
$virtm=$out.VMName
$ip=$out.IPAddresses
if ($ip.Count -gt 1){
foreach($i in $ip.Count) {
if ($ip -match ':'){
$ip = $ip | ?{$_ -notmatch ':'}
}
}
$ip = $ip -join " "
$virtm = ($virtm -split '\n')[0]
}
else {
$ip=$out.IPAddresses
}
$mac=$out.MacAddress
if ($mac.count -gt 1) {
$mac = $mac -join " "
}
foreach($m in $mac) {
$mac=$m.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
}
Write-Output "$virtm, $ip, $mac"
This code works just fine, expect it can add columns to only first MAC address
Current output:
OAP80, 192.168.87.45 192.168.1.45, 00:15:5D:58:12:5E 00155D58125F
I want to add columns to all other MAC addresses for particular VM
Desired output
OAP80, 192.168.87.45 192.168.1.45, 00:15:5D:58:12:5E 00:15:5D:58:12:5F
I tried to add :
before converting collection to to string
$mac=$out.MacAddress
$mac=$mac.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
But getting:
Exception calling "Insert" with "2" argument(s): "Collection was of a fixed size."
At line:35 char:6
+ $mac=$mac.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
Upvotes: 1
Views: 1259
Reputation: 27516
How about this? "$&" means the whole match. There's also negative lookahead to end of line, so a colon doesn't get put at the end.
$mac = echo 00155D58125F 00155D58125G 00155D58125H
$mac = $mac -replace '..(?!$)','$&:'
$mac
00:15:5D:58:12:5F
00:15:5D:58:12:5G
00:15:5D:58:12:5H
Upvotes: 3
Reputation: 743
You dont need foreach
for $mac
Just use after(if mac in format XX-xx-xx-xx-xx-xx)
if ($mac.count -gt 1) {
$mac = $mac -join " "
}
$mac.Replace('-',':')
you can do this(if mac in format xxxxxxxxxxxx) : $mac=$out.MacAddress|foreach{($_.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":"))-join " "}
and can delete this:
if ($mac.count -gt 1) { $mac = $mac -join " " } foreach($m in $mac) { $mac=$m.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":") }
Upvotes: 0
Reputation: 61188
For short, you can do this to put colons in the Mac addresses and merge these as single string with a space as separator:
$mac = ($out.MacAddress | ForEach-Object {
$_.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
}) -join ' '
Write-Output "$virtm, $ip, $mac"
Upvotes: 2
Reputation: 3606
As Lee_Dailey has correctly pointed out: $out.MacAddress
contains an array of strings. You want to go over each element and insert your colons before you join them up into a single string:
$mac = $out.MacAddress
$macWithColons = foreach ($m in $mac)
{
$m.Insert(2, ":").Insert(5, ":").Insert(8, ":").Insert(11, ":").Insert(14, ":")
}
$macWithColons -join " "
Upvotes: 0